EntityType
- The expected Java type.@Supports(value="application/json") public abstract class AbstractJsonReader<EntityType> extends Object implements Reader<EntityType>
Reader
.
This class provides an easy way to create java object from json and also provides the current context:
ctx
. It provides you a JsonNode
to manage the unmarshalling.
The use of this class optimize the JsonFactory usage especially when aggregating unmarshallers.
Modifier and Type | Field and Description |
---|---|
protected RenderingContext |
ctx
The current
RenderingContext . |
protected MarshallerRegistry |
registry
The marshaller registry.
|
Constructor and Description |
---|
AbstractJsonReader() |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(Class<?> clazz,
Type genericType,
javax.ws.rs.core.MediaType mediatype)
Checks if this marshaller can handle the marshalling request.
|
protected org.codehaus.jackson.JsonNode |
getNode(InputStream in,
boolean getCurrentIfAvailable)
Provide a
JsonNode , try to get it from the context. |
protected String |
getStringField(org.codehaus.jackson.JsonNode jn,
String elName)
Try to get a string property of the given
JsonNode . |
EntityType |
read(Class<?> clazz,
Type genericType,
javax.ws.rs.core.MediaType mediaType,
InputStream in)
Read the entity from in
InputStream using mediatype format. |
abstract EntityType |
read(org.codehaus.jackson.JsonNode jn)
Implement this method, read the entity data in the provided
JsonNode and return corresponding java
object. |
protected <T> T |
readEntity(Class<?> clazz,
Type genericType,
org.codehaus.jackson.JsonNode jn)
Use this method to delegate the unmarshalling of a part or your Json to the
MarshallerRegistry . |
@Inject protected RenderingContext ctx
RenderingContext
.@Inject protected MarshallerRegistry registry
public AbstractJsonReader()
public boolean accept(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype)
Marshaller
Please note it's useless to check that clazz is an instance of EntityType or if generic type and entity type are
compatible (unlike JAX-RS which just checks the clazz, not the generic type). It's also useless to check
Supports
is compatible with mediatype. This is already done by the MarshallerRegistry
This method implementation can use injected properties. So you can check the current RenderingContext
to
accept or reject a marshalling request.
accept
in interface Marshaller<EntityType>
clazz
- The type to marshall.genericType
- The generic type to marshall.mediatype
- The managed mimetype.public EntityType read(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediaType, InputStream in) throws IOException
Reader
InputStream
using mediatype format.
This method implementation can use injected properties.
read
in interface Reader<EntityType>
clazz
- The requested marshalled class.genericType
- The requested marshalled generic type.in
- The input of this marshaller.IOException
- If some error append while reading entity from in.protected org.codehaus.jackson.JsonNode getNode(InputStream in, boolean getCurrentIfAvailable) throws IOException, org.codehaus.jackson.JsonParseException, org.codehaus.jackson.JsonProcessingException
JsonNode
, try to get it from the context.in
- The current InputStream
.getCurrentIfAvailable
- If true, try to get it from the context (if another marshaller already create it and
call this marshaller).JsonNode
.IOException
org.codehaus.jackson.JsonParseException
org.codehaus.jackson.JsonProcessingException
public abstract EntityType read(org.codehaus.jackson.JsonNode jn) throws IOException
JsonNode
and return corresponding java
object.jn
- A ready to use JsonNode
.IOException
protected <T> T readEntity(Class<?> clazz, Type genericType, org.codehaus.jackson.JsonNode jn) throws IOException
MarshallerRegistry
. This will
work only if a Json Reader
is registered for the provided clazz and if the node format is the same as the
one expected by the marshaller.clazz
- The expected Java class.genericType
- The generic type of the expected object: usefull if it's a generic List for example (use
TypeUtils to create the parametrize type).jn
- The JsonNode
to unmarshall.IOException
protected String getStringField(org.codehaus.jackson.JsonNode jn, String elName)
JsonNode
. Return null if the node is null.jn
- The JsonNode
to parse.elName
- The property name.Copyright © 2016 Nuxeo SA. All rights reserved.