Package org.dice_research.serial.maps
Class ComplexMapSerializer
- java.lang.Object
-
- com.fasterxml.jackson.databind.JsonSerializer<T>
-
- com.fasterxml.jackson.databind.ser.std.StdSerializer<Map>
-
- org.dice_research.serial.maps.ComplexMapSerializer
-
- All Implemented Interfaces:
com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable,com.fasterxml.jackson.databind.jsonschema.SchemaAware,Serializable
public class ComplexMapSerializer extends com.fasterxml.jackson.databind.ser.std.StdSerializer<Map>
This class can serialize instances of theMapinterface even if its elements have complex objects as key. A serialized map object with[key1 -> value1, key2 -> value2]may look like the following:{ "a"="main key class", "b"="main value class", "c"=[{ "k"={ key1 object }, "v"={ value1 object } },{ "k"={ key2 object }, "v"={ value2 object } }] }Note that the main key and value classes are determined based on the given map. In case different class instances are found within the map, the class that has the most instances is used as main class. All elements of the map with a differing class are stored with additional information as follows:{ "a"="class of the key (only if different from the main class)", "b"="class of the value (only if different from the main class)", "k"={ key1 object }, "v"={ value1 object } }- Author:
- Michael Röder (michael.roeder@uni-paderborn.de)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringARRAY_FIELDstatic StringKEY_FIELDstatic StringKEY_TYPE_FIELDstatic StringVALUE_FIELDstatic StringVALUE_TYPE_FIELD
-
Constructor Summary
Constructors Constructor Description ComplexMapSerializer()Constructor.ComplexMapSerializer(Class<Map> t)Constructor taking additional type that this serializer can process.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Class<?>determineKeyClass(Map map)Method that determines the main key class.protected Class<?>determineMainClass(Stream<?> stream)Method that determines the main class of a stream of objects.protected Class<?>determineValueClass(Map map)Method that determines the main value class.voidserialize(Map map, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider)protected voidwriteElement(Object key, Class mainKeyClass, Object value, Class mainValueClass, com.fasterxml.jackson.core.JsonGenerator gen)This method writes a single element from the map (i.e., a key value pair).protected voidwriteType(Class<?> clazz, boolean isKeyType, com.fasterxml.jackson.core.JsonGenerator gen)A simple method that writes the given class to the JSON generator.-
Methods inherited from class com.fasterxml.jackson.databind.ser.std.StdSerializer
_neitherNull, _nonEmpty, acceptJsonFormatVisitor, createSchemaNode, createSchemaNode, findAnnotatedContentSerializer, findContextualConvertingSerializer, findConvertingContentSerializer, findFormatFeature, findFormatOverrides, findIncludeOverrides, findPropertyFilter, getSchema, getSchema, handledType, isDefaultSerializer, visitArrayFormat, visitArrayFormat, visitFloatFormat, visitIntFormat, visitIntFormat, visitStringFormat, visitStringFormat, wrapAndThrow, wrapAndThrow
-
-
-
-
Field Detail
-
KEY_FIELD
public static final String KEY_FIELD
- See Also:
- Constant Field Values
-
VALUE_FIELD
public static final String VALUE_FIELD
- See Also:
- Constant Field Values
-
KEY_TYPE_FIELD
public static final String KEY_TYPE_FIELD
- See Also:
- Constant Field Values
-
VALUE_TYPE_FIELD
public static final String VALUE_TYPE_FIELD
- See Also:
- Constant Field Values
-
ARRAY_FIELD
public static final String ARRAY_FIELD
- See Also:
- Constant Field Values
-
-
Method Detail
-
serialize
public void serialize(Map map, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException
- Specified by:
serializein classcom.fasterxml.jackson.databind.ser.std.StdSerializer<Map>- Throws:
IOException
-
determineKeyClass
protected Class<?> determineKeyClass(Map map)
Method that determines the main key class.- Parameters:
map- the map that should be serialized- Returns:
- the
Classinstance that represents the highest number of keys in the map
-
determineValueClass
protected Class<?> determineValueClass(Map map)
Method that determines the main value class.- Parameters:
map- the map that should be serialized- Returns:
- the
Classinstance that represents the highest number of values in the map
-
determineMainClass
protected Class<?> determineMainClass(Stream<?> stream)
Method that determines the main class of a stream of objects.- Parameters:
stream- a stream of objects for which the main class should be determined- Returns:
- the
Classinstance that represents the highest number of objects in the stream
-
writeType
protected void writeType(Class<?> clazz, boolean isKeyType, com.fasterxml.jackson.core.JsonGenerator gen) throws IOException
A simple method that writes the given class to the JSON generator.- Parameters:
clazz- the class that should be writtenisKeyType- a flag indicating whether it is a key class (true) or a value class (false)gen- the JSON generator instance which is used to create the JSON- Throws:
IOException- if the generator throws an exception
-
writeElement
protected void writeElement(Object key, Class mainKeyClass, Object value, Class mainValueClass, com.fasterxml.jackson.core.JsonGenerator gen) throws IOException
This method writes a single element from the map (i.e., a key value pair).- Parameters:
key- the key that should be serializedmainKeyClass- the main class of keys in the mapvalue- the value that should be serializedmainValueClass- the main class of the values in the mapgen- the JSON generator instance which is used to create the JSON- Throws:
IOException- if the generator throws an exception
-
-