Class EssentialsImmutableJacksonModule

java.lang.Object
com.fasterxml.jackson.databind.Module
com.fasterxml.jackson.databind.module.SimpleModule
dk.cloudcreate.essentials.jackson.immutable.EssentialsImmutableJacksonModule
All Implemented Interfaces:
com.fasterxml.jackson.core.Versioned, Serializable

public final class EssentialsImmutableJacksonModule extends com.fasterxml.jackson.databind.module.SimpleModule
The Jackson module that assists Jackson support for deserializing immutable classes or other classes that don't have a suitable creator (constructor, or no-arg static factory method, etc.).

This is very useful for when you're using Record's (in Java 14+) or other types supporting immutable objects, as it allows Jackson to create an object instance without requiring a matching constructing.
Property/Field values are set directly using reflection, even if the fields themselves are final.

For this to work we require that opinionated defaults, such as using FIELDS for serialization, have been applied to the ObjectMapper instance.
This can e.g. be accomplished by using the createObjectMapper(Module...) method

The object creation/instantiation logic is as follows:

  • First it will try using the standard Jackson ValueInstantiator
  • IF the standard Jackson ValueInstantiator cannot create a new instance of the Class, then we will use Objenesis to create an instance of the Class
    BE AWARE: If the object is created/instantiated using Objenesis then NO constructor will be called and NO fields will have their default values set by Java!!!!
See Also:
  • Nested Class Summary

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

    com.fasterxml.jackson.databind.Module.SetupContext
  • Field Summary

    Fields inherited from class com.fasterxml.jackson.databind.module.SimpleModule

    _abstractTypes, _deserializerModifier, _deserializers, _hasExplicitName, _keyDeserializers, _keySerializers, _mixins, _name, _namingStrategy, _serializerModifier, _serializers, _subtypes, _valueInstantiators, _version
  • Constructor Summary

    Constructors
    Constructor
    Description
    Configure the Jackson Module
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.databind.ObjectMapper
    createObjectMapper(com.fasterxml.jackson.databind.Module... additionalModules)
    Convenience method for creating a new ObjectMapper with our EssentialsImmutableJacksonModule added and with opinionated defaults like explicit JSON mapping, not failing on unknown properties or empty beans, propagation of transient markers, using FIELDS for serialization (i.e. disabling auto detection of getters and setters and instead using field and allowing ANY visibility for fields and constructors and none for getters/setters)
    void
    setupModule(com.fasterxml.jackson.databind.Module.SetupContext context)
     

    Methods inherited from class com.fasterxml.jackson.databind.module.SimpleModule

    _checkNotNull, addAbstractTypeMapping, addDeserializer, addKeyDeserializer, addKeySerializer, addSerializer, addSerializer, addValueInstantiator, getModuleName, getTypeId, registerSubtypes, registerSubtypes, registerSubtypes, setAbstractTypes, setDeserializerModifier, setDeserializers, setKeyDeserializers, setKeySerializers, setMixInAnnotation, setNamingStrategy, setSerializerModifier, setSerializers, setValueInstantiators, version

    Methods inherited from class com.fasterxml.jackson.databind.Module

    getDependencies

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • EssentialsImmutableJacksonModule

      public EssentialsImmutableJacksonModule()
      Configure the Jackson Module
  • Method Details

    • setupModule

      public void setupModule(com.fasterxml.jackson.databind.Module.SetupContext context)
      Overrides:
      setupModule in class com.fasterxml.jackson.databind.module.SimpleModule
    • createObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper createObjectMapper(com.fasterxml.jackson.databind.Module... additionalModules)
      Convenience method for creating a new ObjectMapper with our EssentialsImmutableJacksonModule added and with opinionated defaults like explicit JSON mapping, not failing on unknown properties or empty beans, propagation of transient markers, using FIELDS for serialization (i.e. disabling auto detection of getters and setters and instead using field and allowing ANY visibility for fields and constructors and none for getters/setters)
      Parameters:
      additionalModules - Additional modules that should be added to the ObjectMapper instance created
      Returns:
      new ObjectMapper instance configured with sensible defaults and the EssentialsImmutableJacksonModule added