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.
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:
- 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".
- By default Jettison will prefix properties mapped to attributes with '@' (lines 11 and 15).
- By default Jettison will represent properties mapped with @XmlValue as '$' (lines 12 and 16).
- 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:
- EclipseLink JAXB (MOXy)'s Native JSON Binding
- JSON Binding with EclipseLink MOXy - Twitter Example
- Binding to JSON & XML - Geocode Example
- MOXy as Your JAX-RS JSON Provider - Server Side
- MOXy as Your JAX-RS JSON Provider - Client Side
- Jettison
How do you take care of namespaces that may be present in the input XML?
ReplyDelete@Excelent friend!
ReplyDeleteI have added an example that demonstrates how to use Jettison when there are namespaces in the JAXB mappings:
ReplyDelete- JAXB and JSON via Jettison - Namespace Example
For information on JAXB and namespaces:
- JAXB & Namespaces
-Blaise
Can i remove the default '@' prefix via some config?
ReplyDeleteJettison does provide some config options to remove the `@` prefix:
Delete- 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
You haven't given me much to go on, but the following may help:
ReplyDelete- JAXB & Namespaces
-Blaise
How I can rename a element in json like first-name to firstName
ReplyDeleteHi,
DeleteIn this example the element names are controlled using the @XmlElement annotation.
-Blaise
Hi Blaise Doughan,
ReplyDeleteThe 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