Posted In: JSON, jsoniter

Generate Java POJO from JSON or JSON schema – Annotation style – None – jsoniter

 

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 – None. Let use jsoniter library to populate this POJO

Maven for jsoniter

<dependency>
	<groupId>com.jsoniter</groupId>
	<artifactId>jsoniter</artifactId>
	<version>0.9.8</version>
</dependency>

 

1. Example will use following sample JSON

 

{
  "addresses": [
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066"
    },
    {
      "addressID": "90ace06b-2257-4760-9dec-75a4ca679d0e",
      "country": "India",
      "state": "Telengana",
      "pinCode": "400066"
    }
  ]
}

 

2. Following POJO will be generated

 

package com.example4;

public class Address {
	private String addressID;
	private String country;
	private String state;
	private String pinCode;

	public String getAddressID() {
		return addressID;
	}

	public void setAddressID(String addressID) {
		this.addressID = addressID;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getPinCode() {
		return pinCode;
	}

	public void setPinCode(String pinCode) {
		this.pinCode = pinCode;
	}
}

 

package com.example4;

import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Addresses {
	private List<Address> addresses = null;

	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}

 

3. Use jsoniter library to parse JSON and populate POJOs

 

package com.example4;

import com.jsoniter.JsonIterator;

//Generate Json Schema from POJO jsoniter (json-iterator)
public class Main8 {
	public static void main(String a[]) {
		String json = "{" + "\"addresses\": [" + "{"
		        + " \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","
		        + " \"country\": \"India\","
		        + " \"state\": \"Telengana\","
		        + " \"pinCode\": \"400066\"" + "}," + "{"
		        + "  \"addressID\": \"90ace06b-2257-4760-9dec-75a4ca679d0e\","
		        + "  \"country\": \"India\","
		        + "  \"state\": \"Telengana\","
		        + "  \"pinCode\": \"400066\"" + "}" + "]" + "}";
		try {
			Addresses addresses = JsonIterator.deserialize(json,
			        Addresses.class);
			System.out.println(addresses);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

jsoniter (json-iterator) does not throw exception if a new field gets added to JSON and POJO do not have corresponding getter/setter.
 
 

Tags: ,

by , on February 21st, 2017

  • Categories