This test fails with: expected: <java.lang.Integer> but was: <java.lang.String>
@Test
public void testSetTypeOfColumns() throws IOException {
String data = "ID,NAME" + System.lineSeparator() + "2,Chris";
InputStream csvStream = new ByteArrayInputStream(data.getBytes());
CsvParser parser = new CsvParser(new CsvParserSettings() {
{
setHeaderExtractionEnabled(true);
}
});
parser.beginParsing(csvStream);
parser.getRecordMetadata().setTypeOfColumns(Integer.class, "ID");
parser.getRecordMetadata().setTypeOfColumns(String.class, "NAME");
Record record = parser.parseNextRecord();
Map<String, Object> map = record.toFieldObjectMap();
assertEquals(Integer.class, map.get("ID").getClass());
}
Is there something wrong in my code is this a bug?
Converts the record into a map of Object values. Conversions must be registered using RecordMetaData.convertFields(Conversion[]) or RecordMetaData.convertIndexes(Conversion[]) (Conversion[])}. Columns without a known conversion will have their values put into the map as plain Strings.
http://docs.univocity.com/parsers/2.7.2/index.html?com/univocity/parsers/common/record/Record.html
Have a look at their tutorial how to map to a simple POJO.
https://www.univocity.com/pages/univocity_parsers_tutorial#using-annotations-to-map-your-java-beans
// BeanListProcessor converts each parsed row to an instance of a given class, then stores each instance into a list.
BeanListProcessor<TestBean> rowProcessor = new BeanListProcessor<TestBean>(TestBean.class);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\n");
parserSettings.setProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(parserSettings);
parser.parse(getReader("/examples/bean_test.csv"));
// The BeanListProcessor provides a list of objects extracted from the input.
List<TestBean> beans = rowProcessor.getBeans();