Java Google Json (Gson) New in Gson 2.3

What’s new in Gson 2.3 In this article we look at the additions in Google Json 2.3. There are three main changes : @TypeAdapter annotation Support for JsonPath using JsonReader.getPath() New Methods in JsonArray we now present examples for all three @TypeAdapter Annotation TypeAdapters are used to specify rules to convert a java class to … Read more

Java Google Json (Gson) Exclusion Strategy

In this tutorial we look at how to selectively include fields from a java object to a json string. By default, Gson tries to map all fields in the java object to the corresponding property in json. However, in certain cases we may want to control that. There are a few ways to do this. … Read more

Java Google Json (Gson) Custom Serializer

Just as we saw in the previous tutorial, Gson provides way to specify custom serializers and deserializers. Register a custom serializer with the GsonBuilder if you need you own way to convert a java object to json and you a custom deserializer if you dont like Gson’s way of converting json to the java object. … Read more

Java Google Json (Gson) Type Adapter

Using Custom Type Adapter In the earlier tutorials we have seen how gson can serialize and deserialize java classes with or without hierarchies. By default, it introspects the classes and comes with with a strategy for serializing and deserializing it. However, in some cases, you want to specify your own conversion strategy. That is, you … Read more

Java Google Json (Gson) Serializing Inner classes

Serializing inner classes Gson can serialize inner classes and static nested classes. The detailed example below demonstrates the following things. Serializing class containing static nested class Serializing class containing non static nested class (Inner class) De-serializing json to a class containing static and non static inner class Serializing static nested class (without the enclosing type) … Read more

Java Google Json (Gson) Serializing Generics

Serializing list As we saw in the previous tutorial serializing and deserializing classes with generic types is non trivial since generic type information is lost while serializing. Gson provides a class called com.google.gson.reflect.TypeToken to store generic types. The example below shows how to use the TypeToken class to serialize and deserialize Classes with generic types. … Read more

Java Google Json (Gson) Serializing Collections

Serializing list Serializing Collections should have been similar to serializing other objects. However, the problem is that Collections are generic and the generic type information is not maintained in the json. We therefore pass the type while deserializing list. Note that if the Collection has different types of objects then there is no way to … Read more

Java Gson – Building json from java (Data-Binding)

Converting a java object to json Gson is quite powerful when you simply want to build json from a java object. It does not require anything fancy, just pass the java object and you are done. However, it does allow customizations. You can control what goes inside the json from the java object and you … Read more

Java Gson – Convert json to a java object tree

Converting json to a java object In the Earlier tutorial we saw how to convert json to a java object. In this tutorial, we build a tree of com.google.gson.JsonElement from the json string. The tree can then be traversed to build java objects. JsonElement has methods such as isJsonObject(), isJsonNull(), etc that can be used … Read more

Java Gson – parsing json to java token by token

Converting json to a java object In the earlier tutorials we saw how to convert json to a java object. In This tutorial we see how to parse json and obtain individual tokens. Although this may seem like a cumbersome way to build java object from json, however it is extremely powerful and may be … Read more