CREATING AND PARSING JSON




This example explains how to write a JSON Decode and Encode using Jackson. Following are the main components of this example.


Step 1: Create Maven project

   Following pom.xml defines the dependencies for this example.
             	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.smoothexample.json</groupId> <artifactId>jackson_parser</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jackson_parser</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <com.fasterxml.jackson.version>2.3.3</com.fasterxml.jackson.version> </properties> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${com.fasterxml.jackson.version}</version> </dependency> </dependencies> </project>


Step 2: Create a data transfer object

Following DTO can be used in parsing and creating json.
             	
package com.smoothexample.json; import javax.xml.bind.annotation.XmlRootElement; public class Address { @Override public String toString() { return "Address [streetAddress=" + streetAddress + ", addressOptional=" + addressOptional + ", city=" + city + ", state=" + state + ", country=" + country + ", zip=" + zip + "]"; } private String streetAddress; private String addressOptional; private String city; private String state; private String country; private String zip; public String getStreetAddress() { return streetAddress; } public void setStreetAddress(String streetAddress) { this.streetAddress = streetAddress; } public String getAddressOptional() { return addressOptional; } public void setAddressOptional(String addressOptional) { this.addressOptional = addressOptional; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } }

Step 3:Creating and Parsing Json - Example

Following application will first convert an object to json. Then it will again convert the generated json string to related java object. Alsojson encode and decode properties are set in the method private static ObjectMapper getObjectMapper() {..}
             	
package com.smoothexample.json; import java.io.IOException; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonExample { public static void main( String[] args ) throws IOException { Address address = createAddress(); ObjectMapper objectMapper = getObjectMapper(); System.out.println(" Converting java object to json"); String jsonString = objectMapper.writeValueAsString(address); System.out.println(jsonString); System.out.println(" Converting json to java object "); Address addressFromJson = objectMapper.readValue(jsonString, Address.class); System.out.println(addressFromJson.toString()); } private static Address createAddress() { Address address = new Address(); address.setStreetAddress("4800 abc Rd"); address.setCity("Edison"); address.setState("NJ"); address.setCountry("US"); address.setZip("10001"); address.setAddressOptional("apt 1900"); return address; } private static ObjectMapper getObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); objectMapper.setSerializationInclusion(Include.NON_DEFAULT); objectMapper.setSerializationInclusion(Include.NON_EMPTY); objectMapper.setSerializationInclusion(Include.NON_NULL); return objectMapper; } }

Sample output

             	
Converting java object to json {"streetAddress":"4800 abc Rd","addressOptional":"apt 1900","city":"Edison","state":"NJ","country":"US","zip":"10001"} Converting json to java object Address [streetAddress=4800 abc Rd, addressOptional=apt 1900, city=Edison, state=NJ, country=US, zip=10001]