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 Boolean |
getBooleanField(com.fasterxml.jackson.databind.JsonNode jn,
String elName)
Tries to get a boolean property of the given
JsonNode . |
protected Long |
getLongField(com.fasterxml.jackson.databind.JsonNode jn,
String elName)
Tries to get a long property of the given
JsonNode . |
protected com.fasterxml.jackson.databind.JsonNode |
getNode(InputStream in,
boolean getCurrentIfAvailable)
Provide a
JsonNode , try to get it from the context. |
protected String |
getStringField(com.fasterxml.jackson.databind.JsonNode jn,
String elName)
Try to get a string property of the given
JsonNode . |
protected List<String> |
getStringListField(com.fasterxml.jackson.databind.JsonNode jn,
String elName)
Tries to get a string list 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(com.fasterxml.jackson.databind.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,
com.fasterxml.jackson.databind.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 com.fasterxml.jackson.databind.JsonNode getNode(InputStream in, boolean getCurrentIfAvailable) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.core.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
com.fasterxml.jackson.core.JsonParseException
com.fasterxml.jackson.core.JsonProcessingException
public abstract EntityType read(com.fasterxml.jackson.databind.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, com.fasterxml.jackson.databind.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(com.fasterxml.jackson.databind.JsonNode jn, String elName)
JsonNode
. Return null if the node is null.jn
- The JsonNode
to parse.elName
- The property name.protected Boolean getBooleanField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
JsonNode
. Return null
if the node is null
or
not a boolean.jn
- the JsonNode
to parseelName
- the property namenull
otherwiseprotected Long getLongField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
JsonNode
. Return null
if the node is null
or
not a number.jn
- the JsonNode
to parseelName
- the property namenull
otherwiseprotected List<String> getStringListField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
JsonNode
. Return null
if the node is
null
or not a string list.jn
- the JsonNode
to parseelName
- the property namenull
otherwiseCopyright © 2018 Nuxeo. All rights reserved.