Gson Json Annotations Example

Google Gson provides useful annotations which can customize the serialization/deserialization of object to/from JSON. We will see commonly used 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…

Continue reading

Gson ExclusionStrategy Example

Lets learn Gson ExclusionStrategy and how it can help us.What if you want to exclude some fields from Serialization and Deserialization? Gson provides several ways to handle this scenario.Let’s explore then one-by-one. 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…

Continue reading

Gson Custom Serialization Example

Sometimes default serialization/deserialization used by GSON is not sufficient enough 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…

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 commonly known as…

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 get going. 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…

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 demonstrate same POJO to JSON Data-binding example using commonly used Jackson JSON Annotations. Basic operations read/write will be implemented exactly as before, but POJO’s are annotated this time. Let’s get going. We will…

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 footprints 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…

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

Converting JSON to/from Java Maps using JACKSON API

Since JSON format is essentially a key-value pair grouping, JSON can easily be converted to/from Java maps. This post is about JSON to MAP conversion. Let’s get going. Step 1: Include JACKSON dependency in pom.xml There are several jar in JACKSON library. For this example, we are only using databind. Step 2: Convert Java Map…

Continue reading