What API should I use to convert Java object to CSV. Can I use google-gson for java object conversion to CSV format?
This is really more of an addition to the above post about using Jackson CSV. One issue I ran into is that it would raise any error on any properties in the object that were not in the CSV schema when I was doing it programatically and not using any annotations. Here is some example code that may be helpful. If you want to know more about why the filter and annotation introspector is necessary see here.
public class CsvWriter {
private final static String CSV_FILTER_NAME = "csvFilter";
public void writeObjects( OutputStream outputStream,
List> objects,
CsvSchema csvSchema ) throws IOException
{
HashSet columnNames = new HashSet();
for (CsvSchema.Column column : csvSchema) {
columnNames.add( column.getName() );
}
SimpleBeanPropertyFilter csvReponseFilter =
new SimpleBeanPropertyFilter.FilterExceptFilter(columnNames);
FilterProvider filterProvider = new SimpleFilterProvider().addFilter( CSV_FILTER_NAME, csvReponseFilter );
CsvMapper csvMapper = new CsvMapper();
csvMapper.setFilters( filterProvider );
csvMapper.setAnnotationIntrospector( new CsvAnnotationIntrospector() );
ObjectWriter objectWriter = csvMapper.writer(csvSchema);
objectWriter.writeValue( outputStream, objects);
}
private class CsvAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
public Object findFilterId(Annotated a) {
return CSV_FILTER_NAME;
}
}
}
and here is an example of how it can be used:
public class CsvWriterTest extends TestCase {
public void testWriteObjects() throws Exception {
Vector entities = new Vector();
entities.add( new Entity("Test entity 1", "Test description 1", "Test unused field"));
entities.add(new Entity("Test entity 2", "Test description 2", "Test unused field"));
CsvSchema csvSchema = CsvSchema.builder()
.addColumn("name")
.addColumn("description")
.setUseHeader( true )
.build()
.withLineSeparator("\r\n");
CsvWriter csvWriter = new CsvWriter();
csvWriter.writeObjects(System.out, entities, csvSchema);
}
public class Entity {
private String name;
private String description;
private String unusedField;
// constructor, getter and setter methods omitted for brevity
}
}
Output from the test method is:
name,description
"Test entity 1","Test description 1"
"Test entity 2","Test description 2"