Customizing the JSON output generated by Jackson

Recently i was using Jackson JSON Processor for converting java object into JSON. When i did that i noticed JackSon was storing date in the long format i.e. it was converting date.getTime() and using that long value in JSON instead of formatted date. This is how my Contact object looks like
package com.spnotes.misc;
import com.spnotes.misc.com.spnotes.misc.CustomDateSerializer;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.util.Date;
/**
* Created by gpzpati on 11/19/13.
*/
public class Contact {
private String firstName;
private String lastName;
private Date dateOfBirth;
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;
}
@JsonSerialize(using = CustomDateSerializer.class)
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Override
public String toString() {
return "Contact{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", dateOfBirth=" + dateOfBirth +
'}';
}
}
view raw Contact.java hosted with ❤ by GitHub
Note: please ignore line 32 @JsonSerialize(using = CustomDateSerializer.class) for now. I had following code to create object of Contact and converting and then using Jackson to convert it to String and write it on console
package com.spnotes.misc.com.spnotes.misc;
import com.spnotes.misc.Contact;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.Calendar;
import java.util.Date;
/**
* Created by gpzpati on 11/19/13.
*/
public class JSONTester {
public static void main(String[] argv) throws Exception{
Contact contact = new Contact();
contact.setFirstName("Sachin");
contact.setLastName("Tendulkar");
ObjectMapper objectMapper = new ObjectMapper();
Calendar cal = Calendar.getInstance();
cal.set(1973,3,24);
contact.setDateOfBirth(cal.getTime());
System.out.println(objectMapper.writeValueAsString(contact));
//objectMapper.writeValue(, contact);
}
}
view raw JSONTester.java hosted with ❤ by GitHub
This was the output that got generated

{"firstName":"Sachin","lastName":"Tendulkar","dateOfBirth":60065349443063}
Now i wanted to customize how the date so that it got generated in dd-MMM-yyyy format. In order to do that i started by creating CustomDateSerializer class like this
package com.spnotes.misc.com.spnotes.misc;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.io.IOException;
import java.util.Date;
/**
* Created by gpzpati on 11/19/13.
*/
public class CustomDateSerializer extends JsonSerializer<Date>{
private static DateTimeFormatter formatter =
DateTimeFormat.forPattern("dd-MMM-yyyy");
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString(formatter.print(date.getTime()));
}
}
Then i had to change the Contact.java class to add @JsonSerialize(using = CustomDateSerializer.class) annotation to getDateOfBirth() field, i could attach it to any other date field that i want. Now when i run JSONTester this the output that it generates

{"firstName":"Sachin","lastName":"Tendulkar","dateOfBirth":"24-Apr-1973"}
You can download the source code for this program from here

No comments: