Null values are written with quotes when writing bean to a file, although...
Brought to you by:
aruckerjones,
sconway
Hi I have problem when exporting to File empty Strings and null values. I found some dicsussion here and also looked at the corresponding test from the source code, but still didn't manage to make it as i want.
Writer writer = new FileWriter(file);
CSVParser csvParser = new CSVParserBuilder().withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS).build();
CSVWriterBuilder csvWriterBuilder = new CSVWriterBuilder(writer);
ICSVWriter csvParserWriter = csvWriterBuilder.withParser(csvParser).build();
StatefulBeanToCsv<T> beanToCsv = new StatefulBeanToCsvBuilder<T>(csvParserWriter).withThrowExceptions(false).build();
beanToCsv.write(/* some dto */)
The problem is that in the result file i have quotes for null values like htis:
"columnValue","","columnValue2"
It looks like a bug for me, or what am i missing? Thank you for help in advance.
I don't think it is a bug but instead a set of conflicting parameters. For writing the CSVParser has a applyQuotesToAll that defaults to true which gives you the output you are experiencing. If you set it to false then nothing will have quotes around it UNLESS the field value itself has quotes. So if you set it to false.
StatefulBeanToCsv<t> beanToCsv = new StatefulBeanToCsvBuilder<t>(csvParserWriter)
.withThrowExceptions(false)
.withApplyQuotesToAll(false)
.build();</t></t>
You would get
columnValue,,columnValue2
The withFieldAsNull is really for the Reader not the Writer. For the writer we are more strict on the standards and either have quotes around everything or quotes around things that require quotes (the field itself has a separator or quote character).
@sconway thank you for the reply!
What i want to achieve is to always have quotes, because 1) i have lots of fields which can contain separator (comma, in my case) and 2) i can't use escape character because of some between-team communication issues (everybody needs to agree about separator symbol - which is quite difficult in our current environment).
But while having quotes, i want to be able to distinguish null values from empty string. And the only way for this when exporting to csv file i can see is to have 2 separators in a row, without quotes, like:
"columnValue",,"columnValue3","",""column5
which would mean the second value is null and the 4th is an empty string. How can i do this?
Thanks in advance,
Dmitrii
Sorry Dmitrii - that is not supported in the writer at all at this time.
Personally, if it was that important to distinguish between null and empty and to have both in your data, then I would preprocess the object so the words null and empty would be written and hope you don't come across someone with an odd name: https://www.bbc.com/future/article/20160325-the-names-that-break-computer-systems
Though to be safe from that I would put in null_string and empty_string.
Scott :)
@sconway thank you for an explanation.
It's a pitty. It looks like a very basic feature. And moreover reader is more flexible here....
Best regards,
Dmitrii
Feel free to open it up as a feature request. If you are someone else is willing to pick it up I would be more than happy to accept the patch.
It is just that it is not a bug. The CSVReaderNullFieldIndicator was a specific request for the CSVReader. And while I don't think it would be a hard change to do looking at the tests I am pretty sure it will be not easy as well so I don't want to work a this as feature wrapped as a bug.
Scott :)
I am closing this as invalid. Like I said before feel free to put this in as a feature request but it is not a bug as it was specifically request and designed for the CSVReader not both reader and writer.