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.SimpleModuleThe 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 theObjectMapperinstance.
This can e.g. be accomplished by using thecreateObjectMapper(Module...)methodThe 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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description EssentialsImmutableJacksonModule()Configure the Jackson Module
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static 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, version
-
-
-
-
Method Detail
-
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
-
-