You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 22, 2019. It is now read-only.
If a schema is setup for a CSV writer and headers are setup to be used, the headers should be written when the writer is closed even if no data rows have been written.
Currently (unless I am using the library incorrectly), the header line is not written out unless there is at least one data row written out. I am not sure if this is relevant here or in another issue tracker such as databind.
My test code that I have been using to verify is:
List<String> headers = Arrays.asList("TestHeader1", "TestHeader2");
List<List<String>> dataSource = Arrays.asList();
// Or alternatively,// List<List<String>> dataSource = Arrays.asList(Arrays.asList("TestValue1", "TestValue2"));java.io.Writerwriter = newStringWriter();
CsvSchema.Builderbuilder = CsvSchema.builder();
for (StringnextHeader : headers) {
builder = builder.addColumn(nextHeader);
}
CsvSchemaschema = builder.setUseHeader(true).build();
try(SequenceWritercsvWriter = newCsvMapper().writerWithDefaultPrettyPrinter().with(schema).forType(List.class).writeValues(writer);) {
for(List<String> nextRow : dataSource) {
csvWriter.write(nextRow);
}
// Check to see whether dataSource is empty // and if so write a single empty list to trigger header outputif(dataSource.isEmpty()) {
csvWriter.write(Arrays.asList());
}
}
System.out.println(writer.toString());
If dataSource contains a single row, there are two lines sent to the Writer, but if it contains zero rows (as happens sometimes), I would like one line to be sent to the writer. One way as shown above is to send an empty List if we know that nothing was sent, but it would be simpler to use if that check were done internally by Jackson if it fits within the architecture.