double quote on format give me 2 double quote
Brought to you by:
aruckerjones,
sconway
Hello, mi name is Juan Carlos, and I think that I found a small bug in the format attribute in the @CsvBindByName when I write a CSV file.
When I write this anotation in my JAVA class: @CsvBindByName (column = "nombre", format="\"%s\n")
The output is: ""bill""
If I choose another chart like single quote: @CsvBindByName (column = "nombre", format="'%s'")
The output is: "'bill'"
I think that is a bug.
Please forgive if is mistake from me.
I´m using your library to generate CSV, but I need the format feature because I need double quotes on string, and no quotes on number.
Regards,
Juan Carlos
Thanks in advance
If I can reproduce the behavior you're describing, it's definitely a bug. I will take a look at it as soon as I can.
Wait a minute: Let me get really straight on what behavior you're expecting. In your first example you have \"%s\n. That is, no closing quote. Yet one is added by the code, you say. What exactly would you expect the output to be?
In the second example you have '%s'. The output you say you get from that looks exactly right to me. Are you expecting something else.
In the first example only write one double quote to show you the problem.
If I use this format: format="\"%s\""
I have this out: ""bill""
Thanks again
Okay, now you've lost me. That's expected behavior. Perhaps it would be easiest if you could submit a failing test case.
I´ll write a small example:
Imagine this class:
public class Subvencion
{
@CsvBindByName (column = "nombre", format="\"%s\"")
private String nombre;
@CsvBindByName
private double importe;
}
And I have three objects:
1. nombre="gasto1", importe=1000
2. nombre="gasto2", importe=200
3. nombre="gasto3", importe=400
When I Write to a CSV I hope:
"gasto1",1000
"gasto2",200
"gasto3",400
But I have
""gasto1"",1000
""gasto2"",200
""gasto3"",400
Please apologize me for the delay.
Last edit: Juan Carlos Ballesteros 2018-12-19
Next time please try and send a failing JUnit test because the above example does not show us how you set up the BeanToCSV or any of the classes it used.
Making alot of assumptions I was able to recreate your situation and it is not a bug.
First I assumed that you are using the formats so you can control what gets quotes around it and what does not.
As such I wrote the following test and it did indeed fail.
But wait! If I said it was not a bug why does my test fail? The reason being is that passing a standard writer into the StatefulBeanToCsvBuilder a CSVWriter is created to handle all the writing. In the CSVWriter class the ESCAPE character is a double quote. So because the format added a double quote to the string even though we said there is no quote characters the code detects the double quote as an escape character and therefore prepends an escape character to it. Thus you get your double double quotes.
There are a couple of ways around this. The first is to create an CSVParserWriter and pass in a RFC4180Parser. But the easiest way is just tell the builder you do not have an escape character.
Which is what I did and my test now passes.
If this does not fix the situation please copy my unit test and modify it to match the situation you are having. It really makes it alot easier to diagnose the problem.
Scott :)
Last edit: Scott Conway 2018-12-20
Now works, I have the expected CSV.
Thanks a lot.
Was able to get expected results by configuration.