Class AbstractJsonReader<EntityType>
- java.lang.Object
-
- org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonReader<EntityType>
-
- Type Parameters:
EntityType- The expected Java type.
- All Implemented Interfaces:
Marshaller<EntityType>,Reader<EntityType>
- Direct Known Subclasses:
BlobJsonReader,DocumentPropertiesJsonReader,EntityJsonReader
@Supports("application/json") public abstract class AbstractJsonReader<EntityType> extends Object implements Reader<EntityType>
Base class for JsonReader.This class provides an easy way to create java object from json and also provides the current context:
ctx. It provides you aJsonNodeto manage the unmarshalling.The use of this class optimize the JsonFactory usage especially when aggregating unmarshallers.
- Since:
- 7.2
-
-
Field Summary
Fields Modifier and Type Field Description protected RenderingContextctxThe currentRenderingContext.protected MarshallerRegistryregistryThe marshaller registry.
-
Constructor Summary
Constructors Constructor Description AbstractJsonReader()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanaccept(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype)Checks if this marshaller can handle the marshalling request.protected BooleangetBooleanField(com.fasterxml.jackson.databind.JsonNode jn, String elName)Tries to get a boolean property of the givenJsonNode.protected LonggetLongField(com.fasterxml.jackson.databind.JsonNode jn, String elName)Tries to get a long property of the givenJsonNode.protected com.fasterxml.jackson.databind.JsonNodegetNode(InputStream in, boolean getCurrentIfAvailable)Provide aJsonNode, try to get it from the context.protected StringgetStringField(com.fasterxml.jackson.databind.JsonNode jn, String elName)Try to get a string property of the givenJsonNode.protected List<String>getStringListField(com.fasterxml.jackson.databind.JsonNode jn, String elName)Tries to get a string list property of the givenJsonNode.abstract EntityTyperead(com.fasterxml.jackson.databind.JsonNode jn)Implement this method, read the entity data in the providedJsonNodeand return corresponding java object.EntityTyperead(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediaType, InputStream in)Read the entity from inInputStreamusing mediatype format.protected <T> TreadEntity(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 Detail
-
ctx
@Inject protected RenderingContext ctx
The currentRenderingContext.
-
registry
@Inject protected MarshallerRegistry registry
The marshaller registry. You may use it to use other marshallers.
-
-
Method Detail
-
accept
public boolean accept(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype)
Description copied from interface:MarshallerChecks 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
Supportsis compatible with mediatype. This is already done by theMarshallerRegistryThis method implementation can use injected properties. So you can check the current
RenderingContextto accept or reject a marshalling request.- Specified by:
acceptin 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:ReaderRead the entity from inInputStreamusing mediatype format.This method implementation can use injected properties.
- Specified by:
readin 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:
IOExceptioncom.fasterxml.jackson.core.JsonParseExceptioncom.fasterxml.jackson.core.JsonProcessingException- Since:
- 7.2
-
read
public abstract EntityType read(com.fasterxml.jackson.databind.JsonNode jn) throws IOException
Implement this method, read the entity data in the providedJsonNodeand 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 JsonReaderis 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- TheJsonNodeto unmarshall.- Returns:
- An object implementing the expected clazz.
- Throws:
IOException- Since:
- 7.2
-
getStringField
protected String getStringField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
Try to get a string property of the givenJsonNode. Return null if the node is null.- Parameters:
jn- TheJsonNodeto parse.elName- The property name.- Returns:
- The property text if it exists and it's a text, null otherwise.
- Since:
- 7.2
-
getBooleanField
protected Boolean getBooleanField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
Tries to get a boolean property of the givenJsonNode. Returnnullif the node isnullor not a boolean.- Parameters:
jn- theJsonNodeto parseelName- the property name- Returns:
- the boolean value if it exists and is a boolean property,
nullotherwise - Since:
- 9.2
-
getLongField
protected Long getLongField(com.fasterxml.jackson.databind.JsonNode jn, String elName)
Tries to get a long property of the givenJsonNode. Returnnullif the node isnullor not a number.- Parameters:
jn- theJsonNodeto parseelName- the property name- Returns:
- the long value if it exists and is a long property,
nullotherwise - 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. Returnnullif the node isnullor not a string list.- Parameters:
jn- theJsonNodeto parseelName- the property name- Returns:
- a string list if it exists and is a valid string list property,
nullotherwise - Since:
- 9.2
-
-