This page will walk through Jackson
1. Java 9
2. Jackson 2.9.4
3. Gradle 4.3.1
4. Eclipse Oxygen
Using field:
Using getter method:
Using setter method:
In all the above cases the logical property is
In the above code snippet, logical property is
In the above code we have made
The logical properties
In the above code, the logical properties
In the above code, the logical properties
In the above class we have
In the above JSON fields,
Now suppose
The logical property
In the above code we have made
Address.java
Book.java
Writer.java
Run the code for serialization.
ObjectToJSON.java
Output
If we use neither
Now run the code for deserialization.
JSONToObject.java
Output
@JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType annotations example. These annotations are used to ignore logical properties in JSON serialization and deserialization. @JsonIgnore is annotated at a class property level to ignore it. @JsonIgnoreProperties is annotated at class level and we need to specify the logical properties of that class to ignore them. @JsonIgnoreType is annotated at class level and it ignores the complete class. @JsonIgnore and @JsonIgnoreType has an element value which accepts Boolean values to make it active and inactive. @JsonIgnoreProperties has elements that are allowGetters, allowSetters, ignoreUnknown and value. The element value in @JsonIgnoreProperties specifies the names of properties to ignore. On this page we will provide complete example to use @JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType in JSON serialization and deserialization step by step.Contents
- 1. Technologies Used
- 2. @JsonIgnore
- 3. @JsonIgnoreProperties
- 3.1. @JsonIgnoreProperties allowGetters
- 3.2. @JsonIgnoreProperties allowSetters
- 3.3. @JsonIgnoreProperties ignoreUnknown
- 4. @JsonIgnoreType
- 5. Complete Example
1. Technologies Used
Find the technologies being used in our example.1. Java 9
2. Jackson 2.9.4
3. Gradle 4.3.1
4. Eclipse Oxygen
2. @JsonIgnore
@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setter, getter or field. It is used as following. Using field:
category. In deserialization, application will throw exception as Unrecognized field "category". In serialization there will be no category field in JSON output. @JsonIgnore can be used with @JsonProperty as given below.bookCategory. As we are using @JsonIgnore, so during JSON serialization and deserialization the logical property bookCategory will not be available. @JsonIgnore has element value which is optional. Using Boolean we can make it active and inactive. We can use it as following.@JsonIgnore inactive by passing false value. Now the logical property categorywill be available in JSON serialization and deserialization.3. @JsonIgnoreProperties
@JsonIgnoreProperties ignores the specified logical properties in JSON serialization and deserialization. It is annotated at class level. Find the code snippet.bookName and bookCategory has been specified in @JsonIgnoreProperties annotation. So these logical properties will not take part in JSON serialization and deserialization. If other logical properties such as bookId has been annotated with @JsonIgnore then all these logical properties will be ignored in JSON serialization and deserialization. It means the union of logical properties ignored by @JsonIgnore and @JsonIgnoreProperties are considered to be ignored in JSON serialization and deserialization. @JsonIgnoreProperties has elements that are allowGetters, allowSetters, ignoreUnknown and value. The element value specifies name of properties to ignore.3.1. @JsonIgnoreProperties allowGetters
When we passtrue to allowGetters element, the getters will be allowed for the specified logical properties. It means the specified logical properties in @JsonIgnoreProperties will take part in JSON serialization but not in deserialization.bookName and bookCategory will take part in JSON serialization but not in deserialization.3.2. @JsonIgnoreProperties allowSetters
When we passtrue to allowSetters element, the setters will be allowed for the specified logical properties. It means the specified logical properties in @JsonIgnoreProperties will take part in JSON deserialization but not in serialization.bookName and bookCategory will take part in JSON deserialization but not in serialization.3.3. @JsonIgnoreProperties ignoreUnknown
When we passtrue to ignoreUnknown element, then in deserialization if JSON data has a field for which there is no logical property then that JSON field will be ignored and no error will be thrown. It can be used as following.bookId, bookName and bookCategory logical properties. Suppose we have a JSON data with some unknown fields.pubYear and price has no corresponding logical properties in Book class. In deserialization, we will not get exception because we are using ignoreUnknown = true in @JsonIgnorePropertiesannotation.4. @JsonIgnoreType
@JsonIgnoreType can be annotated at class level. It ignores all logical properties of annotated class in JSON serialization and deserialization. It is used as following.Address is being used in Writer class.writerAddress will be ignored in serialization and deserialization of Writer class. @JsonIgnoreType has element value which is optional. Using Boolean we can make it active and inactive. We can use it as following.@JsonIgnoreType inactive by passing false value. Now the Address class will be available in JSON serialization and deserialization.5. Complete Example
build.gradleObjectToJSON.java
@JsonIgnore nor @JsonIgnoreProperties nor @JsonIgnoreType in the above example then the output will be as given below.JSONToObject.java
No comments:
Post a Comment