Class ImmutableObjectsValueInstantiator
- java.lang.Object
-
- com.fasterxml.jackson.databind.deser.ValueInstantiator
-
- dk.cloudcreate.essentials.jackson.immutable.ImmutableObjectsValueInstantiator
-
public class ImmutableObjectsValueInstantiator extends com.fasterxml.jackson.databind.deser.ValueInstantiatorValueInstantiatorfor 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 theObjectMapperinstance.
This can e.g. be accomplished by using theEssentialsImmutableJacksonModule.createObjectMapper(Module...)methodThe 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!!!!
- First it will try using the standard Jackson
-
-
Constructor Summary
Constructors Constructor Description ImmutableObjectsValueInstantiator(Class<?> typeToInstantiate, com.fasterxml.jackson.databind.deser.ValueInstantiator defaultJacksonInstantiator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Object_createFromStringFallbacks(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value)booleancanCreateFromBoolean()booleancanCreateFromDouble()booleancanCreateFromInt()booleancanCreateFromLong()booleancanCreateFromObjectWith()booleancanCreateFromString()booleancanCreateUsingArrayDelegate()booleancanCreateUsingDefault()booleancanCreateUsingDelegate()booleancanInstantiate()ObjectcreateFromBoolean(com.fasterxml.jackson.databind.DeserializationContext ctxt, boolean value)ObjectcreateFromDouble(com.fasterxml.jackson.databind.DeserializationContext ctxt, double value)ObjectcreateFromInt(com.fasterxml.jackson.databind.DeserializationContext ctxt, int value)ObjectcreateFromLong(com.fasterxml.jackson.databind.DeserializationContext ctxt, long value)ObjectcreateFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, com.fasterxml.jackson.databind.deser.SettableBeanProperty[] props, com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer buffer)ObjectcreateFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object[] args)ObjectcreateFromString(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value)ObjectcreateUsingArrayDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate)ObjectcreateUsingDefault(com.fasterxml.jackson.databind.DeserializationContext context)ObjectcreateUsingDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate)com.fasterxml.jackson.databind.introspect.AnnotatedWithParamsgetArrayDelegateCreator()com.fasterxml.jackson.databind.JavaTypegetArrayDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config)com.fasterxml.jackson.databind.introspect.AnnotatedWithParamsgetDefaultCreator()com.fasterxml.jackson.databind.introspect.AnnotatedWithParamsgetDelegateCreator()com.fasterxml.jackson.databind.JavaTypegetDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config)com.fasterxml.jackson.databind.deser.SettableBeanProperty[]getFromObjectArguments(com.fasterxml.jackson.databind.DeserializationConfig config)Class<?>getValueClass()StringgetValueTypeDesc()com.fasterxml.jackson.databind.introspect.AnnotatedWithParamsgetWithArgsCreator()
-
-
-
Constructor Detail
-
ImmutableObjectsValueInstantiator
public ImmutableObjectsValueInstantiator(Class<?> typeToInstantiate, com.fasterxml.jackson.databind.deser.ValueInstantiator defaultJacksonInstantiator)
-
-
Method Detail
-
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
public Class<?> getValueClass()
- Overrides:
getValueClassin classcom.fasterxml.jackson.databind.deser.ValueInstantiator
-
getValueTypeDesc
public String 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
-
-