# SPDX-License-Identifier: Apache-2.0

###########################################
# Pekko Serialization Jackson Config File #
###########################################

# This is the reference config file that contains all the default settings.
# Make your edits/overrides in your application.conf.

#//#jackson-modules
pekko.serialization.jackson {

  # The Jackson JSON serializer will register these modules.
  jackson-modules += "org.apache.pekko.serialization.jackson.PekkoJacksonModule"
  # PekkoTypedJacksonModule optionally included if pekko-actor-typed is in classpath
  jackson-modules += "org.apache.pekko.serialization.jackson.PekkoTypedJacksonModule"
  # PekkoStreamsModule optionally included if pekko-streams is in classpath
  jackson-modules += "org.apache.pekko.serialization.jackson.PekkoStreamJacksonModule"
  jackson-modules += "com.fasterxml.jackson.module.paramnames.ParameterNamesModule"
  jackson-modules += "com.fasterxml.jackson.datatype.jdk8.Jdk8Module"
  jackson-modules += "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule"
  jackson-modules += "com.fasterxml.jackson.module.scala.DefaultScalaModule"
}
#//#jackson-modules

pekko.serialization.jackson {
  # When enabled and pekko.loglevel=DEBUG serialization time and payload size
  # is logged for each messages.
  verbose-debug-logging = off

  # Define data migration transformations of old formats to current
  # format here as a mapping between the (old) class name to be
  # transformed to the JacksonJsonMigration class that implements
  # the transformation.
  migrations {
  }

  # Controls the Buffer Recycler Pool implementation used by Jackson.
  # https://javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.18.1/com/fasterxml/jackson/core/util/JsonRecyclerPools.html
  # The default is "thread-local" which is the same as the default in Jackson 2.18.
  buffer-recycler {
    # the supported values are "thread-local", "concurrent-deque", "shared-concurrent-deque", "bounded", "non-recycling"
    pool-instance = "thread-local"
    # the maximum size of bounded recycler pools - must be >=1 or an IllegalArgumentException will occur
    # only applies to pool-instance type "bounded"
    bounded-pool-size = 100
  }
}

#//#stream-read-constraints
pekko.serialization.jackson {
  read {
    # see https://www.javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.17.1/com/fasterxml/jackson/core/StreamReadConstraints.html
    # these defaults are the same as the defaults in `StreamReadConstraints`
    max-nesting-depth = 1000
    max-number-length = 1000
    max-string-length = 20000000
    max-name-length = 50000
    # max-document-length of -1 means unlimited
    max-document-length = -1
    # max-token-count of -1 means unlimited
    max-token-count = -1
  }

  write {
    # see https://www.javadoc.io/static/com.fasterxml.jackson.core/jackson-core/2.17.1/com/fasterxml/jackson/core/StreamWriteConstraints.html
    # these defaults are the same as the defaults in `StreamWriteConstraints`
    max-nesting-depth = 1000
  }
}
#//#stream-read-constraints

