com.sun.jersey.api.json
Class JSONJAXBContext

java.lang.Object
  extended by javax.xml.bind.JAXBContext
      extended by com.sun.jersey.api.json.JSONJAXBContext

public final class JSONJAXBContext
extends javax.xml.bind.JAXBContext

An adaption of JAXBContext that supports marshalling and unmarshalling of JAXB beans using the JSON format.

The JSON format may be configured by setting properties on this class when it is constructed and on properties of the Marshaller and Unmarshaller returned from the relevant methods on this class.

To enable JSON marshalling and unmarshalling it is necessary to set the JSON_ENABLED property to true on the Marshaller and Unmarshaller returned from the relevant methods on this class.


Nested Class Summary
static class JSONJAXBContext.JSONNotation
          Enumeration of supported JSON notations.
 
Field Summary
static java.lang.String JSON_ARRAYS
          JSON arrays property.
static java.lang.String JSON_ATTRS_AS_ELEMS
          JSON attributes as elements property.
static java.lang.String JSON_ENABLED
          JSON enabled property.
static java.lang.String JSON_NON_STRINGS
          JSON non-string values property.
static java.lang.String JSON_NOTATION
          JSON notation property.
static java.lang.String JSON_ROOT_UNWRAPPING
          XML root element unwrapping.
static java.lang.String JSON_XML2JSON_NS
          XML to JSON namespace mapping.
static java.lang.String NAMESPACE
          A namespace for JSONJAXBContext related properties names.
 
Fields inherited from class javax.xml.bind.JAXBContext
JAXB_CONTEXT_FACTORY
 
Constructor Summary
JSONJAXBContext(java.lang.Class... classesToBeBound)
          Constructs a new instance with default properties.
JSONJAXBContext(java.lang.Class[] classesToBeBound, java.util.Map<java.lang.String,java.lang.Object> properties)
          Constructs a new instance with a custom set of properties.
JSONJAXBContext(java.lang.String contextPath)
          Construct a new instance of using context class loader of the thread with default properties.
JSONJAXBContext(java.lang.String contextPath, java.lang.ClassLoader classLoader)
          Construct a new instance using a specified class loader with default properties.
JSONJAXBContext(java.lang.String contextPath, java.lang.ClassLoader classLoader, java.util.Map<java.lang.String,java.lang.Object> properties)
          Construct a new instance using a specified class loader and a custom set of properties.
 
Method Summary
 javax.xml.bind.Marshaller createMarshaller()
          Overrides underlaying createMarshaller method and returns a marshaller which is capable of JSON serialization.
 javax.xml.bind.Unmarshaller createUnmarshaller()
          Overrides underlaying createUnmarshaller method and returns an unmarshaller which is capable of JSON deserialization.
 javax.xml.bind.Validator createValidator()
          Simply delegates to underlying JAXBContext implementation.
 
Methods inherited from class javax.xml.bind.JAXBContext
createBinder, createBinder, createJAXBIntrospector, generateSchema, newInstance, newInstance, newInstance, newInstance, newInstance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAMESPACE

public static final java.lang.String NAMESPACE
A namespace for JSONJAXBContext related properties names.

See Also:
Constant Field Values

JSON_NOTATION

public static final java.lang.String JSON_NOTATION
JSON notation property.

The type of this property is enum type JSONJAXBContext.JSONNotation.

The value may be one of the following that are the currently supported JSON notations: JSONNotation.MAPPED, JSONNotation.MAPPED_JETTISON and JSONNotation.BADGERFISH

The default value is JSONNotation.MAPPED.

See Also:
Constant Field Values

JSON_ENABLED

public static final java.lang.String JSON_ENABLED
JSON enabled property.

The type of this property is Boolean

If set to true, JSON will be serialized/deserialized instead of XML

The default value is false.

See Also:
Constant Field Values

JSON_ROOT_UNWRAPPING

public static final java.lang.String JSON_ROOT_UNWRAPPING
XML root element unwrapping.

The type of this property is Boolean

If set to true, JSON code corresponding to the XML root element will be stripped out for JSONNotation.MAPPED (default) notation.

The default value is false.

See Also:
Constant Field Values

JSON_ARRAYS

public static final java.lang.String JSON_ARRAYS
JSON arrays property. This property is valid for the JSONNotation.MAPPED notation only.

The type of this property is java.util.Collection<String>.

The value is a collection of string values that are object names. The value of an object name in the JSON document that exists in the collection of object names will be declared as an array, even if only one element is present.

For example, consider that the the property value is not set and the JSON document is { ..., "arr1":"single element", ... }. If the property value is set to contain "arr1" then the JSON document would be { ..., "arr1":["single element"], ... }.

The default value is an empty collection.

See Also:
Constant Field Values

JSON_NON_STRINGS

public static final java.lang.String JSON_NON_STRINGS
JSON non-string values property. This property is valid for the JSONNotation.MAPPED notation only.

The type of this property is Collection<String>.

The value is collection of string values that are object names. The value of an object name in the JSON document that exists in the collection of object names will be declared as non-string value, which is not surrounded by double quotes.

For example, consider that the the property value is not set and the JSON document is { ..., "anumber":"12", ... }. If the property value is set to contain "anumber" then the JSON document would be { ..., "anumber":12, ... }.

The default value is an empty collection.

See Also:
Constant Field Values

JSON_ATTRS_AS_ELEMS

public static final java.lang.String JSON_ATTRS_AS_ELEMS
JSON attributes as elements property. This property is valid for the JSONNotation.MAPPED notation only.

The type of this property is Collection<String>.

The value is a collection of string values that are object names that correspond to XML attribute information items. The value of an object name in the JSON document that exists in the collection of object names will be declared as an element as not as an attribute if the object corresponds to an XML attribute information item.

For example, consider that the the property value is not set and the JSON document is { ..., "@number":"12", ... }. If the property value is set contain "number" then the JSON document would be { ..., "number":"12", ... }.

The default value is an empty collection.

See Also:
Constant Field Values

JSON_XML2JSON_NS

public static final java.lang.String JSON_XML2JSON_NS
XML to JSON namespace mapping. This property is valid for the MAPPED_JETTISON notation only.

The type of this property is Map<String,String>.

The value is a map with zero or more key/value pairs, where the key is an XML namespace and the value is the prefix to use as the replacement for the XML namespace.

The default value is a map with zero key/value pairs.

See Also:
Constant Field Values
Constructor Detail

JSONJAXBContext

public JSONJAXBContext(java.lang.Class... classesToBeBound)
                throws javax.xml.bind.JAXBException
Constructs a new instance with default properties.

Parameters:
classesToBeBound - list of java classes to be recognized by the new JSONJAXBContext. Can be empty, in which case a JSONJAXBContext that only knows about spec-defined classes will be returned.
Throws:
javax.xml.bind.JAXBException - if an error was encountered while creating the underlying JAXBContext.

JSONJAXBContext

public JSONJAXBContext(java.lang.Class[] classesToBeBound,
                       java.util.Map<java.lang.String,java.lang.Object> properties)
                throws javax.xml.bind.JAXBException
Constructs a new instance with a custom set of properties.

Parameters:
classesToBeBound - list of java classes to be recognized by the new JSONJAXBContext. Can be empty, in which case a JSONJAXBContext that only knows about spec-defined classes will be returned.
properties - the custom set of properties.
Throws:
javax.xml.bind.JAXBException - if an error was encountered while creating the underlying JAXBContext.

JSONJAXBContext

public JSONJAXBContext(java.lang.String contextPath)
                throws javax.xml.bind.JAXBException
Construct a new instance of using context class loader of the thread with default properties.

Parameters:
contextPath - list of java package names that contain schema derived class and/or java to schema (JAXB-annotated) mapped classes
Throws:
javax.xml.bind.JAXBException - if an error was encountered while creating the underlying JAXBContext.

JSONJAXBContext

public JSONJAXBContext(java.lang.String contextPath,
                       java.lang.ClassLoader classLoader)
                throws javax.xml.bind.JAXBException
Construct a new instance using a specified class loader with default properties.

Parameters:
contextPath - list of java package names that contain schema derived class and/or java to schema (JAXB-annotated) mapped classes
classLoader -
Throws:
javax.xml.bind.JAXBException - if an error was encountered while creating the underlying JAXBContext.

JSONJAXBContext

public JSONJAXBContext(java.lang.String contextPath,
                       java.lang.ClassLoader classLoader,
                       java.util.Map<java.lang.String,java.lang.Object> properties)
                throws javax.xml.bind.JAXBException
Construct a new instance using a specified class loader and a custom set of properties.

Parameters:
contextPath - list of java package names that contain schema derived class and/or java to schema (JAXB-annotated) mapped classes
classLoader -
properties - the custom set of properties.
Throws:
javax.xml.bind.JAXBException - if an error was encountered while creating the underlying JAXBContext.
Method Detail

createUnmarshaller

public javax.xml.bind.Unmarshaller createUnmarshaller()
                                               throws javax.xml.bind.JAXBException
Overrides underlaying createUnmarshaller method and returns an unmarshaller which is capable of JSON deserialization.

Specified by:
createUnmarshaller in class javax.xml.bind.JAXBContext
Returns:
unmarshaller instance with JSON capabilities
Throws:
javax.xml.bind.JAXBException

createMarshaller

public javax.xml.bind.Marshaller createMarshaller()
                                           throws javax.xml.bind.JAXBException
Overrides underlaying createMarshaller method and returns a marshaller which is capable of JSON serialization.

Specified by:
createMarshaller in class javax.xml.bind.JAXBContext
Returns:
marshaller instance with JSON capabilities
Throws:
javax.xml.bind.JAXBException

createValidator

public javax.xml.bind.Validator createValidator()
                                         throws javax.xml.bind.JAXBException
Simply delegates to underlying JAXBContext implementation.

Specified by:
createValidator in class javax.xml.bind.JAXBContext
Returns:
what underlying JAXBContext returns
Throws:
javax.xml.bind.JAXBException


Copyright © 2008 Sun Microsystems, Inc. All Rights Reserved.