FasterXML / jackson-dataformats-text

Uber-project for (some) standard Jackson textual format backends: csv, properties, yaml (xml to be added in future)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weird issue with dates

dabaus opened this issue · comments

I create my mapper:

private static final CsvMapper CSV_MAPPER = new CsvMapper();

static {
    CSV_MAPPER.configure(CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING, true);
    CSV_MAPPER.registerModule(new JavaTimeModule());
}

I set up my csv schema like this

final CsvSchema csvSchema = CSV_MAPPER
    .schemaFor(MyCsvOutputRow.class)
    .withHeader()
    .withColumnSeparator('|')
    .withNullValue("")
    .withLineSeparator("\r\n");

And then i do something like

try (SequenceWriter csvWriter = CSV_MAPPER.writer(csvSchema).writeValues(writer)) {
    for (var entry : myEntries) {
        final MyCsvOutputRow row = ...
        csvWriter.write(row);
    }
}

My problem is that with this setup, whenever i have a date for example 2024-01-30 it gets encoded as 2024;1;30. And it does not seem to matter if i use @jsonformat or format it myself and pass it in as string. It still gets escaped.

What is causing this escape to happen?

Never mind, i had forgotten a JsonFormat and for some reason ; was in the default format?

Yes, that output format suggests that code was trying to serialize value as Array, which gets encoded using semicolons by default (configurable but still).

But with CSV you definitely would want "as-String" instead. This goes to configuration of JavaTimeModule.