Class AbstractJsonReader<EntityType>
- Type Parameters:
EntityType
- The expected Java type.
- All Implemented Interfaces:
Marshaller<EntityType>
,Reader<EntityType>
- Direct Known Subclasses:
BlobJsonReader
,DocumentPropertiesJsonReader
,EntityJsonReader
,ScimResourceReader
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.
- Since:
- 7.2
-
Field Summary
Modifier and TypeFieldDescriptionprotected RenderingContext
The currentRenderingContext
.protected MarshallerRegistry
The marshaller registry. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
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 givenJsonNode
.protected Long
getLongField
(com.fasterxml.jackson.databind.JsonNode jn, String elName) Tries to get a long property of the givenJsonNode
.protected com.fasterxml.jackson.databind.JsonNode
getNode
(InputStream in, boolean getCurrentIfAvailable) Provide aJsonNode
, 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 givenJsonNode
.getStringListField
(com.fasterxml.jackson.databind.JsonNode jn, String elName) Tries to get a string list property of the givenJsonNode
.abstract EntityType
read
(com.fasterxml.jackson.databind.JsonNode jn) Implement this method, read the entity data in the providedJsonNode
and return corresponding java object.read
(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediaType, InputStream in) Read the entity from inInputStream
using mediatype format.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 theMarshallerRegistry
.
-
Field Details
-
ctx
The currentRenderingContext
. -
registry
The marshaller registry. You may use it to use other marshallers.
-
-
Constructor Details
-
AbstractJsonReader
public AbstractJsonReader()
-
-
Method Details
-
accept
Description copied from interface:Marshaller
Checks if this marshaller can handle the marshalling request.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 theMarshallerRegistry
This method implementation can use injected properties. So you can check the current
RenderingContext
to accept or reject a marshalling request.- Specified by:
accept
in interfaceMarshaller<EntityType>
- Parameters:
clazz
- The type to marshall.genericType
- The generic type to marshall.mediatype
- The managed mimetype.- Returns:
- true if this converter handle the request, false otherwise.
-
read
public EntityType read(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediaType, InputStream in) throws IOException Description copied from interface:Reader
Read the entity from inInputStream
using mediatype format.This method implementation can use injected properties.
- Specified by:
read
in interfaceReader<EntityType>
- Parameters:
clazz
- The requested marshalled class.genericType
- The requested marshalled generic type.mediaType
- The input media type.in
- The input of this marshaller.- Throws:
IOException
- If some error append while reading entity from in.
-
getNode
protected com.fasterxml.jackson.databind.JsonNode getNode(InputStream in, boolean getCurrentIfAvailable) throws IOException, com.fasterxml.jackson.core.JsonParseException, com.fasterxml.jackson.core.JsonProcessingException Provide aJsonNode
, try to get it from the context.- Parameters:
in
- The currentInputStream
.getCurrentIfAvailable
- If true, try to get it from the context (if another marshaller already create it and call this marshaller).- Returns:
- A valid
JsonNode
. - Throws:
IOException
com.fasterxml.jackson.core.JsonParseException
com.fasterxml.jackson.core.JsonProcessingException
- Since:
- 7.2
-
read
Implement this method, read the entity data in the providedJsonNode
and return corresponding java object.- Parameters:
jn
- A ready to useJsonNode
.- Returns:
- The unmarshalled entity.
- Throws:
IOException
- Since:
- 7.2
-
readEntity
protected <T> T readEntity(Class<?> clazz, Type genericType, com.fasterxml.jackson.databind.JsonNode jn) throws IOException Use this method to delegate the unmarshalling of a part or your Json to theMarshallerRegistry
. This will work only if a JsonReader
is registered for the provided clazz and if the node format is the same as the one expected by the marshaller.- Parameters:
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
- TheJsonNode
to unmarshall.- Returns:
- An object implementing the expected clazz.
- Throws:
IOException
- Since:
- 7.2
-
getStringField
Try to get a string property of the givenJsonNode
. Return null if the node is null.- Parameters:
jn
- TheJsonNode
to parse.elName
- The property name.- Returns:
- The property text if it exists and it's a text, null otherwise.
- Since:
- 7.2
-
getBooleanField
Tries to get a boolean property of the givenJsonNode
. Returnnull
if the node isnull
or not a boolean.- Parameters:
jn
- theJsonNode
to parseelName
- the property name- Returns:
- the boolean value if it exists and is a boolean property,
null
otherwise - Since:
- 9.2
-
getLongField
Tries to get a long property of the givenJsonNode
. Returnnull
if the node isnull
or not a number.- Parameters:
jn
- theJsonNode
to parseelName
- the property name- Returns:
- the long value if it exists and is a long property,
null
otherwise - Since:
- 10.2
-
getStringListField
protected List<String> getStringListField(com.fasterxml.jackson.databind.JsonNode jn, String elName) Tries to get a string list property of the givenJsonNode
. Returnnull
if the node isnull
or not a string list.- Parameters:
jn
- theJsonNode
to parseelName
- the property name- Returns:
- a string list if it exists and is a valid string list property,
null
otherwise - Since:
- 9.2
-