JSON support in JAXB

Developers: Jakub Podlesak
Status: ongoing

Jersey supports JSON with JAXB using the badgerfish convention. This works OK but can produce JSON that is difficult for clients to consume.

Investigate better support for JSON with JAXB that easier for clients to consume.


Goal: "Easier to consume" may have different meaning based on concrete client requirements/capabilities.
The way should be in allowing users to configure the format which suits them best.

Configuration: The plan is to allow users to create their own JAXBContext instance and set the json/format related options on it.

JSON formats/notations: Currently jettison library is used by Jersey. Besides above mentioned badgerfish convention it supports so called mapped
convention. It should be at least possible to switch between the two and to specify namespace mapping for the mapping convention.

jMaki support: for jMaki widgets, mapped convention looks fine, but is not sufficient as is. Current issues: TBD

What needs to be implemented:

  1. Jersey: the mean to take JAXBContext from application and use it by message readers/writers
  2. JAXB: provide a way to set options for marshaller/unmarshaller directly via JAXBContext
  3. JAXB: integrate jettison/another lib into JAXB for JSON support
  4. JAXB: implement customizations for jMaki support
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 16, 2007

    Arun.Gupta says:

    jMaki can used as the client for consuming JSON generated by Jersey. The jMaki d...

    jMaki can used as the client for consuming JSON generated by Jersey. The jMaki data models are documented at: http://wiki.java.net/bin/view/Projects/jMakiDataModels.

  2. Oct 16, 2007

    4894437 says:

    JAX-WS JSON extension has an extension of badgerfish that works better with JAXB...

    JAX-WS JSON extension has an extension of badgerfish that works better with JAXB, by taking advantages of the schema knowledge. It can also produce the type system for JSON.

    So please talk to us when you get to work on this, so that we can share the information.

    1. Oct 16, 2007

      4894437 says:

      For whatever reason SDN decided that my name is 4894437 — my real name is ...

      For whatever reason SDN decided that my name is 4894437 — my real name is Kohsuke Kawaguchi.

  3. Jun 19, 2008

    adrianquark says:

    So far it seems all the Java JSON service implementations rely on deriving JSON ...

    So far it seems all the Java JSON service implementations rely on deriving JSON from XML, when it would be much simpler, and result in much cleaner JSON, to derive the representation directly from the underlying Java objects.

    In most cases, Java objects can be serialized directly to JSON in the obvious way (collections becoming arrays, primitive types becoming scalars, and objects becoming JSON "objects"). In most cases the context should provide sufficient type information for deserialization without any meta-data in the JSON; the only time when it won't is when a field has a non-final type, in which case we can use a convention like an "@java-type" property on the resulting JSON object to act as a type tag.

    I'm currently working on a JAX-WS/JSON webservice and I really miss this facility. The JAXB schema has some awkwardness imposed by the poor mapping between Java objects and XML, and using this as a basis for the JSON just makes things worse. Please consider providing at least the option to derive the JSON serialization directly from the Java (making use of JAXB annotations only where necessary to keep the JSON simple).

  4. Oct 08, 2008

    jeff_robertson says:

    I just made a MessageBodyWriter that uses the JSON-Tools library to create a ver...

    I just made a MessageBodyWriter that uses the JSON-Tools library to create a very stripped-down JSON representation.

    I don't know how well it would work for de-serialization back to Java objects, but if the goal is to make JSON that will feel "idiomatic" to a javascript client.

    Excuse the "auto-generated by eclipse" parameters names (arg0, arg1, etc). this was quick and dirty just to show the idea.

    import java.io.IOException;
    import java.io.OutputStream;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;
    
    import javax.ws.rs.*;
    import javax.ws.rs.core.*;
    import javax.ws.rs.ext.*;
    
    import com.sdicons.json.model.*;
    import com.sdicons.json.mapper.*;
    
    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    public class JSONWriter implements MessageBodyWriter {
    
        public long getSize(Object arg0) {
            return -1;
        }
    
        public boolean isWriteable(Class arg0, Type arg1, Annotation[] arg2) {
            return true;
        }
    
        public void writeTo(Object arg0, Class arg1, Type arg2, Annotation[] arg3,
                javax.ws.rs.core.MediaType arg4, MultivaluedMap arg5,
                OutputStream arg6) throws IOException, WebApplicationException {
            try {
               
                JSONValue json = JSONMapper.toJSON(arg0);
               
                arg6.write( json.render(false).getBytes() );
               
            } catch (MapperException e) {
                throw new WebApplicationException(e);
            }
           
        }
       
    }
    
  5. Oct 09, 2008

    jeff_robertson says:

    I just realized that my previous comment is mostly irrelevant since Badgerfish h...

    I just realized that my previous comment is mostly irrelevant since Badgerfish has already been made not the default.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact