Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Annotations: Difference Between JsonIgnoreProperties(ignoreUnknown=true) and JsonInclude(Include.NON_EMPTY)

I'm curious is there a difference between Jackson annotations @JsonIgnoreProperties(ignoreUnknown=true) and @JsonInclude(Include.NON_EMPTY) at the class level? Is one just a newer version of the other? Thanks!

The jackson docs state that:

ignoreUnknown Property that defines whether it is ok to just ignore any unrecognized properties during deserialization.

Is that the same as just an empty property?

like image 256
rsmets Avatar asked Aug 17 '16 20:08

rsmets


People also ask

What is JsonIgnoreProperties ignoreUnknown true?

We have just annotated a whole model class as @JsonIgnoreProperties(ignoreUnknown = true), which mean any unknown property in JSON String i.e. any property for which we don't have a corresponding field in the EBook class will be ignored.

What is JsonInclude annotation?

The Jackson @JsonInclude annotation can be used to exclude the properties or fields of a class under certain conditions and it can be defined using the JsonInclude. Include enum. The JsonInclude.

What is the difference between JsonIgnore and JsonIgnoreProperties?

@JsonIgnore is to ignore fields used at field level. while, @JsonIgnoreProperties is used at class level, used for ignoring a list of fields. Annotation can be applied both to classes and to properties.

What does @JsonProperty annotation do?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.


1 Answers

Short answer:

  1. @JsonIgnoreProperties(ignoreUnknown=true) is applicable at deserialization of JSON to Java object (POJO) only. If your POJO does not contain certain properties that JSON does contain, they are ignored and no error is thrown.
  2. On the other hand @JsonInclude(Include.NON_EMPTY) is used at serialization of POJO to JSON and it says, skip POJO properties that are:

    null or what is considered empty are not to be included. Definition of emptiness is data type-specific.

Long answer:

@JsonInclude

It is used at serialization time only. It says that if the value of a property (or all properties) in question is equal to a certain value (null, empty - whatever that means, or a default value) this property is not serialized.

Without this annotation, the property value is always serialized. The annotation helps to reduce the number of transferred properties (Property default value must be specified when it is not present on the receiving side).

Example:

public class Person {     public String firstName = "Mark";     public String middleName;     public String lastName = "Watney"; }  ObjectMapper mapper = new ObjectMapper(); Person p = new Person(); System.out.println(mapper.writeValueAsString(p)); 

Produces following output:

{"firstName":"Mark", "middleName":null, "lastName":"Watney"} 

But if Person is annotated with @JsonInclude(Include.NON_EMPTY), middleName is omitted from the output because its value is "empty" (null in this case):

@JsonInclude(Include.NON_EMPTY) public static class Person {     [....] } 

Console output is: {"firstName":"Mark", "lastName":"Watney"}

@JsonIgnoreProperties

Is used to ignore certain properties in serialization and deserialization regardless of its values:

to prevent specified fields from being serialized or deserialized (i.e. not include in JSON output; or being set even if they were included): @JsonIgnoreProperties({ "internalId", "secretKey" })

To ignore any unknown properties in JSON input without exception: @JsonIgnoreProperties(ignoreUnknown=true)

If JSON input is:

{     "firstName": "Homer",     "middleName": "Jay",     "lastName": "Simpson" } 

And the class is:

public class Person {     public String firstName;     public String lastName; } 

Deserialization mapper.readValue(json, Person.class) will produce UnrecognizedPropertyException exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "middleName" .....

Because property middleName is not part of the Person class.

But if class Person was annotated with @JsonIgnoreProperties(ignoreUnknown=true), unknown properties ( like middleName) would be ignored at deserialization into POJO.

@JsonIgnoreProperties(ignoreUnknown=true) public class person {     [...] } 

Another common use case is to suppress serialization of sensitive properties, like for example password:

@JsonIgnoreProperties("password") public static class User {     public String login = "simpsonh";     public String password = "D00nut";     public String firstName = "Homer";     public String middleName = "Jay";     public String lastName = "Simpson"; } 

Now if you serialize User class , password will be omitted from the output:

User u = new User(); System.out.println(mapper.writeValueAsString(u)); 

Console output:

{     "login":"simpsonh",     "firstName":"Homer",     "middleName":"Jay",     "lastName":"Simpson" } 
like image 141
Michal Foksa Avatar answered Sep 22 '22 17:09

Michal Foksa