jackson-dataformat-csv: cannot serialize LocalDate
Asked Answered
S

1

9

When I try to serialize object containing Local date, I get following error:

csv generator does not support object values for properties

I have JSR-310 module enabled, with WRITE_DATES_AS_TIMESTAMPS and I can convert the same object to JSON without problem.

For now I resorted to mapping the object to another, string only object, but it's decadent and wasteful.

Is there a way for Jackson csv mapper to acknowledge localDates? Should I somehow enable JSR-310 specifically for csv mapper?

Sosa answered 24/5, 2017 at 8:17 Comment(0)
C
15

I had the same problem because of configuring mapper after schema. Make sure you are using the latest verson of jackson and its modules. This code works for me:

final CsvMapper mapper = new CsvMapper();
mapper.findAndRegisterModules();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); //Optional
final CsvSchema schema = mapper.schemaFor(PojoWithLocalDate.class);
// Use this mapper and schema as you need to: get readers, writers etc.

No additional annotations needed in Pojo class.

Chlo answered 25/5, 2017 at 6:0 Comment(2)
Congratulations on your first accepted answer! Keep up the good work.Sosa
some important things I have noticed : 1) findAndRegisterModules is required, otherwise the error will persist 2) mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) can be replaced by Mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) 3) confirmed that no annotations are needed in Pojo classFlyaway

© 2022 - 2024 — McMap. All rights reserved.