Json to Java – DeSerialization libraries

Introduction:

JSON stands for ‘Javascript object notation’ . It is a dominant standard for data exchange . JSON is replacing XML as a standard format of communication.

This post explains the libraries available for converting json to java.In other words java deserialization.

What is Deserialization?

A software ecosystem will typically have multiple applications running built on different languages/frameworks.

software systems

For example, consider an android app that talks to a .NET backend service. This service stores data in a MongoDB database which is based on java . There is another Python based application that queries MongoDB for analytics.

This is a heterogeneous system like most software ecosystems.

The good thing about this is that they all can understand JSON .

When data flows from one system to another over the network, it has to be serialized .In this case, it should be converted as JSON. Deserialization is just the opposite of that.

Deserialization is the process of converting a stream of bytes (in this case JSON) to a java object.

Libraries for json to java

There are numerous libraries available for json to pojo .Ill list down the most popular one’s here.

NameComments
GsonOpen source Library from Google.It Works with POJO’s for which you do not have source code.
Jackson-APIIt’s considered to be as best library to work with JSON. Its lightweight like GSON and gives good performance.
Flex JSONAllows both deep and shallow serialization of objects.Provides better control over the depth of serialization.
JasonBIts a low level java API.This was introduced as part of Java Specification Request 367 .Its shipped along with java 8 library.
RestFBFacebook released RestFB as part of their graph api. Its simple to use and very lightweight.
JSON-JavaProvides capability to convert JSON to XML, HTTP headers,Cookies and CDL.
Custom DeserializerThe above mentioned libraries is enough for most of our use cases.Still incase if there is a custom requirement you can always implement one using JSONB.

There are lots of such frameworks in Java for Json.Choose your package carefully.

I’ll provide examples for each framework in another post.But for now I’ll just show how to convert JSON to POJO using gson.

Code Example – json to java

In this example , lets see how to convert a json to java . Ill use Gson library. Include Gson dependency from mavan.

deserialization example

POM.xml

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

POJO class

I am having a Car class with few properties. This is the class to which the Json maps to and will be deserialized to the instance of Car class.

package com.codingbrains;

import java.util.List;

public class Car {
  private String carName;
  private String manufacturer;
  private String year;
  private List<String> Accessories;
  public String getCarName() {
    return carName;
  }

  public void setCarName(String carName) {
    this.carName = carName;
  }

  public String getManufacturer() {
    return manufacturer;
  }

  public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
  }

  public String getYear() {
    return year;
  }

  public void setYear(String year) {
    this.year = year;
  }

  public List<String> getAccessories() {
    return Accessories;
  }

  public void setAccessories(List<String> accessories) {
    Accessories = accessories;
  }


}

Car Json:

This is the car JSON which I would like to convert to a Car object.

{
  "carName": "Punto",
  "manufacturer": "Fiat",
  "year": "2017",
  "Accessories": [
    "Bluetooth connectivity",
    "Charging cables",
    "Music Stereo"
  ]
}

Json to Java conversion

I am using gson.fromJson() method to convert Car json to car object.

package com.codingbrains;

import com.google.gson.Gson;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

public class CarDeserializer {

  public static void main(String[] args) throws FileNotFoundException {
    Gson gson = new Gson();
    Reader reader = new FileReader("src/main/resources/json/punto.json");
    Car car = gson.fromJson(reader, Car.class);
    System.out.println(car.getAccessories());

  }
}

You may also like...

2 Responses

Leave a Reply

Your email address will not be published.