Achieving data binding with object model

This post explains how to achieve mapping of json data represented by an object model to particular java object.

Here object model is tree like data structure which reads and stores the entire json content in memory.

The difference between this post and other posts in Binding section is that latter posts explains marshalling and unmarshalling of json data without storing them completely in memory.

Below is an example

user.json


{
    "name":{
                "firstName":"firstName1",
                "lastName":"lastName1"
            },
    "ssn":"ssn1",
    "sex":"M",
    "age":28,
    "phoneNumbers":[
        {"areaCode":"111","part1":"222","part2":"3333"},
        {"areaCode":"444","part1":"555","part2":"6666"}
    ]
}

User Class


package package10;

public class User {
    private Name name;
    private String ssn;
    private String sex;
    private int age;
    private PhoneNumber[] phoneNumbers;
    
    public Name getName() {
        return name;
    }
    public void setName(Name name) {
        this.name = name;
    }
    public String getSsn() {
        return ssn;
    }
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public PhoneNumber[] getPhoneNumbers() {
        return phoneNumbers;
    }
    public void setPhoneNumbers(PhoneNumber[] phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(name).append(",");
        sb.append(ssn).append(",");
        sb.append(sex).append(",");
        sb.append(age).append(",");
        
        for(PhoneNumber phoneNumber : phoneNumbers) {
            sb.append(phoneNumber).append(",");
        }
        sb.append("}");
        return sb.toString();
    }
}

Name Class


package package10;

public class Name {
    private String firstName;
    private String lastName;
    
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(firstName).append(",");
        sb.append(lastName);
        sb.append("}");
        
        return sb.toString();
    }
}

PhoneNumber Class


package package10;

public class PhoneNumber {
    private String areaCode;
    private String part1;
    private String part2;
    
    public String getAreaCode() {
        return areaCode;
    }
    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }
    public String getPart1() {
        return part1;
    }
    public void setPart1(String part1) {
        this.part1 = part1;
    }
    public String getPart2() {
        return part2;
    }
    public void setPart2(String part2) {
        this.part2 = part2;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(areaCode).append("-");
        sb.append(part1).append("-");
        sb.append(part2);
        sb.append("}");
        
        return sb.toString();
    }
}

Main code


1  package package10;
2  
3  import java.io.File;
4  import java.io.IOException;
5  
6  import com.fasterxml.jackson.core.JsonProcessingException;
7  import com.fasterxml.jackson.databind.JsonNode;
8  import com.fasterxml.jackson.databind.ObjectMapper;
9  
10 public class TreeModelDemo3 {
11  public static void main(String[] args) throws JsonProcessingException, IOException {
12      ObjectMapper objectMapper = new ObjectMapper();
13      File file = new File("user.json");
14      JsonNode jsonNode = objectMapper.readTree(file);
15      User user = objectMapper.treeToValue(jsonNode, User.class);
16      System.out.println(user);
17  }
18 }

Explanation

The user.json file contains serialized data of a particular User class's object.

At line 14 we read the json file content into the object jsonNode.

Then we mapped it to another User instance by calling treeToValue method in ObjectMapper. Refer to line 15.

treeToValue method takes two agruments
1) the object model
2) Class instance of the class to be mapped.

Output

{{firstName1,lastName1},ssn1,M,28,{111-222-3333},{444-555-6666},}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s