Class ImmutableObjectsValueInstantiator
java.lang.Object
com.fasterxml.jackson.databind.deser.ValueInstantiator
dk.cloudcreate.essentials.jackson.immutable.ImmutableObjectsValueInstantiator
public final class ImmutableObjectsValueInstantiator
extends com.fasterxml.jackson.databind.deser.ValueInstantiator
ValueInstantiator for Jackson that allows for deserializing of 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 of immutable classes, 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 applied to the
ObjectMapper instance.This can e.g. be accomplished by using the
EssentialsImmutableJacksonModule.createObjectMapper(Module...) method
The object creation/instantiation logic is as follows:
- First it will try using the standard Jackson
ValueInstantiatorprovided via the constructor - Only 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 using Objenesis then NO constructor will be called and NO default field values will be applied!!!!
-
Nested Class Summary
Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.deser.ValueInstantiator
com.fasterxml.jackson.databind.deser.ValueInstantiator.Base, com.fasterxml.jackson.databind.deser.ValueInstantiator.Delegating, com.fasterxml.jackson.databind.deser.ValueInstantiator.Gettable -
Constructor Summary
ConstructorsConstructorDescriptionImmutableObjectsValueInstantiator(Class<?> typeToInstantiate, com.fasterxml.jackson.databind.deser.ValueInstantiator defaultJacksonInstantiator) -
Method Summary
Modifier and TypeMethodDescriptionprotected Object_createFromStringFallbacks(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) booleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanbooleanbooleancreateFromBoolean(com.fasterxml.jackson.databind.DeserializationContext ctxt, boolean value) createFromDouble(com.fasterxml.jackson.databind.DeserializationContext ctxt, double value) createFromInt(com.fasterxml.jackson.databind.DeserializationContext ctxt, int value) createFromLong(com.fasterxml.jackson.databind.DeserializationContext ctxt, long value) createFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, com.fasterxml.jackson.databind.deser.SettableBeanProperty[] props, com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer buffer) createFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object[] args) createFromString(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) createUsingArrayDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) createUsingDefault(com.fasterxml.jackson.databind.DeserializationContext context) createUsingDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) com.fasterxml.jackson.databind.introspect.AnnotatedWithParamscom.fasterxml.jackson.databind.JavaTypegetArrayDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config) com.fasterxml.jackson.databind.introspect.AnnotatedWithParamscom.fasterxml.jackson.databind.introspect.AnnotatedWithParamscom.fasterxml.jackson.databind.JavaTypegetDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config) com.fasterxml.jackson.databind.deser.SettableBeanProperty[]getFromObjectArguments(com.fasterxml.jackson.databind.DeserializationConfig config) Class<?>com.fasterxml.jackson.databind.introspect.AnnotatedWithParamsMethods inherited from class com.fasterxml.jackson.databind.deser.ValueInstantiator
canCreateFromBigDecimal, canCreateFromBigInteger, createContextual, createFromBigDecimal, createFromBigInteger, createUsingDefaultOrWithoutArguments
-
Constructor Details
-
ImmutableObjectsValueInstantiator
public ImmutableObjectsValueInstantiator(Class<?> typeToInstantiate, com.fasterxml.jackson.databind.deser.ValueInstantiator defaultJacksonInstantiator)
-
-
Method Details
-
canCreateUsingDefault
public boolean canCreateUsingDefault()- Overrides:
canCreateUsingDefaultin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
createUsingDefault
public Object createUsingDefault(com.fasterxml.jackson.databind.DeserializationContext context) throws IOException - Overrides:
createUsingDefaultin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
getValueClass
- Overrides:
getValueClassin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getValueTypeDesc
- Overrides:
getValueTypeDescin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canInstantiate
public boolean canInstantiate()- Overrides:
canInstantiatein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromString
public boolean canCreateFromString()- Overrides:
canCreateFromStringin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromInt
public boolean canCreateFromInt()- Overrides:
canCreateFromIntin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromLong
public boolean canCreateFromLong()- Overrides:
canCreateFromLongin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromDouble
public boolean canCreateFromDouble()- Overrides:
canCreateFromDoublein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromBoolean
public boolean canCreateFromBoolean()- Overrides:
canCreateFromBooleanin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateUsingDelegate
public boolean canCreateUsingDelegate()- Overrides:
canCreateUsingDelegatein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateUsingArrayDelegate
public boolean canCreateUsingArrayDelegate()- Overrides:
canCreateUsingArrayDelegatein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
canCreateFromObjectWith
public boolean canCreateFromObjectWith()- Overrides:
canCreateFromObjectWithin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getFromObjectArguments
public com.fasterxml.jackson.databind.deser.SettableBeanProperty[] getFromObjectArguments(com.fasterxml.jackson.databind.DeserializationConfig config) - Overrides:
getFromObjectArgumentsin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getDelegateType
public com.fasterxml.jackson.databind.JavaType getDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config) - Overrides:
getDelegateTypein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getArrayDelegateType
public com.fasterxml.jackson.databind.JavaType getArrayDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config) - Overrides:
getArrayDelegateTypein classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
createFromObjectWith
public Object createFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object[] args) throws IOException - Overrides:
createFromObjectWithin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromObjectWith
public Object createFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, com.fasterxml.jackson.databind.deser.SettableBeanProperty[] props, com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer buffer) throws IOException - Overrides:
createFromObjectWithin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createUsingDelegate
public Object createUsingDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) throws IOException - Overrides:
createUsingDelegatein classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createUsingArrayDelegate
public Object createUsingArrayDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) throws IOException - Overrides:
createUsingArrayDelegatein classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromString
public Object createFromString(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) throws IOException - Overrides:
createFromStringin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromInt
public Object createFromInt(com.fasterxml.jackson.databind.DeserializationContext ctxt, int value) throws IOException - Overrides:
createFromIntin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromLong
public Object createFromLong(com.fasterxml.jackson.databind.DeserializationContext ctxt, long value) throws IOException - Overrides:
createFromLongin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromDouble
public Object createFromDouble(com.fasterxml.jackson.databind.DeserializationContext ctxt, double value) throws IOException - Overrides:
createFromDoublein classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
createFromBoolean
public Object createFromBoolean(com.fasterxml.jackson.databind.DeserializationContext ctxt, boolean value) throws IOException - Overrides:
createFromBooleanin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-
getDefaultCreator
public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getDefaultCreator()- Overrides:
getDefaultCreatorin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getDelegateCreator
public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getDelegateCreator()- Overrides:
getDelegateCreatorin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getArrayDelegateCreator
public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getArrayDelegateCreator()- Overrides:
getArrayDelegateCreatorin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getWithArgsCreator
public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getWithArgsCreator()- Overrides:
getWithArgsCreatorin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
_createFromStringFallbacks
protected Object _createFromStringFallbacks(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) throws IOException - Overrides:
_createFromStringFallbacksin classcom.fasterxml.jackson.databind.deser.ValueInstantiator- Throws:
IOException
-