Jackson CSV deserialization ignoring header order
Asked Answered
Z

1

6

I'm trying to deserialize a csv(tsv) file into an immutable POJO.

Jackson is assigning values to the wrong fields (it's ignoring the annotated column header name) The class fields are written in the same order as found in the file.

Do I really have to create a custom CSV schema for this? What's the point of "withHeader()" anyway, just ignore the first line??

I tried editing the csv(tsv) file's headers changing the name to something not annotated, but no error occurs.

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Person(@JsonProperty("name") String name, 
        @JsonProperty("address") String address, 
        @JsonProperty("phone") String phone) {
    this.name = name;
    ...
}


CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Person.class).withColumnSeparator('\t').withHeader();
MappingIterator<Person> it = mapper.readerFor(Person.class).with(schema).readValues(file);
Zombie answered 15/5, 2019 at 2:32 Comment(1)
@JsonPropertyOrder is also ignored...Zombie
E
0

I was having the same problem but when I added the withColumnReordering flag, it started to work.

CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Person.class).withColumnSeparator('\t').withHeader().withColumnReordering(true);

I don't have the @JsonCreator annotation on my POJO but I do have the @JsonProperty annotation on all of my member fields.

Ezequieleziechiele answered 7/6, 2024 at 12:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.