univocity csv 解析器 setTypeOfColumns 不工作
univocity csv parser setTypeOfColumns not working
此测试失败:预期: 但实际是:
@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());
}
我的代码有什么问题吗?这是一个错误吗?
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
看看他们的教程如何映射到一个简单的 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();
此测试失败:预期:
@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());
}
我的代码有什么问题吗?这是一个错误吗?
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
看看他们的教程如何映射到一个简单的 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();