如何使用 Immutables 反序列化 CSV 文件中的 JSON?

问题描述

我想反序列化 CSV 文件中的 JSON。

Sample CSV:
name;interests
baberiba;{"programming":"java"}

CsvMapper 示例:

public static <T> List<T> loadCsvList(Class<T> type,byte[] fileBytes) {
    try {
        CsvSchema bootstrapSchema = CsvSchema.emptySchema().withColumnSeparator(';').withHeader();

        CsvMapper mapper = new CsvMapper();
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNowN_PROPERTIES);
        mapper.enable(JsonGenerator.Feature.IGnorE_UNKNowN);
        mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
        MappingIterator<T> readValues = mapper.readerFor(type).with(bootstrapSchema).readValues(fileBytes);

        return readValues.readAll();
    } catch (ValueInstantiationException error) {
        throw new NotValidFlatFileFormatException("The CSV format is not valid",error);
    } catch (Exception e) {
        throw new BadRequestException("The CSV is not valid",e);
    }
}

课程:

@Value.Immutable
@JsonSerialize(as = ImmutablePerson.class)
@JsonDeserialize(builder = ImmutablePerson.Builder.class)
@JsonIgnoreProperties(ignoreUnkNown = true)
@Value.Style(passAnnotations = {JsonProperty.class})
public abstract class Person {
    @JsonProperty(value = "name")
    public abstract String getName();
    @JsonProperty(value = "interests")
    public abstract Interests getInterests();
}

@Value.Immutable
@JsonSerialize(as = ImmutableInterests.class)
@JsonDeserialize(builder = ImmutableInterests.Builder.class)
@JsonIgnoreProperties(ignoreUnkNown = true)
@Value.Style(passAnnotations = {JsonProperty.class})
public abstract class Interests {
    @JsonProperty(value = "programming")
    public abstract String getProgramming();
}

在运行我的 CSV 文件解析时,我得到: 无法构造 com.baberiba.test.ImmutablePerson$Builder 的实例(尽管至少存在一个 Creator):没有从字符串值反序列化的字符串参数构造函数/工厂方法 ('{"programming":"java"}')

我希望映射发生在 CsvMapper 中,而不是发生在任何其他层中。 工作:将兴趣解析为 String 而不是 Interest.class 并执行 JSONparser 以接收对象。

如何在没有 JSONparser 的情况下自定义 CsvMapper 以处理 CSV 中的 JSON?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...