Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - How to tell Jackson to ignore empty object during deserialization?

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?

I'm using Jackson 2.6.6 and Spring 4.2.6

The JSON data received by my controller is as follows:

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

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.

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
    }

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:

My entity class is as follows:

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

//Contructor, getters and setters omitted

}

And my contained object class as follows:

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

//Contructor, getters and setters omitted

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }

    }

then in your model:

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

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

   //Contructor, getters and setters omitted

 }

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...