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.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 ValueInstantiator provided via the constructor
  • Only 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 default field values will be applied!!!!
  • Constructor Details

    • ImmutableObjectsValueInstantiator

      public ImmutableObjectsValueInstantiator(Class<?> typeToInstantiate, com.fasterxml.jackson.databind.deser.ValueInstantiator defaultJacksonInstantiator)
  • Method Details

    • canCreateUsingDefault

      public boolean canCreateUsingDefault()
      Overrides:
      canCreateUsingDefault in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • createUsingDefault

      public Object createUsingDefault(com.fasterxml.jackson.databind.DeserializationContext context) throws IOException
      Overrides:
      createUsingDefault in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • getValueClass

      public Class<?> getValueClass()
      Overrides:
      getValueClass in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getValueTypeDesc

      public String getValueTypeDesc()
      Overrides:
      getValueTypeDesc in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canInstantiate

      public boolean canInstantiate()
      Overrides:
      canInstantiate in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromString

      public boolean canCreateFromString()
      Overrides:
      canCreateFromString in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromInt

      public boolean canCreateFromInt()
      Overrides:
      canCreateFromInt in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromLong

      public boolean canCreateFromLong()
      Overrides:
      canCreateFromLong in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromDouble

      public boolean canCreateFromDouble()
      Overrides:
      canCreateFromDouble in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromBoolean

      public boolean canCreateFromBoolean()
      Overrides:
      canCreateFromBoolean in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateUsingDelegate

      public boolean canCreateUsingDelegate()
      Overrides:
      canCreateUsingDelegate in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateUsingArrayDelegate

      public boolean canCreateUsingArrayDelegate()
      Overrides:
      canCreateUsingArrayDelegate in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • canCreateFromObjectWith

      public boolean canCreateFromObjectWith()
      Overrides:
      canCreateFromObjectWith in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getFromObjectArguments

      public com.fasterxml.jackson.databind.deser.SettableBeanProperty[] getFromObjectArguments(com.fasterxml.jackson.databind.DeserializationConfig config)
      Overrides:
      getFromObjectArguments in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getDelegateType

      public com.fasterxml.jackson.databind.JavaType getDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config)
      Overrides:
      getDelegateType in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getArrayDelegateType

      public com.fasterxml.jackson.databind.JavaType getArrayDelegateType(com.fasterxml.jackson.databind.DeserializationConfig config)
      Overrides:
      getArrayDelegateType in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • createFromObjectWith

      public Object createFromObjectWith(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object[] args) throws IOException
      Overrides:
      createFromObjectWith in class com.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:
      createFromObjectWith in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createUsingDelegate

      public Object createUsingDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) throws IOException
      Overrides:
      createUsingDelegate in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createUsingArrayDelegate

      public Object createUsingArrayDelegate(com.fasterxml.jackson.databind.DeserializationContext ctxt, Object delegate) throws IOException
      Overrides:
      createUsingArrayDelegate in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createFromString

      public Object createFromString(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) throws IOException
      Overrides:
      createFromString in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createFromInt

      public Object createFromInt(com.fasterxml.jackson.databind.DeserializationContext ctxt, int value) throws IOException
      Overrides:
      createFromInt in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createFromLong

      public Object createFromLong(com.fasterxml.jackson.databind.DeserializationContext ctxt, long value) throws IOException
      Overrides:
      createFromLong in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createFromDouble

      public Object createFromDouble(com.fasterxml.jackson.databind.DeserializationContext ctxt, double value) throws IOException
      Overrides:
      createFromDouble in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • createFromBoolean

      public Object createFromBoolean(com.fasterxml.jackson.databind.DeserializationContext ctxt, boolean value) throws IOException
      Overrides:
      createFromBoolean in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException
    • getDefaultCreator

      public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getDefaultCreator()
      Overrides:
      getDefaultCreator in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getDelegateCreator

      public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getDelegateCreator()
      Overrides:
      getDelegateCreator in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getArrayDelegateCreator

      public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getArrayDelegateCreator()
      Overrides:
      getArrayDelegateCreator in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • getWithArgsCreator

      public com.fasterxml.jackson.databind.introspect.AnnotatedWithParams getWithArgsCreator()
      Overrides:
      getWithArgsCreator in class com.fasterxml.jackson.databind.deser.ValueInstantiator
    • _createFromStringFallbacks

      protected Object _createFromStringFallbacks(com.fasterxml.jackson.databind.DeserializationContext ctxt, String value) throws IOException
      Overrides:
      _createFromStringFallbacks in class com.fasterxml.jackson.databind.deser.ValueInstantiator
      Throws:
      IOException