如何告诉杰克逊在反序列化期间忽略空对象? [英] How to tell Jackson to ignore empty object during deserialization?

查看:134
本文介绍了如何告诉杰克逊在反序列化期间忽略空对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在反序列化过程中(据我所知是将JSON数据转换为Java对象的过程),如何告诉Jackson当它读取不包含数据的对象时,应该忽略它?

At the deserialization process (which as I understand is the process of converting JSON data into a Java Object), how can I tell Jackson that when it reads a object that contains no data, it should be ignored?

我正在使用Jackson 2.6.6和Spring 4.2.6

I'm using Jackson 2.6.6 and Spring 4.2.6

我的控制器收到的JSON数据如下:

The JSON data received by my controller is as follows:

{
    "id": 2,
    "description": "A description",
    "containedObject": {}
}

问题是对象containsObject 被解释为是,它正在被实例化。因此,只要我的控制器读取此JSON数据,它就会生成ContainedObject对象类型的实例,但我需要将其替换为null。

The problem is that the object "containedObject" is interpreted as is and it's being instantiated. Therefore, as soon as my controller reads this JSON data, it produces an instance of the ContainedObject object type but I need this to be null instead.

最简单,最快速的解决方案将在收到的JSON数据中,此值为null,如下所示:

The easiest and fastest solution would be that in the JSON data received, this value be null like this:

 {
        "id": 2,
        "description": "A description",
        "containedObject": null
    }

但这是不可能的,因为我无法控制发送给我的JSON数据。

But this isn't possible since I'm not in control of the JSON data that is sent to me.

是否有注释( 这里解释的这一点)适用于反序列化过程,可能是在我的情况下有帮助吗?

Is there an annotation (like this explained here) that works for the deserialization process and could be helpfull in my situation?

我留下了我的课程表示以获取更多信息:

I leave a representation of my classes for more information:

我的实体课程是如下:

public class Entity {
    private long id;
    private String description;
    private ContainedObject containedObject;

//Contructor, getters and setters omitted

}

我包含的对象类如下:

public class ContainedObject {
    private long contObjId;
    private String aString;

//Contructor, getters and setters omitted

}


推荐答案

我会使用 JsonDeserializer 。检查有问题的字段,确定是否 emtpy 并返回 null ,这样你的 ContainedObject 将为null。

I would use a JsonDeserializer. Inspect the field in question, determine, if it is emtpy and return null, so your ContainedObject would be null.

像这样的东西(半伪):

Something like this (semi-pseudo):

 public class MyDes extends JsonDeserializer<ContainedObject> {

        @Override
        public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
            //read the JsonNode and determine if it is empty JSON object
            //and if so return null

            if (node is empty....) {
                return null;
            }
            return node;
        }

    }

然后在你的模型中:

 public class Entity {
    private long id;
    private String description;

    @JsonDeserialize(using = MyDes.class)
    private ContainedObject containedObject;

   //Contructor, getters and setters omitted

 }

希望这会有所帮助!

这篇关于如何告诉杰克逊在反序列化期间忽略空对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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