1

I have a class like this:

@JsonPropertyOrder({"Chilli"})
public interface ChilliReport {
    @JsonProperty("Chilli")
    String getChilli();

    @JsonProperty("Price")
    String getPrice();
}

// Then, I'll populate this with some values.


CsvMapper csvMapper = new CsvMapper();
CsvSchema schema = csvMapper.schemaFor(ChilliReport.class).withHeader();

String payload = csvMapper.writer(schema).writeValueAsString(reports);
System.out.println(payload);

Why is is that sometimes, the field "chilli" will be quoted while sometimes, it won't be?

Ex: Sometimes, my data looks like this:

"Chilli" "Price"

"Red",10


Other times like this: "Chilli" "Price"

Yellow,10

Btw, I changed the name of the class and actual names of the fields but that's the gist of my problem

Vicky
  • 99
  • 1
  • 1
  • 6

1 Answers1

0

If fields contain a separator, they have to be quoted.

Some writers may quote all fields, some only those with separators inside (which actually keeps the file smaller).

So, you probably have something like this:

Chilli,Price
"Red,extra",10
Yellow,10

which gives the following table:

Chilli          Price
Red,extra       10
Yellow          10

Without the quotes you would get:

Chilli          Price
Red             extra       10
Yellow          10

Or maybe there are spaces in front or at the end of the string and the writer decides to quote those.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46