Class ElementMapper
java.lang.Object
org.gcube.informationsystem.serialization.ElementMapper
A central class for handling JSON serialization and deserialization of
Element types.
This class configures a Jackson ObjectMapper with custom settings
for the Information System model, including subtype registration and fallback
mechanisms for unknown types.
- Author:
- Luca Frosini (ISTI - CNR)
-
Field Summary
FieldsModifier and TypeFieldDescriptionA map of interfaces to their implementation classes.A map of known types, keyed by type name.protected static final org.gcube.com.fasterxml.jackson.databind.ObjectMapperThe object mapper. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <El extends Element,ELImpl extends El>
voidaddDynamicAssociation(Class<El> interfaceClz, Class<ELImpl> implementationClass) Adds a dynamic mapping between an interface and its implementation class.protected static org.gcube.com.fasterxml.jackson.databind.node.ArrayNodeanalizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Analyzes the types in an array node.protected static org.gcube.com.fasterxml.jackson.databind.JsonNodeanalizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode) Analyzes the types in an object node.protected static StringBufferReturns an error message for an unknown type.static org.gcube.com.fasterxml.jackson.databind.ObjectMapperReturns the configuredObjectMapperinstance.marshal(El object) Serializes an object to a JSON string.marshal(El[] array) Serializes an array of objects to a JSON string.static <T extends OutputStream,El extends Element>
Tmarshal(El object, T stream) Serializes an object to anOutputStream.Serializes a list of objects to a JSON string.static <El extends Element>
voidregisterSubtype(Class<El> clz) Registers a subtype with the object mapper.protected static <El extends Element,ELImpl extends El>
voidregisterSubtypes(Class<El> interfaceClz, Class<ELImpl> implementationClass) Registers a subtype with the object mapper.protected static org.gcube.com.fasterxml.jackson.databind.node.ObjectNodesetTypeToBestAvailable(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode) Sets the type of an object to the best available supertype.static <El extends Element>
Elunmarshal(Class<El> clz, InputStream stream) Deserializes an object from anInputStream.static <El extends Element>
ElDeserializes an object from aReader.static <El extends Element>
ElDeserializes an object from a JSON string.unmarshalList(Class<El> clz, String string) Deserializes a list of objects from a JSON string.unmarshalList(String string) Deserializes a list of objects from a JSON string.
-
Field Details
-
mapper
protected static final org.gcube.com.fasterxml.jackson.databind.ObjectMapper mapperThe object mapper. -
knownTypes
A map of known types, keyed by type name. -
interfaceToImplementation
protected static final Map<Class<? extends Element>,Class<? extends Element>> interfaceToImplementationA map of interfaces to their implementation classes.
-
-
Constructor Details
-
ElementMapper
public ElementMapper()
-
-
Method Details
-
getObjectMapper
public static org.gcube.com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Returns the configuredObjectMapperinstance.- Returns:
- The singleton
ObjectMapper.
-
addDynamicAssociation
public static <El extends Element,ELImpl extends El> void addDynamicAssociation(Class<El> interfaceClz, Class<ELImpl> implementationClass) Adds a dynamic mapping between an interface and its implementation class.- Type Parameters:
El- The interface type.ELImpl- The implementation type.- Parameters:
interfaceClz- The interface class.implementationClass- The implementation class.
-
registerSubtypes
protected static <El extends Element,ELImpl extends El> void registerSubtypes(Class<El> interfaceClz, Class<ELImpl> implementationClass) Registers a subtype with the object mapper.- Type Parameters:
El- The element type.ELImpl- The implementation type.- Parameters:
interfaceClz- The interface class.implementationClass- The implementation class.
-
registerSubtype
Registers a subtype with the object mapper.- Type Parameters:
El- The element type.- Parameters:
clz- The class to register.
-
marshal
public static <T extends OutputStream,El extends Element> T marshal(El object, T stream) throws org.gcube.com.fasterxml.jackson.core.JsonGenerationException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Serializes an object to anOutputStream.- Type Parameters:
T- The type of the output stream.El- The type of the element.- Parameters:
object- The object to serialize.stream- The output stream.- Returns:
- The output stream.
- Throws:
IOException- if an error occurs during serialization.org.gcube.com.fasterxml.jackson.core.JsonGenerationException- if an error occurs during JSON generation.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
marshal
public static <T extends Writer,El extends Element> T marshal(El object, T writer) throws org.gcube.com.fasterxml.jackson.core.JsonGenerationException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Serializes an object to aWriter.- Type Parameters:
T- The type of the writer.El- The type of the element.- Parameters:
object- The object to serialize.writer- The writer.- Returns:
- The writer.
- Throws:
IOException- if an error occurs during serialization.org.gcube.com.fasterxml.jackson.core.JsonGenerationException- if an error occurs during JSON generation.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
marshal
public static <El extends Element> String marshal(El object) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException Serializes an object to a JSON string.- Type Parameters:
El- The type of the element.- Parameters:
object- The object to serialize.- Returns:
- The JSON string.
- Throws:
org.gcube.com.fasterxml.jackson.core.JsonProcessingException- if an error occurs during serialization.
-
marshal
public static <El extends Element> String marshal(List<El> list) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException Serializes a list of objects to a JSON string.- Type Parameters:
El- The type of the elements in the list.- Parameters:
list- The list to serialize.- Returns:
- The JSON string.
- Throws:
org.gcube.com.fasterxml.jackson.core.JsonProcessingException- if an error occurs during serialization.
-
marshal
public static <El extends Element> String marshal(El[] array) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException Serializes an array of objects to a JSON string.- Type Parameters:
El- The type of the elements in the array.- Parameters:
array- The array to serialize.- Returns:
- The JSON string.
- Throws:
org.gcube.com.fasterxml.jackson.core.JsonProcessingException- if an error occurs during serialization.
-
getError
Returns an error message for an unknown type.- Parameters:
unknownType- The unknown type.- Returns:
- The error message.
-
setTypeToBestAvailable
protected static org.gcube.com.fasterxml.jackson.databind.node.ObjectNode setTypeToBestAvailable(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode) Sets the type of an object to the best available supertype.- Parameters:
objectNode- The object node.- Returns:
- The object node with the updated type.
-
analizeTypes
protected static org.gcube.com.fasterxml.jackson.databind.JsonNode analizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode) Analyzes the types in an object node.- Parameters:
objectNode- The object node.- Returns:
- The analyzed object node.
-
analizeTypes
protected static org.gcube.com.fasterxml.jackson.databind.node.ArrayNode analizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Analyzes the types in an array node.- Parameters:
arrayNode- The array node.- Returns:
- The analyzed array node.
-
unmarshal
public static <El extends Element> El unmarshal(Class<El> clz, Reader reader) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Deserializes an object from aReader.- Type Parameters:
El- The type of the element.- Parameters:
clz- The class of the element.reader- The reader.- Returns:
- The deserialized object.
- Throws:
IOException- if an error occurs during deserialization.org.gcube.com.fasterxml.jackson.core.JsonParseException- if an error occurs during JSON parsing.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
unmarshal
public static <El extends Element> El unmarshal(Class<El> clz, InputStream stream) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Deserializes an object from anInputStream.- Type Parameters:
El- The type of the element.- Parameters:
clz- The class of the element.stream- The input stream.- Returns:
- The deserialized object.
- Throws:
IOException- if an error occurs during deserialization.org.gcube.com.fasterxml.jackson.core.JsonParseException- if an error occurs during JSON parsing.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
unmarshal
public static <El extends Element> El unmarshal(Class<El> clz, String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Deserializes an object from a JSON string.- Type Parameters:
El- The type of the element.- Parameters:
clz- The class of the element.string- The JSON string.- Returns:
- The deserialized object.
- Throws:
IOException- if an error occurs during deserialization.org.gcube.com.fasterxml.jackson.core.JsonParseException- if an error occurs during JSON parsing.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
unmarshalList
public static <El extends Element> List<El> unmarshalList(Class<El> clz, String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Deserializes a list of objects from a JSON string.- Type Parameters:
El- The type of the elements in the list.- Parameters:
clz- The class of the elements.string- The JSON string.- Returns:
- The deserialized list.
- Throws:
IOException- if an error occurs during deserialization.org.gcube.com.fasterxml.jackson.core.JsonParseException- if an error occurs during JSON parsing.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-
unmarshalList
public static <El extends Element> List<El> unmarshalList(String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException Deserializes a list of objects from a JSON string.- Type Parameters:
El- The type of the elements in the list.- Parameters:
string- The JSON string.- Returns:
- The deserialized list.
- Throws:
IOException- if an error occurs during deserialization.org.gcube.com.fasterxml.jackson.core.JsonParseException- if an error occurs during JSON parsing.org.gcube.com.fasterxml.jackson.databind.JsonMappingException- if an error occurs during JSON mapping.
-