4

I try to serialize object containing java.time.LocalDate in two ways but both failed.

  1. Annotation with @JsonUnwrapped and @JsonFormat(shape=JsonFormat.Shape.STRING) get error:

    Exception in thread "main" com.fasterxml.jackson.dataformat.csv.CsvMappingException: CSV generator does not support Object values for properties (nested Objects) (through reference chain: java.util.ArrayList[0]->models.DateRange["finish"]->java.time.LocalDate["chronology"])

@Data
@AllArgsConstructor
@NoArgsConstructor
//@JsonFormat(shape=JsonFormat.Shape.STRING)
public class DateRange {
  @JsonUnwrapped
  @JsonFormat(shape=JsonFormat.Shape.STRING)
  private LocalDate start;
  @JsonUnwrapped
  @JsonFormat(shape=JsonFormat.Shape.STRING)
  private LocalDate finish;
}

--------
public class CsvPractice {
  public static void main(String[] args) throws JsonProcessingException {
    ArrayList<DateRange> csv = Lists.newArrayList();
    for (int i = 1; i < 6; i++) {
      csv.add(new DateRange(LocalDate.now().plusDays(i), LocalDate.now().plusDays(i + 1)));
    }
    CsvMapper mapper = new CsvMapper();
//    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    CsvSchema schema = mapper.schemaFor(DateRange.class).withHeader();
    System.out.println(mapper.writer(schema).writeValueAsString(csv));
  }
}
  1. Config mapper mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); without annotation on objects but get:

    Exception in thread "main" com.fasterxml.jackson.dataformat.csv.CsvMappingException: CSV generator does not support Object values for properties (nested Objects) (through reference chain: java.util.ArrayList[0]->models.DateRange["finish"])

@Data
@AllArgsConstructor
@NoArgsConstructor
//@JsonFormat(shape=JsonFormat.Shape.STRING)
public class DateRange {
//  @JsonUnwrapped
//  @JsonFormat(shape=JsonFormat.Shape.STRING)
  private LocalDate start;
//  @JsonUnwrapped
//  @JsonFormat(shape=JsonFormat.Shape.STRING)
  private LocalDate finish;
}
---------
public class CsvPractice {
  public static void main(String[] args) throws JsonProcessingException {
    ArrayList<DateRange> csv = Lists.newArrayList();
    for (int i = 1; i < 6; i++) {
      csv.add(new DateRange(LocalDate.now().plusDays(i), LocalDate.now().plusDays(i + 1)));
    }
    CsvMapper mapper = new CsvMapper();
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    CsvSchema schema = mapper.schemaFor(DateRange.class).withHeader();
    System.out.println(mapper.writer(schema).writeValueAsString(csv));
  }
}

My jackson version is:

    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-csv</artifactId>
      <version>2.9.8</version>
    </dependency>

Anything I missed?

Refer

Community
  • 1
  • 1
FakeAlcohol
  • 860
  • 7
  • 28

1 Answers1

8

Your code is nearly there. Your problem is that your mapper is not serializing LocalDate correctly.

To enable Jackson's serialization of Java 8 date and time types you need to include the following module:

https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.10.1

After doing so, you'll need to modify your code to register the JSR-310 module for Jackson and then instruct it to disable SerializationFeature.WRITE_DATES_AS_TIMESTAMPS.

So based on the above, include the module and then just do the following code change:

CsvMapper mapper = new CsvMapper();
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

After doing your expected result should look something like:

finish,start
2020-01-16,2020-01-15
2020-01-17,2020-01-16
2020-01-18,2020-01-17
2020-01-19,2020-01-18
2020-01-20,2020-01-19
akortex
  • 5,067
  • 2
  • 25
  • 57