Gson Json Annotations Example

Google Gson provides useful annotations which can customize the serialization/deserialization of object to/from Json. We will see common Google gson annotations including @Expose, @SerializedName, @Since, @Until, & @JsonAdapter, exploring them in detail. First thing first, update project’s pom.xml to include Gson dependency as following: Let’s start now with each annotation: 1) @Expose It is useful…

Continue reading

Gson ExclusionStrategy Example

What if you want to exclude some fields from Serialization and Deserialization? Gson provides several ways to handle this scenario. 1) Using @Expose annotation GsonBuilder’s excludeFieldsWithoutExposeAnnotation() method is a handy way to exclude certain fields from being serialized/deserialized. Just annotate the fields you want to be serialized/deserialzed with @Expose annotation. Don’t annotate the fields which…

Continue reading

Gson Custom Serialization Example

Sometimes default serialization/deserialization used by GSON is not sufficient and we need a custom behavior to be applied while serializing/deserializing objects to/from JSON. This can easily be done by writing custom JsonSerializer & JsonDeserializer implementation and hooking them with GsonBuilder. We will write a custom implementation to handle a date property in our example pojo….

Continue reading

Gson Streaming Api Example

Similar to Jackson Streaming API, Google GSON too provides streaming API for the situation where performance is utmost important. Gson Streaming API is based on Sequential read/write and is very useful in situations where it is not desirable to load complete object model in memory(e.g complete data is not available). This is known as token…

Continue reading

Gson Tree Model Example

Similar to Jackson API, Google GSON API provides a conceptual tree notion for JSON. Google Gson Tree model API provides com.google.gson.JsonParser which can be used to parse the input Json string into a tree of com.google.gson.JsonElement. On reading side, JsonElement provides isJsonXXX methods to check the type of Json element and getAsYYY() methods to get…

Continue reading

Converting JSON to/from Java Objects using Google GSON

This post explains converting Java object to/from JSON using Google GSON library. To get hold of Json Basics concepts, please refer to JSON basics. Let’s begin. com.google.gson.Gson is the main class used for converting JSON to/from java objects. This class provides toJson & fromJson methods to convert Java object to/from JSON. Gson library also provides…

Continue reading

Jackson Json Annotations Example

Previous data-binding posts gave the idea about how the POJO’s are mapped to JSON and viceversa using ObjectMapper API. This post will explain same POJO to JSON Data-binding using commonly used Jackson JSON Annotations. Basic operations read/write will be implemented exactly as before, but POJO’s are annotated this time. We will start with code and…

Continue reading

Jackson Streaming Api Example

For situations where performance is most critical, Jackson comes with Streaming API which is a high performance, sequential access api, with very low memory and processing overhead. Due to it’s incremental processing/sequential access nature, all content must be read/write in order as it arrives, so it’s bit hard to use. This API is mainly used…

Continue reading

Jackson Tree Model Example

Conceptually, JSON can be represented as a Tree (similar to DOM tree). Therefore, it is possible to traverse to individual nodes in the tree in order to access/edit their values. Jackson library provides api to do it. JsonNode refers to an individual node in tree which can be accessed using node name.ObjectMappers readTree and writeTree…

Continue reading