Class 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 the Map interface 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
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.JsonSerializer

        com.fasterxml.jackson.databind.JsonSerializer.None
    • 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.
      void serialize​(Map map, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider)  
      protected void writeElement​(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 void writeType​(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
      • Methods inherited from class com.fasterxml.jackson.databind.JsonSerializer

        getDelegatee, isEmpty, isEmpty, isUnwrappingSerializer, properties, replaceDelegatee, serializeWithType, unwrappingSerializer, usesObjectId, withFilterId
    • Constructor Detail

      • ComplexMapSerializer

        public ComplexMapSerializer()
        Constructor.
      • ComplexMapSerializer

        public ComplexMapSerializer​(Class<Map> t)
        Constructor taking additional type that this serializer can process.
        Parameters:
        t - Nominal type supported, usually declared type of property for which serializer is used.
    • Method Detail

      • serialize

        public void serialize​(Map map,
                              com.fasterxml.jackson.core.JsonGenerator gen,
                              com.fasterxml.jackson.databind.SerializerProvider provider)
                       throws IOException
        Specified by:
        serialize in class com.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 Class instance 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 Class instance 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 Class instance 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 written
        isKeyType - 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 serialized
        mainKeyClass - the main class of keys in the map
        value - the value that should be serialized
        mainValueClass - the main class of the values in the map
        gen - the JSON generator instance which is used to create the JSON
        Throws:
        IOException - if the generator throws an exception