Japod's blog
Archives
« May 2008
SunMonTueWedThuFriSat
    
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
27
28
29
30
31
       
Today
Click me to subscribe
Search

Links
 

View My Stats
« Jersey 0.5 Available... | Main | jMaki Widgets Talkin... »
Friday Feb 22, 2008
Better JSON Available In Jersey

Since today a better support for JSON data generated out of JAXB beans is available in Jersey.

The main improvement is a simpler default JSON data format.
Badgerfish convention was replaced by a new one, a slightly simplified so called mapped convention from Jettison. JSON convention used for particular beans is now also fully configurable by the end user.
I will show how the configuration could be done below.

The main differences between available formats could be described using the following example.
Lets start with a very simple JAXB bean. You can see the source code, XML output and then
all formats you can get from Jersey resource:


@XmlRootElement
public static class SimpleJaxbBean {
public String name = "Franz";
public String surname = "Kafka";
}

XML output:


<simpleJaxbBean>
<name>Franz</name>
<surname>Kafka</surname>
</simpleJaxbBean>

Badgerfish format:


{"simpleJaxbBean":{"name":{"$":"Franz"},"surname":{"$":"Kafka"}}}

Mapped format (Jettison):


{"simpleJaxbBean":{"name":"Franz","surname":"Kafka"}}

and finally Jersey default format:


{"name":"Franz","surname":"Kafka"}

If you want to control, which format is actually used for which JAXB bean,
you can do it by providing Jersey runtime with your own custom JAXBContext classes
via implementing custom ContextResolver.

You can get inspired by following example:


@Path("/jsonFormats")
public class SimpleJsonResource {
@Provider
public static class JAXBContextResolver implements ContextResolver<JAXBContext> {

private JAXBContext context;
private Class[] types = {SimpleJaxbBean.class};

public JAXBContextResolver() throws Exception {
Map<String, Object> props = new HashMap<String, Object>();
props.put(JSONJAXBContext.JSON_NOTATION, "MAPPED_JETTISON");
props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.FALSE);
this.context = new JSONJAXBContext(types, props);
}

public JAXBContext getContext(Class<?> objectType) {
return (types[0].equals(objectType)) ? context : null;
}
}

@XmlRootElement
public static class SimpleJaxbBean {
public String name = "Franz";
public String surname = "Kafka";
}

@GET
@ProduceMime({"application/json", "application/xml"})
public SimpleJaxbBean getJson() {
return new SimpleJaxbBean();
}
}

The following options are available for the JSONJAXBContext.JSON_NOTATION
property, where the first one is the default value:

In the future i plan to add more properties so that the JSON serialization and deserialization
is highly configurable.

Posted at 09:10PM Feb 22, 2008 by Jakub Podlesak in REST  |  Comments[4]

Comments:

Congrats!

What about showing how to install this on top of the GFv3 M2 that was announced today?

- eduard/o

Posted by Eduardo Pelegri-Llopart on February 23, 2008 at 12:53 AM CET #

This is definitely a welcome change over badgerfish, but it's proving confusing to me for things that return lists. The default behavior is to take a JAXB bean that would be serialized as:

<list>
<item>
<k>1</k>
<v>A</v>
</item>
<item>
<k>2</k>
<v>B</v>
</item>
</list>

and turn it into

{"item":["k":"1","v":"A","k":"2","v":"B"]}

which is clearly not what i want...help?

Posted by Eric Jensen on February 29, 2008 at 08:17 PM CET #

Hi Eric. Thanks for pointing that out. It should be something like: {"item":[{"k":"1", "v":"A"},{"k":"2","v":"B"}]}, right? I am going to create a test case for this and fix it.

Posted by Jakub on March 03, 2008 at 11:22 AM CET #

Eric, i responded too fast. The above mentioned JSON gets produced just fine.
Please see the unit test at https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/test/com/sun/ws/rest/impl/json/ for a similar case.
Could you please give an example of desired JSON expression you want to get produced?
Thanks,
~Jakub

Posted by Jakub on March 03, 2008 at 04:18 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed