杰克逊用空对象反序列化数组 [英] Jackson deserialize array with empty object

查看:139
本文介绍了杰克逊用空对象反序列化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON:

{"beans":["{}",{"name":"Will"}]}

以及相应的POJO类:

and the corresponding POJO classes:

public class BeanA {
    private BeanB[] beans;
    ...getters/setters
}

public class BeanB {
    private String name;
    ...getters/setters
}

我希望杰克逊使用BeanB数组反序列化到BeanA,第一个元素只是BeanB的一个实例,第二个元素是具有name属性集的BeanB的一个实例.

I would like jackson to deserialize to BeanA with an array of BeanBs, the first element would be just an instance of BeanB and the second with be an instance of BeanB with the name property set.

我通过序列化创建了原始字符串:

I've created the original string by serializing this:

BeanA beanA = new BeanA();
BeanB beanB = new BeanB();
beanB.setName("Will");
beanA.setBeans(new BeanB[] {new BeanB(), beanB});

这是我对objectMapper的完整配置:

Here's my full configuration for objectMapper:

this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

我得到的错误是:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `BeanB` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{}')

推荐答案

使用

这篇关于杰克逊用空对象反序列化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