FasterXML / jackson-dataformats-text

Uber-project for (some) standard Jackson textual format backends: csv, properties, yaml (xml to be added in future)
Apache License 2.0
407 stars 149 forks source link

(csv) Is there some way to use the Spring MessageSource for column header? #18

Open temp-impl opened 7 years ago

temp-impl commented 7 years ago

Or I need to join column header after writeValueAsString() ?

Below is similar problem: http://stackoverflow.com/questions/35295997/set-localized-column-title-in-csv-file-using-jackson

cowtowncoder commented 7 years ago

I don't know what MessageSource is, but no dependencies to Spring libraries should be added. So I am guessing answer would be "no".

But in general I don't think it is a good idea to integrate localization quite at this level: you can, however, programmatically construct CsvSchema using translations.

temp-impl commented 7 years ago

Thanks for the answer.

Is there good way to do programmatically construct CsvSchema using translations? I tried the following, but... (My goal is to output a CSV file with a localized header from the list of POJO class)

cowtowncoder commented 7 years ago

@temp-impl it is possible to just construct whole CsvSchema programmatically, it need not come from a POJO. But I guess what you are asking is sort of renaming operation, which does not really exist at this point. It is of course possible to use different schema for reading and writing, but challenge still remains between naming used in POJOs (if used) and output.

I'll have to think about this: I don't really have a good idea how this could or should work. But I do think this is bit more general than just CSV: in theory one might want to rename properties of JSON or XML or Properties file as well.

Further, these would be somewhat dynamic translations, somewhat similar to @JsonFilter in that different mapping could be used on per-call basis (on serialization). Existing renaming systems do not allow this; not even NamingStrategy: assumption is there is just one mapping.

Interesting idea. Just not sure how to go about it.

tschlegel commented 3 years ago

I was looking for a solutions as well. I worked around this problem by writing header (first line) and content of the csv seperatly as follows:

   CsvSchema schema = csvMapper.schemaFor(clazz).withHeader();
    Builder builder = schema.rebuild();

    for (Column column : schema) {
        String localizedName= messageSource.getMessage(column.getName(), null, column.getName(), locale);
        builder.renameColumn(column.getIndex(), localizedName);
    }

    CsvSchema renamedSchema = builder.build();
    String header = csvMapper.writer(renamedSchema).writeValueAsString(List.of());

    // write content
    CsvSchema noheaderSchema = csvMapper.schemaFor(clazz).withoutHeader();
    String csvData = csvMapper.writer(schemaRenamed).writeValueAsString(data);`
    String localizedCSV = header + csvData
cowtowncoder commented 3 years ago

Hmmh. I would have thought that it should be possible to simply create ObjectWriter with desired schema, and that should... work?