In this article we will compare capability of JSON binding libraries to ignore certain fields & exclude them during serialization or deserialization. JSON binding libraries compared will be Jackson vs. Gson vs. JSON-B (Eclipse Yasson)
For complete comparison of top JSON libraries & their features, visit below article.
Jackson
Jackson provides several ways of ignoring fields or properties. We can ignore properties individually or at class level. We can also ignore properties of other class which is used in instance variable of this class.
Here are the POJO classes annotated to ignore fields as explained above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties({ "middleName", "contact" }) public class PersonIgnore { private String name; private String middleName; private String lastName; @JsonIgnoreProperties({ "street" }) private AddressIgnore address; @JsonIgnore private int age; private String contact; ... getters and setters ... } |
Notive that we haven’t put any annotations on below class. But still we will ignore fields by putting annotation in PersonIgnore.java
1 2 3 4 5 6 7 |
public class AddressIgnore { private String street; private String city; ... getters and setters ... } |
Now lets see these annotations in action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonIgnore { public static void main(String[] args) throws JsonProcessingException { PersonIgnore person = new PersonIgnore(); person.setName("Jimmy"); person.setMiddleName("C"); // Ignored person.setLastName("Kimmel"); person.setAge(30); // Ignored person.setContact("1234567890"); // Ignored AddressIgnore address = new AddressIgnore(); address.setStreet("Some street"); // Ignored address.setCity("Los Angeles"); person.setAddress(address); ObjectMapper basicMapper = new ObjectMapper(); System.out.println("person = " + basicMapper.writeValueAsString(person)); } } |
1 |
person = {"name":"Jimmy","lastName":"Kimmel","address":{"city":"Los Angeles"}} |
GSON
GSON has provided a reverse approach. In GSON you can mark all the fields that you can to expose in JSON string with @Expose & rest of the fields without annotation will be ignored.
This is out of the box capability. But you can write your own custom Exclusion Strategy with your own custom annotations to achieve capabilities similar to Jackson. GSON provides way to register such Exclusion Strategy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.gson.annotations.Expose; public class PersonIgnore { @Expose private String name; private String middleName; @Expose private String lastName; @Expose private AddressIgnore address; private int age; private String contact; ... getters and setters ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.itsallbinary.json.gson; import com.google.gson.annotations.Expose; public class AddressIgnore { private String street; @Expose private String city; ... getters and setters ... } |
@Expose do not work unless you specifically tell GSON to exclude Fields Without Expose Annotation. Make sure not to miss this steps else @Expose will not be effective.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonIgnore { public static void main(String[] args) { PersonIgnore person = new PersonIgnore(); person.setName("Jimmy"); person.setMiddleName("C"); // Ignored person.setLastName("Kimmel"); person.setAge(30); // Ignored person.setContact("1234567890"); // Ignored AddressIgnore address = new AddressIgnore(); address.setStreet("Some street"); // Ignored address.setCity("Los Angeles"); person.setAddress(address); Gson nonNullGson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); System.out.println(nonNullGson.toJson(person)); } } |
1 |
{"name":"Jimmy","lastName":"Kimmel","address":{"city":"Los Angeles"}} |
JSON-B
JSONB provides annotations to ignore field which works are field or method level. JSON-B does not provide out of the box way to ignore fields at class or within other instance variable types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import javax.json.bind.annotation.JsonbTransient; public class PersonIgnore { private String name; @JsonbTransient private String middleName; private String lastName; private AddressIgnore address; @JsonbTransient private int age; @JsonbTransient private String contact; ... getters and setters ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.itsallbinary.json.jsonb; import javax.json.bind.annotation.JsonbTransient; public class AddressIgnore { @JsonbTransient private String street; private String city; ... getters and setters ... } |
Lets check out these @JsonbTransient annotations in action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.json.bind.Jsonb; import javax.json.bind.JsonbBuilder; public class JsonBIgnore { public static void main(String[] args) { PersonIgnore person = new PersonIgnore(); person.setName("Jimmy"); person.setMiddleName("C"); // Ignored person.setLastName("Kimmel"); person.setAge(30); // Ignored person.setContact("1234567890"); // Ignored AddressIgnore address = new AddressIgnore(); address.setStreet("Some street"); // Ignored address.setCity("Los Angeles"); person.setAddress(address); Jsonb basicJsonb = JsonbBuilder.create(); System.out.println(basicJsonb.toJson(person)); } } |
1 |
{"address":{"city":"Los Angeles"},"lastName":"Kimmel","name":"Jimmy"} |