gpt4 book ai didi

java - Jackson CSV 映射器不适用于自定义架构

转载 作者:行者123 更新时间:2023-12-02 10:10:04 25 4
gpt4 key购买 nike

我有这个代码:

CsvSchema sema = CsvSchema.builder()
.addColumn("name")
.addColumn("year", CsvSchema.ColumnType.NUMBER)
.build().withHeader();

ObjectReader reader = new CsvMapper().readerFor(JsonNode.class).with(sema);

JsonNode o = reader.readValue(new FileInputStream(new File("/path/to/test.csv")));
System.out.println(o);

test.csv是:

test, year
1, 1

该代码应将 CSV 解析为下一种格式的 JSON:

{"name":"1","year":1}

但我的输出是:

{"name":"1","year":"1"}

问题是:Jackson 将年份解析为 String,但我在 CSV Schema 中配置了该年份为 Number 。有人知道问题出在哪里吗?

Jackson版本是2.9.8,我也在2.7.1上尝试过

最佳答案

经过几个小时的工作,我找到了适合您的解决方案。

我使用 FlexJson 来配置 json 的序列化。

    <!-- https://mvnrepository.com/artifact/net.sf.flexjson/flexjson -->
<dependency>
<groupId>net.sf.flexjson</groupId>
<artifactId>flexjson</artifactId>
<version>2.0</version>
</dependency>

它不是很漂亮,但很有效。

我希望这对您有所帮助,并且我相信您可以改进此代码

public String generateJsonFromCSV(File csvFile, File schemaJson) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

//Get CsvSchema as Map
Map<String, CsvSchema.ColumnType> map = getSchemaMapFromJson(schemaJson);
//Create CsvSchema from CsvSchemaMap
CsvSchema.Builder schemaBuilder = new CsvSchema.Builder();
map.forEach(schemaBuilder::addColumn);
CsvSchema schema = schemaBuilder.build();
//read CSV
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<?, ?>> mappingIterator = csvMapper.readerFor(Map.class).with(schema).readValues(csvFile);
//Get configured JsonSerializer from CsvSchemaMap
JSONSerializer jsonSerializer = getJsonSerializer(map);
List<Map<?, ?>> lines = mappingIterator.readAll();
//remove first line
lines.remove(0);
//serialize
return jsonSerializer.deepSerialize(lines);
}

/**
*
* @param schemaMap mapping field to ColumnType
* @return a configured JSONSerializer
*/
private JSONSerializer getJsonSerializer(Map<String, CsvSchema.ColumnType> schemaMap){
Map<CsvSchema.ColumnType, Transformer> transformerMap = new EnumMap<>(CsvSchema.ColumnType.class);
transformerMap.put(CsvSchema.ColumnType.STRING, new StringTransformer());
transformerMap.put(CsvSchema.ColumnType.NUMBER, new NumberTransformer());
JSONSerializer jsonSerializer = new JSONSerializer();
for (Map.Entry<String, CsvSchema.ColumnType> columnTypeEntry : schemaMap.entrySet()) {
jsonSerializer.transform(transformerMap.get(columnTypeEntry.getValue()),columnTypeEntry.getKey());
}
return jsonSerializer;
}

/**
/**
*
* @param file JSON CsvSchema
* @return fieldname ColumnType mapping
* @throws ClassNotFoundException
*/
private Map<String, CsvSchema.ColumnType> getSchemaMapFromJson(File file) throws ClassNotFoundException {
Map<String, String> schema = new JSONDeserializer<Map<String,String>>().deserialize(getResourceFileAsString(file.getName()));
Map<String, CsvSchema.ColumnType> result = new HashMap<>(schema.size());
for (Map.Entry<String, String> columnSchema : schema.entrySet()) {
result.put(columnSchema.getKey(), CsvSchema.ColumnType.valueOf(columnSchema.getValue().toUpperCase()));
}
return result;
}

输出将是

[{"name":"foobar","year":1986},{"name":"testtest","year":777}]

关于java - Jackson CSV 映射器不适用于自定义架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55067378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com