Marshaling Java Object

This post explains marshalling a java object to json format and storing it to a file. This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects

User

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


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

  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();
}
}

Now the main code which does the marshalling from java object to json file.

Main Code

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MarshallingDemo {
    public static void main(String[] args) throws JsonMappingException, JsonGenerationException, IOException {
        PhoneNumber phoneNumber1 = new PhoneNumber();
        phoneNumber1.setAreaCode("111");
        phoneNumber1.setPart1("222");
        phoneNumber1.setPart2("3333");

    PhoneNumber phoneNumber2 = new PhoneNumber();
    phoneNumber2.setAreaCode("444");
    phoneNumber2.setPart1("555");
    phoneNumber2.setPart2("6666");

    PhoneNumber[] phoneNumbers = new PhoneNumber[2];
    phoneNumbers[0] = phoneNumber1;
    phoneNumbers[1] = phoneNumber2;

    Name name = new Name();
    name.setFirstName("firstName1");
    name.setLastName("lastName1");

    User user = new User();
    user.setName(name);
    user.setSex("M");
    user.setSsn("ssn1");
    user.setAge(28);

    user.setPhoneNumbers(phoneNumbers);

    File file = new File("user.json");

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.writeValue(file, user);
}
}

As shown in the main code, we create an instance of ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();

Then we instruct the objectMapper to convert the java object to json formant and save to a file as shown below
objectMapper.writeValue(file, user);

Output

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

Formatted the output
{
“name”:{
“firstName”:”firstName1″,
“lastName”:”lastName1″
},
“ssn”:”ssn1″,
“sex”:”M”,
“age”:28,
“phoneNumbers”:[
{
“areaCode”:”111″,
“part1″:”222”,
“part2″:”3333”
},
{
“areaCode”:”444″,
“part1″:”555”,
“part2″:”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