Saturday, December 13, 2008

Jackson 0.9.5: Now With Bean Serialization!

[Update on 22-Oct-2011: a LOT has happened since this blog entry -- be sure to read "7 Jackson Killer Features", "Jackson 1.9" and other later entries here; as well as see FasterXML Jackson Wiki]

The latest Jackson Json-processor release, 0.9.5, is not loaded with tons of features: in fact there is pretty much just one significant new feature. But fortunately, this is the #2 requested feature: ability to serialize regular Java beans/POJOs (so what is #1? read on!). So in this case quality trumps quantity.

Serialization (s11n?) means ability to convert data contained in a Java object into a data format: in this case Json. Prior to 0.9.5, org.codehaus.jackson.map.JavaTypeMapper has been able to serialize some Java objects, but only those that are (or extend) basic Java types, such as java.util containers (Lists, Maps, Sets), wrappers (Boolean, Integer, Double etc), arrays (int[], long[], double[]) or "simple" values (Strings, nulls). While this is somewhat useful for loosely typed data processing, it is still bit low-level compared to normal dealing with Java Beans (simple but custom value types, where getXxx()/setXxx() naming convention is used to imply existence of set of accessible properties -- also known as Plain Old Java Objects, aka POJO).

This is no longer the case: now Jackson can fully serialize instances of any Java classes that use Bean-style method naming.

Let's have a look at a simple example of having class User (and related value classes), as follows:


public interface User {
  public enum Gender { MALE, FEMALE };

  public Name getName();
  public Address getAddress();
  public boolean isVerified();
  public Gender getGender();
  public byte[] getUserImage();
}

public interface Name {
  public String getFirst();
  public String getLast();
}
public interface Address {
  public String getStreet();
  public String getCity();
  public String getState();
  public int getZip();
  public String getCountry();
}

So how do we get Json out of an instance of User? Simple:

  User user = ...; // construct or fetch it
  File resultFile = new File("user.json");
  JavaTypeMapper mapper = new JavaTypeMapper();
  mapper.writeValue(resultFile, user);

and there we have it. Output looks something like (with added indentation for readability -- can be enabled in Jackson JsonGenerator as well, if necessary):


{
 "verified":true,
 "gender":"MALE",
 "userImage":"Rm9vYmFyIQ==", /* base64 encoded gif/jpeg; although this is invalid */
 "address": {
"street":"1 Deadend Street",
"city":"Mercer Island",
"zip":98040,
"state":"WA",
"country":"US"},
 "name": {
   "first":"Santa",
   "last":"Claus"
 }
}

Simple, ay? I thought so.

Want to know more?

Oh and the thing about #1 requested feature? Yes, you guessed it the Bean Deserialization. That's something for Jackson 0.9.6, if my crystal ball is right.

blog comments powered by Disqus

Sponsored By


Related Blogs

(by Author (topics))

Powered By

About me

  • I am known as Cowtowncoder
  • Contact me at@yahoo.com
Check my profile to learn more.