Jackson vs. Gson vs. JSON-B | Ignore or exclude properties from JSON in Java

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 vs. Gson vs. JSON-B vs. JSON-P vs. org.JSON vs. Jsonpath | Java JSON libraries features comparison

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.

Notive that we haven’t put any annotations on below class. But still we will ignore fields by putting annotation in PersonIgnore.java

Now lets see these annotations in action.



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.

@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.



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.

Lets check out these @JsonbTransient annotations in action.



Leave a Reply

Your email address will not be published. Required fields are marked *