Package com.github.jasminb.jsonapi
Class ResourceConverter
- java.lang.Object
-
- com.github.jasminb.jsonapi.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 Summary
Constructors Constructor Description ResourceConverter(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?>... classes)ResourceConverter(com.fasterxml.jackson.databind.ObjectMapper mapper, String baseURL, Class<?>... classes)Creates new ResourceConverter.ResourceConverter(Class<?>... classes)Creates new ResourceConverter.ResourceConverter(String baseURL, Class<?>... classes)Creates new ResourceConverter.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voiddisableDeserializationOption(DeserializationFeature option)Removes (disables) existing deserialization option.voiddisableSerializationOption(SerializationFeature option)Removes (disables) existing serialization option.voidenableDeserializationOption(DeserializationFeature option)Adds (enables) new deserialization option.voidenableSerializationOption(SerializationFeature option)Adds (enables) new serialization option.booleanisRegisteredType(Class<?> type)Checks if provided type is registered with this converter instance.<T> JSONAPIDocument<T>readDocument(byte[] data, Class<T> clazz)Reads JSON API spec document and converts it into target type.<T> JSONAPIDocument<T>readDocument(InputStream dataStream, Class<T> clazz)Reads JSON API spec document and converts it into target type.<T> JSONAPIDocument<List<T>>readDocumentCollection(byte[] data, Class<T> clazz)Reads JSON API spec document and converts it into collection of target type objects.<T> JSONAPIDocument<List<T>>readDocumentCollection(InputStream dataStream, Class<T> clazz)Reads JSON API spec document and converts it into collection of target type objects.<T> TreadObject(byte[] data, Class<T> clazz)Deprecated.<T> List<T>readObjectCollection(byte[] data, Class<T> clazz)Deprecated.booleanregisterType(Class<?> type)Registers new type to be used with this converter instance.voidsetGlobalResolver(RelationshipResolver resolver)Registers global relationship resolver.voidsetTypeResolver(RelationshipResolver resolver, Class<?> type)Registers relationship resolver for given type.byte[]writeDocument(JSONAPIDocument<?> document)Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.byte[]writeDocument(JSONAPIDocument<?> document, SerializationSettings settings)Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.byte[]writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection)Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.byte[]writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection, SerializationSettings serializationSettings)Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.byte[]writeObject(Object object)Deprecated.<T> byte[]writeObjectCollection(Iterable<T> objects)Deprecated.use writeDocumentCollection instead
-
-
-
Constructor Detail
-
ResourceConverter
public ResourceConverter(Class<?>... classes)
Creates new ResourceConverter.All classes that should be handled by instance of
ResourceConvertermust be registered when creating a new instance of it.- Parameters:
classes-Classarray 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
ResourceConvertermust be registered when creating a new instance of it.
-
ResourceConverter
public ResourceConverter(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?>... classes)
-
-
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 theincludedsection 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 instancetype- 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 dataclazz- 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 inputclazz- 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-byteraw data (server response)clazz-Classtarget 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-byteraw dataStream (server response)clazz-Classtarget 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-byteraw data (server response)clazz-Classtarget 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-InputStreaminput streamclazz-Classtarget 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.JsonProcessingExceptionIllegalAccessException
-
writeDocument
public byte[] writeDocument(JSONAPIDocument<?> document) throws DocumentSerializationException
Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.- Parameters:
document-JSONAPIDocumentdocument to serialize- Returns:
- serialized content in bytes
- Throws:
DocumentSerializationException- thrown in case serialization fails
-
writeDocument
public byte[] writeDocument(JSONAPIDocument<?> document, SerializationSettings settings) throws DocumentSerializationException
Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.- Parameters:
document-JSONAPIDocumentdocument to serializesettings-SerializationSettingssettings that override global serialization settings- Returns:
- serialized content in bytes
- Throws:
DocumentSerializationException- thrown in case serialization fails
-
writeDocumentCollection
public byte[] writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection) throws DocumentSerializationException
Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.- Parameters:
documentCollection-JSONAPIDocumentdocument collection to serialize- Returns:
- serialized content in bytes
- Throws:
DocumentSerializationException- thrown in case serialization fails
-
writeDocumentCollection
public byte[] writeDocumentCollection(JSONAPIDocument<? extends Iterable<?>> documentCollection, SerializationSettings serializationSettings) throws DocumentSerializationException
Serializes providedJSONAPIDocumentinto JSON API Spec compatible byte representation.- Parameters:
documentCollection-JSONAPIDocumentdocument collection to serializeserializationSettings-SerializationSettingssettings that override global serialization settings- Returns:
- serialized content in bytes
- Throws:
DocumentSerializationException- thrown in case serialization fails
-
writeObjectCollection
@Deprecated public <T> byte[] writeObjectCollection(Iterable<T> objects) throws com.fasterxml.jackson.core.JsonProcessingException, IllegalAccessException
Deprecated.use writeDocumentCollection insteadConverts input object to byte array.- Parameters:
objects- List of input objects- Returns:
- raw bytes
- Throws:
com.fasterxml.jackson.core.JsonProcessingExceptionIllegalAccessException
-
isRegisteredType
public boolean isRegisteredType(Class<?> type)
Checks if provided type is registered with this converter instance.- Parameters:
type- class to check- Returns:
- returns
trueif type is registered, elsefalse
-
registerType
public boolean registerType(Class<?> type)
Registers new type to be used with this converter instance.- Parameters:
type-Classtype to register- Returns:
trueif type was registed, elsefalse(in case type was registered already or type is not eligible for registering ie. missing required annotations)
-
enableDeserializationOption
public void enableDeserializationOption(DeserializationFeature option)
Adds (enables) new deserialization option.- Parameters:
option-DeserializationFeatureoption
-
disableDeserializationOption
public void disableDeserializationOption(DeserializationFeature option)
Removes (disables) existing deserialization option.- Parameters:
option-DeserializationFeaturefeature to disable
-
enableSerializationOption
public void enableSerializationOption(SerializationFeature option)
Adds (enables) new serialization option.- Parameters:
option-SerializationFeatureoption
-
disableSerializationOption
public void disableSerializationOption(SerializationFeature option)
Removes (disables) existing serialization option.- Parameters:
option-SerializationFeaturefeature to disable
-
-