GSON – Java JSON Library

google-gson is Java library to convert Java object to JSON and JSON to Java object.

gson-java-json

Objective:

  • How to use gson library to convert Java to JSON and JSON to Java?

Environment & Tools:

  • Eclipse
  • Maven

Library:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.4</version>
</dependency>

( 1 ) Simple Java “POJO”/JSON

package com.hmkcode.vo;

import java.util.LinkedList;
import java.util.List;

public class Article {

	private String title;
	private String url;
	private List<String> categories;
	private List<String> tags;
	
//getters & setters...
	
}

Convert Java to JSON & JSON to Java

package com.hmkcode;

import com.google.gson.Gson;
import com.hmkcode.vo.Article;


public class App 
{
    public static void main( String[] args )
    {
    	Gson gson = new Gson();
    	// Java --> JSON
    	String json = gson.toJson(createArticle());
    	System.out.println("toJson: "+json);
    	
    	// JSON --> Java
    	Article article = gson.fromJson(json, Article.class);
    	System.out.println("fromJson: "+article);
    	
    	

    }
    private static Article createArticle(){
    	
    	Article article = new Article();
    	
    	article.setTitle("GSON - Java JSON Library");
    	article.setUrl("http://hmkcode.com/gson-json-java");
    	article.addCategory("Java");
    	article.addTag("Java");
    	article.addTag("GSON");
    	article.addTag("JSON");
    	
    	return article;
    }
}

( 2 ) Dealing with Collection<E>

If we have a list of articles List<Artricle>, then converting this list to json is similar to the simple conversion we have seen above
.

Gson gson = new Gson();
    	
List<Article> articles = new LinkedList<Article>();
    	
articles.add(createArticle());
articles.add(createArticle());
    	
// Java --> JSON
String json = gson.toJson(articles);

However, when converting this list back “from JSON to Java” we need to be a little bit careful. Converting JSON to Java using the simple way, will work but it will not give us List<Article> it will give us List<Map>.

// JSON --> Java
List list = gson.fromJson(json, List.class);
System.out.println("Class Type: "+list.get(0).getClass());

Output:

Class Type: class com.google.gson.internal.LinkedTreeMap

To get List<Article> we need to do one more step.

// JSON --> Java "Get the actual type"
Type type = new TypeToken<List<Article>>(){}.getType();
List list = gson.fromJson(json, type);
System.out.println("Class Type: "+list.get(0).getClass());

Output:

Class Type: class com.hmkcode.vo.Article

Complete Code:

package com.hmkcode;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmkcode.vo.Article;


public class App 
{
    public static void main( String[] args )
    {
    	Gson gson = new Gson();
    	
    	List<Article> articles = new LinkedList<Article>();
    	
    	articles.add(createArticle());
    	articles.add(createArticle());
    	
    	// Java --> JSON
    	String json = gson.toJson(articles);
    	System.out.println("toJson: "+json);
    	
    	
    	// JSON --> Java
    	List list = gson.fromJson(json, List.class);
    	System.out.println("fromJson: "+list);
    	System.out.println("Class Type: "+list.get(0).getClass());

    	// JSON --> Java "Get the actual type"
    	Type type = new TypeToken<List<Article>>(){}.getType();
    	list = gson.fromJson(json, type);
    	System.out.println("fromJson: "+list);
    	System.out.println("Class Type: "+list.get(0).getClass());


    }
    private static Article createArticle(){
    	
    	Article article = new Article();
    	
    	article.setTitle("GSON - Java JSON Library");
    	article.setUrl("http://hmkcode.com/gson-json-java");
    	article.addCategory("Java");
    	article.addTag("Java");
    	article.addTag("GSON");
    	article.addTag("JSON");
    	
    	return article;
    }
}

Source Code @ GitHub