Java object to json

This example will show how to convert a java object to json using GSON and jackson. JSON is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. In the set up we will initialize a list of Planes that each snippet will use to serialize to json.

Deserializing json to a java object will allow you to convert back to json if needed.

Setup

class Plane {
    private String planeType;
    
    public Plane(String planeType) {
        super();
        this.planeType = planeType;
    }

    public String getPlaneType() {
        return planeType;
    }
    
    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("plane type: ", planeType).toString();
    }
    
}

private List<Plane> planes = Lists.newArrayList();
private Plane seaPlane; 

@Before
public void seedData () {
    planes.add(new Plane("Commercial Transport Planes"));
    planes.add(new Plane("General Aviation Planes"));
    planes.add(new Plane("Military Planes"));
    planes.add(new Plane("Sea Planes"));
    planes.add(new Plane("Special Purpose Planes"));
    
    seaPlane = new Plane("Sea Planes");
}

GSON

Using gson, this snippet will convert a java object to json.

Object

@Test
public void marshall_java_object_to_json_with_gson () {
    Gson gson = new Gson();
    String json = gson.toJson(seaPlane);

    logger.info(json);
    
    assertEquals(JSON_OBJECT, json);
}

Output

{
   "planeType":"Sea Planes"
}

List

This snippet will convert java list to json array using GSON.

@Test
public void marshall_java_list_to_json_with_gson () {
    Gson gson = new Gson();
    String json = gson.toJson(planes);
    
    logger.info(json);
    
    assertEquals(JSON_ARRAY, json);
}

Output

[
   {
      "planeType":"Commercial Transport Planes"
   },
   {
      "planeType":"General Aviation Planes"
   },
   {
      "planeType":"Military Planes"
   },
   {
      "planeType":"Sea Planes"
   },
   {
      "planeType":"Special Purpose Planes"
   }
]

Jackson

This snippet will convert an object to json using jackson by calling ObjectMapper.writeValueAsString which will serialize the java object as a JSON string.

Object

@Test
public void marshall_java_object_to_json_with_jackson () throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(seaPlane);

    logger.info(json);
    
    assertEquals(JSON_OBJECT, json);
}

Output

{
   "planeType":"Sea Planes"
}

List

This snippet will convert an ArrayList to JSON using Jackson.

@Test
public void marshall_java_list_to_json_with_jackson () throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(planes);
    
    logger.info(json);
    
    assertEquals(JSON_ARRAY, json);
}

Output

[
   {
      "planeType":"Commercial Transport Planes"
   },
   {
      "planeType":"General Aviation Planes"
   },
   {
      "planeType":"Military Planes"
   },
   {
      "planeType":"Sea Planes"
   },
   {
      "planeType":"Special Purpose Planes"
   }
]