Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson-dataformat-csv: cannot serialize LocalDate

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?

like image 943
charlie_pl Avatar asked May 24 '17 08:05

charlie_pl


1 Answers

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.

like image 109
Константин Токарев Avatar answered Oct 25 '22 10:10

Константин Токарев