Updated 10 June 2017
In this blog, we are going to tell about the google’s GSON library. How to parse JSON to Java objects and vice-versa.
Gson is a Java library that can be used to parse the Java Objects into their JSON and vice-versa. It can also be used to convert a JSON string to an equivalent Java object.
Google developed GSON for internal use but open sourced it later.
For this purpose, Gson provides several built-in serializers and deserializers.
Serializers: A serializer allows to convert a Json string to corresponding Java Model or objects.
Deserializer: A deserializer allows to convert from Java Model to a JSON representation. You can also configure Gson to use custom representations of your objects.
Gson allows serializing an Collections
of objects of the same type.
For use Gson in a Gradle build, add
1 compile 'com.google.code.gson:gson:2.6.2'or a later version as dependency to your build.gradl e build file.
There are two methods to create an Gson
instance:
new Gson()
GsonBuilder
instance and calling on it with create().
We can simply create the GSON by below method:
1 |
Gson gson = new Gson(); |
Here is an example of creating a GsonBuilder
and calling:create()
1 2 |
GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); |
Using a GsonBuilder
allows you to set configuration options on the GsonBuilder
before creating the Gson
object.
GSON can parse JSON into Java objects using the fromJson()
method of the Gson
object. Below is a GSON example of parsing JSON into a Java object or Model:
1 2 3 4 5 |
String json = "{\"brand\":\"Nice\", \"type\": \"t-shirt\"}"; //JSON string to parse Gson gson = new Gson(); // creates a Gson instance Cloth cloth = gson.fromJson(json, Cloth.class); //parses the JSON string into an Cloth object |
Below is how the Cloth
model class looks:
1 2 3 4 5 6 7 8 |
public class Cloth { @SerializedName("brand") public String brand = ""; @SerializedName("type") public String type = ""; } |
GSON can also generate JSON from Java objects. You do so via the Gson
object. To generate JSON you call the toJson()
method of the Gson
object. Here is an example of generating JSON from a Java object with GSON:
1 2 3 4 5 6 7 |
Cloth cloth = new Cloth(); cloth.brand = "adidas"; cloth.type = "trousers"; Gson gson = new Gson(); String json = gson.toJson(cloth); |
Here is an example of the compact JSON output from a default Gson
instance:
1 |
{"brand":"adidas","type":"trousers"} |
However, this compact JSON can be hard to read. Therefore GSON offers a pretty printing option where the JSON is printed so it is more readable in a text editor.
Here is an example of how to create a Gson
instance with pretty print enabled:
1 |
Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
Here is an example of how the same JSON would look pretty-printed:
1 2 3 4 |
{ "brand":"adidas", "type":"trousers" } |
Thank you for reading it, maybe it will help you to understand the Basis GSON.
Happy Coding, Stay Super and Stay Cool.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.