Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 605
Mauricio Campagner Avatar asked Nov 01 '16 18:11

Mauricio Campagner


People also ask

How do you tell Jackson to ignore a field during deserialization if its value is null?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do I ignore null values in Jackson?

In Jackson, we can use @JsonInclude(JsonInclude. Include. NON_NULL) to ignore the null fields.

How do I ignore empty values in JSON response?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL.


1 Answers

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!

like image 102
Mechkov Avatar answered Oct 05 '22 06:10

Mechkov