jackson-dataformat-csv: Mapping number value without POJO

Igor Luzhanov :

I'm trying to parse a CSV file using jackson-dataformat-csv and I want to map the numeric column to the Number java type.

CsvSchema schema = CsvSchema.builder().setUseHeader(true)
    .addColumn("firstName", CsvSchema.ColumnType.STRING)
    .addColumn("lastName", CsvSchema.ColumnType.STRING)
    .addColumn("age", CsvSchema.ColumnType.NUMBER)
    .build();

CsvMapper csvMapper = new CsvMapper();  

MappingIterator<Map<String, Object>> mappingIterator = csvMapper
        .readerFor(Map.class)
        .with(schema)
        .readValues(is);        

while (mappingIterator.hasNext()) {
    Map<String, Object> entryMap = mappingIterator.next();
    Number age = (Number) entryMap.get("age");
}       

I'm expecting entryMap.get("age") should be a Number, but I get String instead.

My CSV file:

firstName,lastName,age
John,Doe,21
Error,Name,-10

I know that CsvSchema works fine with POJOs, but I need to process arbitrary CSV schemas, so I can't create a new java class for every case.

Any way to parse CSV into a typed Map or Array?

Jeronimo Backes :

You can use univocity-parsers for this sort of thing. It's faster and way more flexible:

var settings = new CsvParserSettings(); //configure the parser if needed
var parser = new CsvParser(settings);

for (Record record : parser.iterateRecords(is)) {
    Short age = record.getShort("age");
}

To get a typed map, tell the parser what is the type of the columns you are working with:

parser.getRecordMetadata().setTypeOfColumns(Short.class, "age" /*, and other column names*/);

//to get 0 instead of nulls when the field is empty in the file:
parser.getRecordMetadata().setDefaultValueOfColumns("0", "age", /*, and other column names*/);

// then parse
for (Record record : parser.iterateRecords(is)) {
    Map<String,Object> map = record.toFieldMap();
}

Hope this helps

Disclaimer: I'm the author of this library. It's open source and free (Apache 2.0 license)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101128&siteId=1