Java Json Libraries

JavaJavaBeginner
Practice Now

Introduction

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is easy to read and write and widely used for data exchange between client-server. In this lab, you will learn how to use popular Java JSON libraries including Jackson, Gson, Genson, and Json-io to serialize and deserialize objects to JSON format.

Setting Up the Project

  1. Open up the terminal and navigate to the project directory by running cd ~/project.
  2. Create a new Java file for this lab by running touch JsonLab.java command in the terminal and paste the following code:
import java.io.IOException;

public class JsonLab {
    public static void main(String[] args) throws IOException {
        ClassA classAObj = new ClassA("Demo", 20.0); // create object to serialize and deserialize later
    }

    // add ClassA class here
}

Using Jackson Library

  1. To work with Jackson, we need to add the Jackson Maven dependency in pom.xml file:
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
</dependencies>
  1. Next, we need to serialize and deserialize objects to JSON string using ObjectMapper class as follows:
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonLab {
  public static void main(String[] args) throws IOException {
    ObjectMapper objMapper = new ObjectMapper();
    ClassA classAObj = new ClassA("Demo", 20.0);

    // Serialization
    String jsonString = objMapper.writeValueAsString(classAObj);
    System.out.println("Serialized String: " + jsonString);

    // Deserialization
    ClassA objFromStr = objMapper.readValue(jsonString, ClassA.class);
    System.out.print("Deserialized Object: " + objFromStr.getField1() + " " + objFromStr.getField2());
  }
}

Using Gson Library

  1. To work with Gson, we need to add the Gson Maven dependency in pom.xml file:
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>
</dependencies>
  1. Next, we need to serialize and deserialize objects using Gson object as follows:
import com.google.gson.Gson;

public class JsonLab {
  public static void main(String[] args) throws IOException {
    Gson gson = new Gson();
    ClassA obj = new ClassA("Demo", 20.0);

    // Serialization
    String jsonStrFromObj = gson.toJson(obj);
    System.out.println("Serialized String: " + jsonStrFromObj);

    // Deserialization
    ClassA objFromJsonStr = gson.fromJson(jsonStrFromObj, ClassA.class);
    System.out.print("Deserialized Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
  }
}

Using Genson Library

  1. To work with Genson, we need to add the Genson Maven dependency in pom.xml file:
<dependencies>
    <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>${genson.version}</version>
    </dependency>
</dependencies>
  1. Next, we need to serialize and deserialize objects using Genson object as follows:
import com.owlike.genson.Genson;

public class JsonLab {
  public static void main(String[] args) throws IOException {
    Genson genson = new Genson();
    ClassA obj = new ClassA("Demo", 20.0);

    // Serialization
    String jsonStrFromObj = genson.serialize(obj);
    System.out.println("Serialized String: " + jsonStrFromObj);

    // Deserialization
    ClassA objFromJsonStr = genson.deserialize(jsonStrFromObj, ClassA.class);
    System.out.print("Deserialized Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
  }
}

Using Json-io Library

  1. To work with Json-io library, we need to add the Json-io Maven dependency in pom.xml file:
<dependencies>
  <dependency>
    <groupId>com.cedarsoftware</groupId>
    <artifactId>json-io</artifactId>
    <version>${json-io.version}</version>
  </dependency>
</dependencies>
  1. Next, we need to serialize and deserialize objects using JsonWriter and JsonReader as follows:
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;

public class JsonLab {
  public static void main(String[] args) throws IOException {
    ClassA obj = new ClassA("Demo", 20.0);

    // Serialization
    String jsonStrFromObj = JsonWriter.objectToJson(obj);
    System.out.println("Serialized String: " + jsonStrFromObj);

    // Deserialization
    ClassA objFromJsonStr = (ClassA) JsonReader.jsonToJava(jsonStrFromObj);
    System.out.print("Deserialized Object: " + objFromJsonStr.getField1() + " " + objFromJsonStr.getField2());
  }
}

Summary

In this lab, you learn how to use various Java JSON libraries to convert objects to JSON format and vice versa. We learned how to use Jackson, Gson, Genson, and Json-io libraries to serialize and deserialize objects to JSON.

Other Java Tutorials you may like