reading a csv file with null values an map it in a POJO

1,199 views
Skip to first unread message

Michael

unread,
May 30, 2016, 7:41:42 AM5/30/16
to jackson-user
hi all,

i have a problem with the csv objectreader -  i want to read comma-separated string in a pojo. - the problem are the null values in the String - all null values are mapped to blank "" in my pojo. 

example:

String: "Value1","Value2",,,,"Value6"

mapped POJO:

V1: "Value1"
V2: "Value2"
V3: ""
V4: ""
V5: ""
V6: "Value6"


but i want to have 

V1: "Value1"
V2: "Value2"
V3: null
V4: null
V5: null
V6: "Value6"

I tried the options - but nothing happens:
 
 csvSchema.withNullValue(null);

csvSchema.withNullValue("n/a");

.with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)

what is wrong?

thanks
Michael

Tatu Saloranta

unread,
May 31, 2016, 3:50:35 PM5/31/16
to jackso...@googlegroups.com
If empty String should map to Java `null`, you should specify that as "null value":

CsvSchema schema = schema.withNullValue("");

Note, too, that it is not enough to call `withNullValue()` but also make sure you use returned objects: `CsvSchema` objects are immutable so methods do NOT change instance you call method on but rather create a new differently configured instance.

I hope this helps,

-+ Tatu +-




--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Michael

unread,
Jun 3, 2016, 5:17:13 AM6/3/16
to jackson-user
ok it works (with the returned object from the Schema)  - thank you - but if now a field with "" in the csv file that is also zero. 

Is it possible to to distinguish between ,"","" , and ,,, ?

String: "Value1","Value2","",,,"Value6"

mapped POJO:

V1: "Value1"
V2: "Value2"
V3: ""
V4: null
V5: null
V6: "Value6"

thanks 

Tatu Saloranta

unread,
Jun 3, 2016, 10:46:15 AM6/3/16
to jackso...@googlegroups.com
No, since the contents are the same logically, after taking out quotes (and possibly quoted quotes inside.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages