I have string which contains "," and need to serialize/deserialize it as CSV. i've been able to serialize it correctly, but when deserialize, it always wrong. here is my code in java
List<String> header = ...
test1,
test2, test3 <-- string contains comma ,
test4
List<String> updatedHeaderList = Lists.transform(headerList, new Function<String, String> () {
public String apply(String input) {
return StringEscapeUtils.escapeCsv(input);
}
});
Joiner joiner = Joiner.on(separator);
stringBuilder = joiner.appendTo(stringBuilder, updatedHeaderList);
stringBuilder.append("\n");
return stringBuilder.toString();
as i checked the serialized string it is: test1, "test2\, test3", test4 which looks right to me
then i tried to deserialize it:
Splitter.on(",").splitToList(resultString)
it returned list as
test1,
test2,
test3,
test4
how can i deserialized it back to
test1
test2, test3
test4
thanks
As per RFC 4180:
Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
So, the valid csv content has to be:
test1
"test2, test3"
test4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With