简体   繁体   中英

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

}

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!

You can implement a custom deserializer as follows:

public class Entity {
    private long id;
    private String description;
    @JsonDeserialize(using = EmptyToNullObject.class)
    private ContainedObject containedObject;

//Contructor, getters and setters omitted

}


public class EmptyToNullObject extends JsonDeserializer<ContainedObject> {

  public ContainedObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    JsonNode node = jp.getCodec().readTree(jp);
    long contObjId = (Long) ((LongNode) node.get("contObjId")).numberValue();
    String aString = node.get("aString").asText();
    if(aString.equals("") && contObjId == 0L) {
      return null;
    } else {
      return new ContainedObject(contObjId, aString);
    }

  }
}

Approach 1 : This is mostly used. @JsonInclude is used to exclude properties with empty/null/default values.Use @JsonInclude(JsonInclude.Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_EMPTY) as per your requirement.

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Employee {

        private String empId;
        private String firstName;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String lastName;
        private String address;
        private String emailId;

   }

More info about the jackson annotations : https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

Approach 2 : GSON

use GSON ( https://code.google.com/p/google-gson/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM