Class GeneratedSerdeInclusionUtil

java.lang.Object
io.micronaut.serde.util.GeneratedSerdeInclusionUtil

@Internal public final class GeneratedSerdeInclusionUtil extends Object
Inclusion helpers used by generated serializers so that the global micronaut.serde.serialization.inclusion setting is honored without falling back to the runtime object serializer for every simple shape.

The active inclusion is resolved once, when a generated serializer is created, and is stored in a generated field. The per-property methods take the already resolved inclusion so that no configuration lookup happens on the serialization hot path.

The scalar methods mirror the isEmpty / isAbsent / isDefault contract of the runtime serde that would otherwise write the value, so a generated serializer and the runtime object serializer agree for the same model and configuration.

Since:
3.2
  • Method Details

    • resolveInclusion

      public static SerdeConfig.SerInclude resolveInclusion(Serializer.EncoderContext context)
      Resolve the inclusion to apply for the given context. Invoked once per generated serializer instance, never on the per-property path.
      Parameters:
      context - The encoder context
      Returns:
      The active inclusion
    • includeAlways

      public static boolean includeAlways(SerdeConfig.SerInclude include)
      Whether the resolved inclusion writes every property, which lets generated serializers skip the per-property inclusion check entirely.
      Parameters:
      include - The resolved inclusion
      Returns:
      true if no property can be skipped
    • shouldSerialize

      public static boolean shouldSerialize(SerdeConfig.SerInclude include, Serializer.EncoderContext context, Serializer serializer, @Nullable Object value)
      Whether a property written by the given serializer should be included.
      Parameters:
      include - The resolved inclusion
      context - The encoder context
      serializer - The property serializer
      value - The property value
      Returns:
      true if the property should be serialized
    • shouldSerializePrimitive

      public static boolean shouldSerializePrimitive(SerdeConfig.SerInclude include, boolean isDefault)
      Whether a primitive property should be included. Primitive values are never null and never empty, so only NEVER and NON_DEFAULT can skip them.
      Parameters:
      include - The resolved inclusion
      isDefault - Whether the value equals the default the matching serde reports for its type
      Returns:
      true if the property should be serialized
    • shouldSerializeString

      public static boolean shouldSerializeString(SerdeConfig.SerInclude include, @Nullable String value)
      Whether a String property should be included. Mirrors StringSerde, which reports null and the empty string as empty and reports no default value.
      Parameters:
      include - The resolved inclusion
      value - The property value
      Returns:
      true if the property should be serialized
    • shouldSerializeNumber

      public static boolean shouldSerializeNumber(SerdeConfig.SerInclude include, @Nullable Number value)
      Whether a Number property should be included. Mirrors the number serdes, which report only null as empty and treat the boxed zero of their own type as the default value.
      Parameters:
      include - The resolved inclusion
      value - The property value
      Returns:
      true if the property should be serialized
    • shouldSerializeBoolean

      public static boolean shouldSerializeBoolean(SerdeConfig.SerInclude include, @Nullable Boolean value)
      Whether a Boolean property should be included. Mirrors BooleanSerde, which treats false as the default value.
      Parameters:
      include - The resolved inclusion
      value - The property value
      Returns:
      true if the property should be serialized
    • shouldSerializeCharacter

      public static boolean shouldSerializeCharacter(SerdeConfig.SerInclude include, @Nullable Character value)
      Whether a Character property should be included. Mirrors CharSerde, which treats the NUL character as the default value.
      Parameters:
      include - The resolved inclusion
      value - The property value
      Returns:
      true if the property should be serialized