Class ResourceConverter


  • public class ResourceConverter
    extends Object
    JSON API data converter.
    Provides methods for conversion between JSON API resources to java POJOs and vice versa.
    Author:
    jbegic
    • Constructor Detail

      • ResourceConverter

        public ResourceConverter​(Class<?>... classes)
        Creates new ResourceConverter.

        All classes that should be handled by instance of ResourceConverter must be registered when creating a new instance of it.

        Parameters:
        classes - Class array of classes to be handled by this resource converter instance
      • ResourceConverter

        public ResourceConverter​(String baseURL,
                                 Class<?>... classes)
        Creates new ResourceConverter.

        All classes that should be handled by instance of ResourceConverter must be registered when creating a new instance of it.

        Parameters:
        baseURL - String base URL, eg. https://api.mysite.com
        classes - Class array of classes to be handled by this resource converter instance
      • ResourceConverter

        public ResourceConverter​(com.fasterxml.jackson.databind.ObjectMapper mapper,
                                 Class<?>... classes)
      • ResourceConverter

        public ResourceConverter​(com.fasterxml.jackson.databind.ObjectMapper mapper,
                                 String baseURL,
                                 Class<?>... classes)
        Creates new ResourceConverter.
        Parameters:
        mapper - ObjectMapper custom mapper to be used for resource parsing
        baseURL - String base URL, eg. https://api.mysite.com
        classes - Class array of classes to be handled by this resource converter instance
    • Method Detail

      • setGlobalResolver

        public void setGlobalResolver​(RelationshipResolver resolver)
        Registers global relationship resolver. This resolver will be used in case relationship is present in the API response but not provided in the included section and relationship resolving is enabled trough relationship annotation.
        In case type resolver is registered it will be used instead.
        Parameters:
        resolver - resolver instance
      • setTypeResolver

        public void setTypeResolver​(RelationshipResolver resolver,
                                    Class<?> type)
        Registers relationship resolver for given type. Resolver will be used if relationship resolution is enabled trough relationship annotation.
        Parameters:
        resolver - resolver instance
        type - type
      • readObject

        @Deprecated
        public <T> T readObject​(byte[] data,
                                Class<T> clazz)
        Deprecated.
        Converts raw data input into requested target type.
        Type Parameters:
        T - type
        Parameters:
        data - raw data
        clazz - target object
        Returns:
        converted object
        Throws:
        RuntimeException - in case conversion fails
      • readObjectCollection

        @Deprecated
        public <T> List<T> readObjectCollection​(byte[] data,
                                                Class<T> clazz)
        Deprecated.
        Converts rawdata input into a collection of requested output objects.
        Type Parameters:
        T - type
        Parameters:
        data - raw data input
        clazz - target type
        Returns:
        collection of converted elements
        Throws:
        RuntimeException - in case conversion fails
      • readDocument

        public <T> JSONAPIDocument<T> readDocument​(byte[] data,
                                                   Class<T> clazz)
        Reads JSON API spec document and converts it into target type.
        Type Parameters:
        T - type
        Parameters:
        data - byte raw data (server response)
        clazz - Class target type
        Returns:
        JSONAPIDocument
      • readDocument

        public <T> JSONAPIDocument<T> readDocument​(InputStream dataStream,
                                                   Class<T> clazz)
        Reads JSON API spec document and converts it into target type.
        Type Parameters:
        T - type
        Parameters:
        dataStream - byte raw dataStream (server response)
        clazz - Class target type
        Returns:
        JSONAPIDocument
      • readDocumentCollection

        public <T> JSONAPIDocument<List<T>> readDocumentCollection​(byte[] data,
                                                                   Class<T> clazz)
        Reads JSON API spec document and converts it into collection of target type objects.
        Type Parameters:
        T - type
        Parameters:
        data - byte raw data (server response)
        clazz - Class target type
        Returns:
        JSONAPIDocument
      • readDocumentCollection

        public <T> JSONAPIDocument<List<T>> readDocumentCollection​(InputStream dataStream,
                                                                   Class<T> clazz)
        Reads JSON API spec document and converts it into collection of target type objects.
        Type Parameters:
        T - type
        Parameters:
        dataStream - InputStream input stream
        clazz - Class target type
        Returns:
        JSONAPIDocument
      • writeObject

        @Deprecated
        public byte[] writeObject​(Object object)
                           throws com.fasterxml.jackson.core.JsonProcessingException,
                                  IllegalAccessException
        Deprecated.
        Converts input object to byte array.
        Parameters:
        object - input object
        Returns:
        raw bytes
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
        IllegalAccessException
      • writeObjectCollection

        @Deprecated
        public <T> byte[] writeObjectCollection​(Iterable<T> objects)
                                         throws com.fasterxml.jackson.core.JsonProcessingException,
                                                IllegalAccessException
        Deprecated.
        use writeDocumentCollection instead
        Converts input object to byte array.
        Parameters:
        objects - List of input objects
        Returns:
        raw bytes
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
        IllegalAccessException
      • isRegisteredType

        public boolean isRegisteredType​(Class<?> type)
        Checks if provided type is registered with this converter instance.
        Parameters:
        type - class to check
        Returns:
        returns true if type is registered, else false
      • registerType

        public boolean registerType​(Class<?> type)
        Registers new type to be used with this converter instance.
        Parameters:
        type - Class type to register
        Returns:
        true if type was registed, else false (in case type was registered already or type is not eligible for registering ie. missing required annotations)
      • disableDeserializationOption

        public void disableDeserializationOption​(DeserializationFeature option)
        Removes (disables) existing deserialization option.
        Parameters:
        option - DeserializationFeature feature to disable
      • disableSerializationOption

        public void disableSerializationOption​(SerializationFeature option)
        Removes (disables) existing serialization option.
        Parameters:
        option - SerializationFeature feature to disable