Support for collection fields
I have a class:
public class Person {
@CsvColumn(columnName = "name")
private String name;
@CsvColumn(columnName = "phones")
private List<String> phones;
}
where phones could have 0...* elements;
I'd expect that such data (in JSON for readability):
[{ name: "Foo", phones: ["11"] }, { name: "Foo", phones: ["22", "23"] }, { name: "Foo", phones: [] }]
transformed by this code:
CsvProcessor<Person > csvProcessor = new CsvProcessor<>(Person .class);
File csvFile = new File("people.csv");
csvProcessor.writeAll(csvFile, list, true);
should output something like this:
name,phones/0,phones/1
Foo,11,
Foo,22,23
Foo,,
exactly like this tool https://konklone.io/json/ does it
but it throws:
java.lang.IllegalArgumentException: No converter available for type: interface java.util.List
at com.j256.simplecsv.processor.ColumnInfo.fromAnnoation(ColumnInfo.java:242)
at com.j256.simplecsv.processor.ColumnInfo.fromAnnotation(ColumnInfo.java:219)
at com.j256.simplecsv.processor.CsvProcessor.addColumnInfo(CsvProcessor.java:1183)
at com.j256.simplecsv.processor.CsvProcessor.configureEntityClass(CsvProcessor.java:1017)
at com.j256.simplecsv.processor.CsvProcessor.checkEntityConfig(CsvProcessor.java:997)
at com.j256.simplecsv.processor.CsvProcessor.writeAll(CsvProcessor.java:414)
at com.j256.simplecsv.processor.CsvProcessor.writeAll(CsvProcessor.java:398)
Could you please support collections in SimpleCSV?
One solution is that @CsvColumn could have boolean parameter collectionValues (because names can be generated using name as a base)
Kind regards
Hrmm. Interesting idea. Thanks for it.
So would this list/array enabled field be just for the last one? So the last one can be effectively a ... field or something? name,phones... or something?
I think that it could be in any order.
While writing - it does not matter - you just need to reserve constant quantity of columns equal to max(row1.list.size, row2.list.size, ...).
While reading - you can constrain column name pattern like name/N and then collect N columns as Set
Hello, I like the idea, maybe the output can be specified by a delimiter in the annotation :
@CsvColumn(columnName = "phones", delimiter ="-")
private List<String> phones;
name,phones Foo,11 Foo,22-23 Foo,11-99-88 Foo,
Pretty nice solution @hatim-heffoudhi ! Thanks.