April 12, 2011

JAXB and JSON via Jettison

JAXB implementations (Metro, EclipseLink MOXy, Apache JaxMe, etc) provide an easy means of converting objects to/from XML.  There is a library called Jettison that exposes access to JSON messages via the StAX API that a JAXB implementation can use to convert objects to/from JSON.  This library is being leveraged by a number of JAX-RS implementations.  In this post I'll demonstrate its use in a standalone example.

Java Model

The following Java class will be used for the domain model in this example.

Customer

package blog.json.jettison;

import java.util.List;
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    private int id;

    @XmlElement(name="first-name")
    private String firstName;

    @XmlElement(name="last-name")
    private String lastName;
 
    private Address address;

    @XmlElement(name="phone-number")
    private List<PhoneNumber> phoneNumbers;

}

Address

package blog.json.jettison;
 
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

    private String street;

}

PhoneNumber

package blog.json.jettison;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {

    @XmlAttribute
    private String type;

    @XmlValue
    private String number;

}

XML Input

The JAXB metadata we have applied to our domain model, allow us to interact with the XML document below.  We will use the following XML document to populate our domain model.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <id>123</id>
    <first-name>Jane</first-name>
    <last-name>Doe</last-name>
    <address>
        <street>123 A Street</street>
    </address>
    <phone-number type="work">555-1111</phone-number>
    <phone-number type="cell">555-2222</phone-number>
</customer>

Marshal Demo

Jettison works by exposing StAX interfaces to JSON data.  Since JAXB knows how to deal with StAX interfaces we can easily marshal to them.

package blog.json.jettison;
 
import java.io.*;
import javax.xml.bind.*;
import javax.xml.stream.XMLStreamWriter;
import org.codehaus.jettison.mapped.*;
 
public class MarshalDemo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(new File("src/blog/json/jettison/input.xml"));

        Configuration config = new Configuration();
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        Writer writer = new OutputStreamWriter(System.out);
        XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(customer, xmlStreamWriter);
    }

}

JSON Output

The following JSON was produced (Note:  the JSON produced by the code above was unformatted).

{
   "customer": {
      "id": 123,
      "first-name": "Jane",
      "last-name": "Doe",
      "address": {
         "street": "123 A Street"
      },
      "phone-number": [
         {
            "@type": "work",
            "$": "555-1111"
         },
         {
            "@type": "cell",
            "$": "555-2222"
         }
      ]
   }
}

There are a couple of items to note in the above representation:
  1. Based on what is passed in through the writeCharacters event Jettison will decide if the value should be quoted (line 3).  This is normally fine, but can problematic if you need to differentiate between 123 and "123"
  2. By default Jettison will prefix properties mapped to attributes with '@' (lines 11 and 15).
  3. By default Jettison will represent properties mapped with @XmlValue as '$' (lines 12 and 16).
  4. If an XML element event is reported two or more times consecutively then Jettison will represent this in JSON as an array (lines 9 and 18).  This means if there had only been one instance of PhoneNumber in the collection the JSON representation would have looked like:
{
   "customer": {
      "id": 123,
      "first-name": "Jane",
      "last-name": "Doe",
      "address": {
         "street": "123 A Street"
      },
      "phone-number": {
         "@type": "work",
         "$": "555-1111"
      }
   }
}

Unmarshal Demo

Similar to the marshal use case, we we will unmarshal from a Jettison object (MappedXMLStreamReader) that implements the StAX interface XMLStreamReader.

package blog.json.jettison;

import javax.xml.bind.*;
import javax.xml.stream.XMLStreamReader;
import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.mapped.*;

public class UnmarshalDemo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        JSONObject obj = new JSONObject("{\"customer\":{\"id\":123,\"first-name\":\"Jane\",\"last-name\":\"Doe\",\"address\":{\"street\":\"123 A Street\"},\"phone-number\":[{\"@type\":\"work\",\"$\":\"555-1111\"},{\"@type\":\"cell\",\"$\":\"555-2222\"}]}}");
        Configuration config = new Configuration();
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Customer customer = (Customer) unmarshaller.unmarshal(xmlStreamReader);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

Further Reading

If you enjoyed this post you may also be interested in:
   

10 comments:

  1. How do you take care of namespaces that may be present in the input XML?

    ReplyDelete
  2. I have added an example that demonstrates how to use Jettison when there are namespaces in the JAXB mappings:

    - JAXB and JSON via Jettison - Namespace Example

    For information on JAXB and namespaces:

    - JAXB & Namespaces

    -Blaise

    ReplyDelete
  3. Can i remove the default '@' prefix via some config?

    ReplyDelete
    Replies
    1. Jettison does provide some config options to remove the `@` prefix:
      - http://jira.codehaus.org/browse/JETTISON-2

      You may also be interested in the JSON binding support being added to EclipseLink JAXB (MOXy):
      - JSON Binding with EclipseLink MOXy - Twitter Example

      -Blaise

      Delete
  4. i have different types of namespace in my xml
    sometime it may xmlns="http://www.example.org/XML? or it may
    xmlns="http://www.example.org/
    i got the following error as
    unexpected element (uri:"", "http://www.example.org/). Expected elements are <{"http://www.example.org/XML?}>

    help me!!!!!!!!

    ReplyDelete
    Replies
    1. You haven't given me much to go on, but the following may help:
      - JAXB & Namespaces

      -Blaise

      Delete
  5. How I can rename a element in json like first-name to firstName

    ReplyDelete
    Replies
    1. Hi,

      In this example the element names are controlled using the @XmlElement annotation.

      -Blaise

      Delete
  6. Hi Blaise Doughan,

    The article was very useful. One small clarification. As you mentioned

    If an XML element event is reported two or more times consecutively then Jettison will represent this in JSON as an array (lines 9 and 18). This means if there had only been one instance of PhoneNumber in the collection the JSON representation would have looked like:


    Are there any work around to construct json array by default without considering count, like (lines 9 and 18 from the above point)

    Thanks
    Ganesh

    ReplyDelete

Note: Only a member of this blog may post a comment.