Java JSON Libraries
JSON stands for JavaScript Object Notation and is a popular file format used for storing and transmitting data objects. A lot of external libraries are available that enable us to work with JSON in Java.
Some of the most popular ones are Jackson, Gson, Genson, and json-io.
In this tutorial, we learn the basics of each of these libraries. We will see some examples to understand how to serialize and deserialize objects to JSON using these libraries.
We will use the following class in our serialization and deserialization examples.
class ClassA
{
private String field1;
private Double field2;
//Constructors
ClassA()
{}
ClassA(String field1, Double field2)
{
this.field1 = field1;
this.field2 = field2;
}
//getters and setters
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public Double getField2() {
return field2;
}
public void setField2(Double field2) {
this.field2 = field2;
}
}
Java Jackson
Jackson is a popular JSON processing library for Java. This library is composed of three core packages - Streaming, Databind, and Annotations. To use this into your project, add the following Maven dependency for Jackson to pom.xml file.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
Jackson provides the ObjectMapper
class to serialize objects to JSON strings and deserialize JSON strings back to the objects.
The writeValueAsString() method of this class can serialize objects to JSON strings. Similarly, the readValue() method deserializes the string back to the object. The following code demonstrates this.
public class Demo
{
public static void main(String[] args) throws IOException
{
ClassA classAObj = new ClassA("Demo", 20.0);//object
ObjectMapper objMapper = new ObjectMapper();
String jsonString = objMapper.writeValueAsString(classAObj);//Generating JSON String from object
System.out.println("String: " + jsonString);
ClassA objFromStr = objMapper.readValue(jsonString, ClassA.class);//Getting object from the JSON string
System.out.print("Object: " + objFromStr.getField1() + " " + objFromStr.getField2());
}
}
String: {"field1":"Demo","field2":20.0}
Object: Demo 20.0
Java Gson
Gson is an open-source library developed by Google. Gson is easy to use and understand. Unlike some other libraries, Gson provides extensive support for Generics in Java. The Maven dependency for Gson is shown below. You can use these into pom.xml file of the project.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
We can use the toJson() and fromJson() methods for serialization and deserialization of objects. The code below demonstrates this.
public class Demo
{
public static void main(String[] args)
{
ClassA obj = new ClassA("Demo", 20.0);
Gson gson = new Gson();
String jsonStrFromObj = gson.toJson(obj);//Object to JSON String
System.out.println("String: " + jsonStrFromObj);
ClassA objFromJsonStr = gson.fromJson(jsonStrFromObj, ClassA.class);//JSON String to Object
System.out.print("Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
}
}
String: {"field1":"Demo","field2":20.0}
Object: Demo 20.0
Java Genson
The Genson library provides simple serialize() and deserialize() methods for conJavversion between objects and JSON. Genson library also provides complete support for Generic and Polymorphic types and immutable objects that do not provide default constructors. We can use the following Maven dependency or download the Jenson jar file from the official website.
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>${genson.version}</version>
</dependency>
The code below demonstrates the working of the serialize() and deserialize() methods.
public class Demo
{
public static void main(String[] args)
{
ClassA obj = new ClassA("Demo", 20.0);
Genson genson = new Genson();
String jsonStrFromObj = genson.serialize(obj);//Serializing object to JSON String
System.out.println("String: " + jsonStrFromObj);
ClassA objFromJsonStr = genson.deserialize(jsonStrFromObj, ClassA.class);//Deserializing JSON String to Object
System.out.print("Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
}
}
String: {"field1":"Demo","field2":20.0}
Object: Demo 20.0
Java Json-io
Json-io is another popular library used for JSON processing. It contains two main classes - the JsonReader and the JsonWriter.
We can use the objectToJson() method of JsonWriter to serialize an object to JSON string. Similarly, we can use the jsonToJava() method of JsonReader class to deserialize JSON string to an object.
Note that the type information is also included in the JSON string to tell JsonReader what class to instantiate. We also need an explicit cast while deserializing.
Maven Dependency:
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
<version>${json-io.version}</version>
</dependency>
The following code demonstrates the working of JsonWriter and JsonReader.
public class Demo
{
public static void main(String[] args)
{
ClassA obj = new ClassA("Demo", 20.0);
String jsonStrFromObj = JsonWriter.objectToJson(obj);//Object to JSON String
System.out.println("String: " + jsonStrFromObj);
ClassA objFromJsonStr = (ClassA) JsonReader.jsonToJava(jsonStrFromObj);//JSON String to Object
System.out.print("Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
}
}
String: {"@type":"ClassA","field1":"Demo","field2":20.0}
Object: Demo 20.0
Summary
JSON is a widely used format for objects. There are a lot of external libraries available to process JSON in Java. In this tutorial, we learn the basics of the Jackson library, Gson library, Genson library, and Json-io library. We also saw an example of serialization and deserialization for all these libraries.