Class 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 Json 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
    • Method Detail

      • accept

        public boolean accept​(Class<?> clazz,
                              Type genericType,
                              javax.ws.rs.core.MediaType mediatype)
        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 the MarshallerRegistry

        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 interface Marshaller<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 in InputStream using mediatype format.

        This method implementation can use injected properties.

        Specified by:
        read in interface Reader<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 a JsonNode, try to get it from the context.
        Parameters:
        in - The current InputStream.
        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

        public abstract EntityType read​(com.fasterxml.jackson.databind.JsonNode jn)
                                 throws IOException
        Implement this method, read the entity data in the provided JsonNode and return corresponding java object.
        Parameters:
        jn - A ready to use JsonNode.
        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 the 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.
        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 - The JsonNode to 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 given JsonNode. Return null if the node is null.
        Parameters:
        jn - The JsonNode to 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 given JsonNode. Return null if the node is null or not a boolean.
        Parameters:
        jn - the JsonNode to parse
        elName - the property name
        Returns:
        the boolean value if it exists and is a boolean property, null otherwise
        Since:
        9.2
      • getLongField

        protected Long getLongField​(com.fasterxml.jackson.databind.JsonNode jn,
                                    String elName)
        Tries to get a long property of the given JsonNode. Return null if the node is null or not a number.
        Parameters:
        jn - the JsonNode to parse
        elName - the property name
        Returns:
        the long value if it exists and is a long property, null otherwise
        Since:
        10.2
      • getStringListField

        protected List<StringgetStringListField​(com.fasterxml.jackson.databind.JsonNode jn,
                                                  String elName)
        Tries to get a string list property of the given JsonNode. Return null if the node is null or not a string list.
        Parameters:
        jn - the JsonNode to parse
        elName - the property name
        Returns:
        a string list if it exists and is a valid string list property, null otherwise
        Since:
        9.2