#//#features
pekko.serialization.jackson {
  # Configuration of the ObjectMapper serialization features.
  # See com.fasterxml.jackson.databind.SerializationFeature
  # Enum values corresponding to the SerializationFeature and their boolean value.
  serialization-features {
    # Date/time in ISO-8601 (rfc3339) yyyy-MM-dd'T'HH:mm:ss.SSSZ format
    # as defined by com.fasterxml.jackson.databind.util.StdDateFormat
    # For interoperability it's better to use the ISO format, i.e. WRITE_DATES_AS_TIMESTAMPS=off,
    # but WRITE_DATES_AS_TIMESTAMPS=on has better performance.
    WRITE_DATES_AS_TIMESTAMPS = off
    WRITE_DURATIONS_AS_TIMESTAMPS = off
    FAIL_ON_EMPTY_BEANS = off
  }

  # Configuration of the ObjectMapper deserialization features.
  # See com.fasterxml.jackson.databind.DeserializationFeature
  # Enum values corresponding to the DeserializationFeature and their boolean value.
  deserialization-features {
    FAIL_ON_UNKNOWN_PROPERTIES = off
  }

  # Configuration of the ObjectMapper deserialization features.
  # See com.fasterxml.jackson.databind.cfg.EnumFeature
  # Enum values corresponding to the EnumFeature and their
  # boolean values, for example:
  #
  # enum-features {
  #   READ_ENUM_KEYS_USING_INDEX = off
  # }
  enum-features {}

  # Configuration of the ObjectMapper mapper features.
  # See com.fasterxml.jackson.databind.MapperFeature
  # Enum values corresponding to the MapperFeature and their
  # boolean values, for example:
  #
  # mapper-features {
  #   SORT_PROPERTIES_ALPHABETICALLY = on
  # }
  mapper-features {}

  # Configuration of the ObjectMapper JsonParser features.
  # See com.fasterxml.jackson.core.JsonParser.Feature
  # Enum values corresponding to the JsonParser.Feature and their
  # boolean value, for example:
  #
  # json-parser-features {
  #   ALLOW_SINGLE_QUOTES = on
  # }
  json-parser-features {}

  # Configuration of the ObjectMapper JsonParser features.
  # See com.fasterxml.jackson.core.JsonGenerator.Feature
  # Enum values corresponding to the JsonGenerator.Feature and
  # their boolean value, for example:
  #
  # json-generator-features {
  #   WRITE_NUMBERS_AS_STRINGS = on
  # }
  json-generator-features {}

  # Configuration of the JsonFactory StreamReadFeature.
  # See com.fasterxml.jackson.core.StreamReadFeature
  # Enum values corresponding to the StreamReadFeatures and
  # their boolean value, for example:
  #
  # stream-read-features {
  #   STRICT_DUPLICATE_DETECTION = on
  # }
  stream-read-features {}

  # Configuration of the JsonFactory StreamWriteFeature.
  # See com.fasterxml.jackson.core.StreamWriteFeature
  # Enum values corresponding to the StreamWriteFeatures and
  # their boolean value, for example:
  #
  # stream-write-features {
  #   WRITE_BIGDECIMAL_AS_PLAIN = on
  # }
  stream-write-features {}

  # Configuration of the JsonFactory JsonReadFeature.
  # See com.fasterxml.jackson.core.json.JsonReadFeature
  # Enum values corresponding to the JsonReadFeatures and
  # their boolean value, for example:
  #
  # json-read-features {
  #   ALLOW_SINGLE_QUOTES = on
  # }
  json-read-features {}

  # Configuration of the JsonFactory JsonWriteFeature.
  # See com.fasterxml.jackson.core.json.JsonWriteFeature
  # Enum values corresponding to the JsonWriteFeatures and
  # their boolean value, for example:
  #
  # json-write-features {
  #   WRITE_NUMBERS_AS_STRINGS = on
  # }
  json-write-features {}

  # Configuration of the JsonFactory Visibility.
  # See com.fasterxml.jackson.annotation.PropertyAccessor
  # and com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility
  # Enum values. For example, to serialize only public fields
  # overwrite the default values with:
  #
  # visibility {
  #   FIELD = PUBLIC_ONLY
  # }
  # Default: all fields (including private and protected) are serialized.
  visibility {
    FIELD = ANY
  }

  # Deprecated, use `allowed-class-prefix` instead
  whitelist-class-prefix = []

  # Additional classes that are allowed even if they are not defined in `serialization-bindings`.
  # This is useful when a class is not used for serialization any more and therefore removed
  # from `serialization-bindings`, but should still be possible to deserialize.
  allowed-class-prefix = ${pekko.serialization.jackson.whitelist-class-prefix}


  # settings for compression of the payload
  compression {
    # Compression algorithm.
    # - off  : no compression
    # - gzip : using common java gzip
    algorithm = off

    # If compression is enabled with the `algorithm` setting the payload is compressed
    # when it's larger than this value.
    compress-larger-than = 0 KiB
  }

  # Whether the type should be written to the manifest.
  # If this is off, then either deserialization-type must be defined, or there must be exactly
  # one serialization binding declared for this serializer, and the type in that binding will be
  # used as the deserialization type. This feature will only work if that type either is a
  # concrete class, or if it is a supertype that uses Jackson polymorphism (ie, the
  # @JsonTypeInfo annotation) to store type information in the JSON itself. The intention behind
  # disabling this is to remove extraneous type information (ie, fully qualified class names) when
  # serialized objects are persisted in Pekko persistence or replicated using Pekko distributed
  # data. Note that Pekko remoting already has manifest compression optimizations that address this,
  # so for types that just get sent over remoting, this offers no optimization.
  type-in-manifest = on

  # The type to use for deserialization.
  # This is only used if type-in-manifest is disabled. If set, this type will be used to
  # deserialize all messages. This is useful if the binding configuration you want to use when
  # disabling type in manifest cannot be expressed as a single type. Examples of when you might
  # use this include when changing serializers, so you don't want this serializer used for
  # serialization and you haven't declared any bindings for it, but you still want to be able to
  # deserialize messages that were serialized with this serializer, as well as situations where
  # you only want some sub types of a given Jackson polymorphic type to be serialized using this
  # serializer.
  deserialization-type = ""

  # Specific settings for jackson-json binding can be defined in this section to
  # override the settings in 'pekko.serialization.jackson'
  jackson-json {}

  # Specific settings for jackson-cbor binding can be defined in this section to
  # override the settings in 'pekko.serialization.jackson'
  jackson-cbor {}

  # Issue #28918 for compatibility with data serialized with JacksonCborSerializer in
  # Akka 2.6.4 or earlier, which was plain JSON format.
  jackson-cbor-264 = ${pekko.serialization.jackson.jackson-cbor}

}
#//#features

#//#compression
# Compression settings for the jackson-json binding
pekko.serialization.jackson.jackson-json.compression {
  # Compression algorithm.
  # - off  : no compression
  # - gzip : using common java gzip
  # - lz4 : using lz4-java
  algorithm = gzip

  # If compression is enabled with the `algorithm` setting the payload is compressed
  # when it's larger than this value.
  compress-larger-than = 32 KiB
}
#//#compression

pekko.actor {
  serializers {
    jackson-json = "org.apache.pekko.serialization.jackson.JacksonJsonSerializer"
    jackson-cbor = "org.apache.pekko.serialization.jackson.JacksonCborSerializer"

    # Issue #28918 for compatibility with data serialized with JacksonCborSerializer in
    # Akka 2.6.4 or earlier, which was plain JSON format.
    jackson-cbor-264 = "org.apache.pekko.serialization.jackson.JacksonJsonSerializer"
  }
  serialization-identifiers {
    jackson-json = 31
    jackson-cbor = 33

    # Issue #28918 for compatibility with data serialized with JacksonCborSerializer in
    # Akka 2.6.4 or earlier, which was plain JSON format.
    jackson-cbor-264 = 32
  }
  serialization-bindings {
    # Define bindings for classes or interfaces use Jackson serializer, e.g.
    # "com.example.Jsonable" = jackson-json
    # "com.example.MyMessage" = jackson-cbor
    #
    # For security reasons it is disallowed to bind the Jackson serializers to
    # open ended types that might be target to be deserialization gadgets, such as
    # java.lang.Object, java.io.Serializable, java.util.Comparable

  }
}
