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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| 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 } |
Address
1
2
3
4
5
6
7
8
9
10
| package blog.json.jettison; import javax.xml.bind.annotation.*; @XmlAccessorType (XmlAccessType.FIELD) public class Address { private String street; } |
PhoneNumber
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| 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.
1
2
3
4
5
6
7
8
9
10
11
|
< 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| 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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| { "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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| { "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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| 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
No comments:
Post a Comment