Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for collection fields #9

Open
eximius313 opened this issue Feb 23, 2018 · 4 comments
Open

Support for collection fields #9

eximius313 opened this issue Feb 23, 2018 · 4 comments

Comments

@eximius313
Copy link

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

@j256
Copy link
Owner

j256 commented Feb 23, 2018

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?

@eximius313
Copy link
Author

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

@hatim-heffoudhi
Copy link

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,

@j256
Copy link
Owner

j256 commented Dec 4, 2018

Pretty nice solution @hatim-heffoudhi ! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants