org.codehaus.jackson.jaxrs
Class JacksonJsonProvider

java.lang.Object
  extended by org.codehaus.jackson.jaxrs.JacksonJsonProvider
All Implemented Interfaces:
MessageBodyReader<Object>, MessageBodyWriter<Object>

@Provider
public class JacksonJsonProvider
extends Object
implements MessageBodyReader<Object>, MessageBodyWriter<Object>

Basic implementation of JAX-RS abstractions (MessageBodyReader, MessageBodyWriter) needed for binding JSON ("application/json") content to and from POJOs.

Currently most configurability is via caller configuring ObjectMapper it uses to construct this provider. Additionally it is possible to enable detection of which types can be serialized/deserialized, which is not enabled by default (since it is usually not needed).

Author:
Tatu Saloranta

Field Summary
protected  boolean _cfgCheckCanDeserialize
          Whether we want to actually check that Jackson has a deserializer for given type.
protected  boolean _cfgCheckCanSerialize
          Whether we want to actually check that Jackson has a serializer for given type.
protected  ObjectMapper _configuredMapper
          Mapper provider was constructed with if any; if null, provider is dynamically located; and if that fails, default instance will be used.
protected  ObjectMapper _defaultMapper
           
protected  Providers _providers
          Injectable context object used to locate configured instance of ObjectMapper to use for actual serialization.
static Class<?>[] _unreadableClasses
           
static HashSet<ClassKey> _untouchables
          06-Apr-2009, tatu: Looks like we need to worry about accidental data binding for types we shouldn't be handling.
static Class<?>[] _unwritableClasses
           
 
Constructor Summary
JacksonJsonProvider()
          Default constructor, usually used when provider is automatically configured to be used with JAX-RS implementation.
JacksonJsonProvider(ObjectMapper mapper)
          Constructor to use when a custom mapper (usually components like serializer/deserializer factories that have been configured) is to be used.
 
Method Summary
protected  JavaType _convertType(Type jdkType)
          Method used to construct a JDK generic type into type definition Jackson understands.
 void checkCanDeserialize(boolean state)
           
 void checkCanSerialize(boolean state)
           
 long getSize(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
           
 boolean isJsonType(MediaType mediaType)
           
 boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
           
 boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
           
protected  ObjectMapper locateMapper(Class<?> type, MediaType mediaType)
          Method called to locate ObjectMapper to use for serialization and deserialization.
 Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream)
           
 void setMapper(ObjectMapper m)
          Method that can be used to directly define ObjectMapper to use for serialization and deserialization; if null, will use the standard provider discovery from context instead.
 void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_untouchables

public static final HashSet<ClassKey> _untouchables
06-Apr-2009, tatu: Looks like we need to worry about accidental data binding for types we shouldn't be handling. This is probably not a very good way to do it, but let's start by blacklisting things we are not to handle. (why ClassKey? since plain old Class has no hashCode() defined, lookups are painfully slow)


_unreadableClasses

public static final Class<?>[] _unreadableClasses

_unwritableClasses

public static final Class<?>[] _unwritableClasses

_defaultMapper

protected ObjectMapper _defaultMapper

_providers

@Context
protected Providers _providers
Injectable context object used to locate configured instance of ObjectMapper to use for actual serialization.


_configuredMapper

protected ObjectMapper _configuredMapper
Mapper provider was constructed with if any; if null, provider is dynamically located; and if that fails, default instance will be used.


_cfgCheckCanSerialize

protected boolean _cfgCheckCanSerialize
Whether we want to actually check that Jackson has a serializer for given type. Since this should generally be the case (due to auto-discovery) and since the call to check this is not free, defaults to false.


_cfgCheckCanDeserialize

protected boolean _cfgCheckCanDeserialize
Whether we want to actually check that Jackson has a deserializer for given type. Since this should generally be the case (due to auto-discovery) and since the call to check this is not free, defaults to false.

Constructor Detail

JacksonJsonProvider

public JacksonJsonProvider()
Default constructor, usually used when provider is automatically configured to be used with JAX-RS implementation.


JacksonJsonProvider

public JacksonJsonProvider(ObjectMapper mapper)
Constructor to use when a custom mapper (usually components like serializer/deserializer factories that have been configured) is to be used.

Method Detail

checkCanDeserialize

public void checkCanDeserialize(boolean state)

checkCanSerialize

public void checkCanSerialize(boolean state)

setMapper

public void setMapper(ObjectMapper m)
Method that can be used to directly define ObjectMapper to use for serialization and deserialization; if null, will use the standard provider discovery from context instead. Default setting is null.


isReadable

public boolean isReadable(Class<?> type,
                          Type genericType,
                          Annotation[] annotations,
                          MediaType mediaType)
Specified by:
isReadable in interface MessageBodyReader<Object>

readFrom

public Object readFrom(Class<Object> type,
                       Type genericType,
                       Annotation[] annotations,
                       MediaType mediaType,
                       MultivaluedMap<String,String> httpHeaders,
                       InputStream entityStream)
                throws IOException
Specified by:
readFrom in interface MessageBodyReader<Object>
Throws:
IOException

getSize

public long getSize(Object value,
                    Class<?> type,
                    Type genericType,
                    Annotation[] annotations,
                    MediaType mediaType)
Specified by:
getSize in interface MessageBodyWriter<Object>

isWriteable

public boolean isWriteable(Class<?> type,
                           Type genericType,
                           Annotation[] annotations,
                           MediaType mediaType)
Specified by:
isWriteable in interface MessageBodyWriter<Object>

writeTo

public void writeTo(Object value,
                    Class<?> type,
                    Type genericType,
                    Annotation[] annotations,
                    MediaType mediaType,
                    MultivaluedMap<String,Object> httpHeaders,
                    OutputStream entityStream)
             throws IOException
Specified by:
writeTo in interface MessageBodyWriter<Object>
Throws:
IOException

locateMapper

protected ObjectMapper locateMapper(Class<?> type,
                                    MediaType mediaType)
Method called to locate ObjectMapper to use for serialization and deserialization. If an instance has been explicitly defined by setMapper(org.codehaus.jackson.map.ObjectMapper) (or non-null instance passed in constructor), that will be used. If not, will try to locate it using standard JAX-RS ContextResolver mechanism, if it has been properly configured to access it (by JAX-RS runtime). Finally, if no mapper is found, will return a default unconfigured ObjectMapper instance (one constructed with default constructor and not modified in any way)

Parameters:
type - Class of object being serialized or deserialized; not checked at this point, since it is assumed that unprocessable classes have been already weeded out, but will be passed to ContextResolver as is.
mediaType - Declared media type for the instance to process: not used by this method, but will be passed to ContextResolver as is.

isJsonType

public boolean isJsonType(MediaType mediaType)

_convertType

protected JavaType _convertType(Type jdkType)
Method used to construct a JDK generic type into type definition Jackson understands.