Posted In: GSON, JSON

Generate Java POJO from JSON or JSON schema – Annotation style – GSON

 

We created POJOs from JSON in previous example generate-java-pojo-from-json-or-json-schema. Let us go through some options that we have on http://www.jsonschema2pojo.org/


 

Annotation style – GSON

1. Example will use following sample JSON

 

{
  "addresses": [
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "creationDateTime": "2014-08-19T22:04:56.814-07:00",
      "lastUpdDateTime": "2014-08-19T22:04:56.814-07:00",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066",
      "streetName": "street",
      "buildingName": "my buliding",
      "flatNumber": "102"
    },
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "creationDateTime": "2014-08-19T22:04:56.814-07:00",
      "lastUpdDateTime": "2014-08-19T22:04:56.814-07:00",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066",
      "streetName": "street",
      "buildingName": "my buliding",
      "flatNumber": "102"
    }
  ]
}

 

2. Following POJO will be generated

 


package com.example2;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Address {
	@SerializedName("addressID")
	@Expose
	private String addressID;
	@SerializedName("creationDateTime")
	@Expose
	private String creationDateTime;
	@SerializedName("lastUpdDateTime")
	@Expose
	private String lastUpdDateTime;
	@SerializedName("country")
	@Expose
	private String country;
	@SerializedName("state")
	@Expose
	private String state;
	@SerializedName("pinCode")
	@Expose
	private String pinCode;
	@SerializedName("streetName")
	@Expose
	private String streetName;
	@SerializedName("buildingName")
	@Expose
	private String buildingName;
	@SerializedName("flatNumber")
	@Expose
	private String flatNumber;

	public String getAddressID() {
		return addressID;
	}
//...................................
}

 

3. Use Gson library to parse JSON and populate POJOs

 

package com.example2;

import java.io.Reader;
import java.io.StringReader;

import com.example.Addresses;
import com.google.gson.Gson;

public class Main4 {
	public static void main(String a[]) {
		String json = "{" + "\"addresses\": [" + "{"
		        + " \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","
		        + " \"creationDateTime\": \"2014-08-19T22:04:56.814-07:00\","
		        + " \"lastUpdDateTime\": \"2014-08-19T22:04:56.814-07:00\","
		        + " \"country\": \"India\","
		        + " \"state\": \"Telengana\","
		        + " \"pinCode\": \"400066\","
		        + " \"streetName\": \"street\","
		        + " \"buildingName\": \"my buliding\","
		        + " \"flatNumber\": \"102\"" + "}," + "{"
		        + "  \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","
		        + "  \"creationDateTime\": \"2014-08-19T22:04:56.814-07:00\","
		        + "  \"lastUpdDateTime\": \"2014-08-19T22:04:56.814-07:00\","
		        + "  \"country\": \"India\","
		        + "  \"state\": \"Telengana\","
		        + "  \"pinCode\": \"400066\","
		        + "  \"streetName\": \"street\","
		        + "  \"buildingName\": \"my buliding\","
		        + "  \"flatNumber\": \"102\"" + "}" + "]" + "}";
		try {
			Gson gson = new Gson();
			Reader reader = new StringReader(json);
			Addresses addresses = gson.fromJson(reader,
			        Addresses.class);
			System.out.println(addresses);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

Gson does not throw exception if a new field gets added to JSON and POJO do not have corresponding getter/setter.
 
Advantage of using these Gson annotations like @SerializedName is you could change JSON key to different POJO field. For example you could do following
	@SerializedName("streetNameNew")
	@Expose
	private String streetName;

 

@Expose annotation. This annotation should be removed for fields which has sensitive information and you do not want to be part of your JSON response. For example you do not want to send password, SSN in your JSON. In that case remove @Expose for those fields. Use GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() then the toJson() and fromJson() methods of Gson will exclude the password, ssn field.
	@SerializedName("userId")
	@Expose
	private String userId;
	@SerializedName("password")
	private String password;
	@SerializedName("ssn")
	private String ssn;

 
 

Tags: ,

by , on February 20th, 2017

  • Categories