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
This can e.g. be accomplished by using the
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
ValueInstantiatorcannot create a new instance of the Class, then we will useObjenesisto create an instance of the Class
BE AWARE: If the object is created/instantiated usingObjenesisthen 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.ObjectMappercreateObjectMapper(com.fasterxml.jackson.databind.Module... additionalModules) Convenience method for creating a newObjectMapperwith ourEssentialsImmutableJacksonModuleadded 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.voidsetupModule(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, versionMethods inherited from class com.fasterxml.jackson.databind.Module
getDependencies
-
Constructor Details
-
EssentialsImmutableJacksonModule
public EssentialsImmutableJacksonModule()Configure the Jackson Module
-
-
Method Details
-
setupModule
public void setupModule(com.fasterxml.jackson.databind.Module.SetupContext context) - Overrides:
setupModulein classcom.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 newObjectMapperwith ourEssentialsImmutableJacksonModuleadded 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 theObjectMapperinstance created- Returns:
- new
ObjectMapperinstance configured with sensible defaults and theEssentialsImmutableJacksonModuleadded
-