FormattedYearMonthSerializer

abstract class FormattedYearMonthSerializer(name: String, format: DateTimeFormat<YearMonth>) : KSerializer<YearMonth> (source)

An abstract serializer for YearMonth values that uses a custom DateTimeFormat to serialize and deserialize the value.

name is the name of the serializer. The SerialDescriptor.serialName of the resulting serializer is kotlinx.datetime.YearMonth/serializer/name. SerialDescriptor.serialName must be unique across all serializers in the same serialization context. When defining a serializer in a library, it is recommended to use the fully qualified class name in name to avoid conflicts with serializers defined by other libraries and client code.

This serializer is abstract and must be subclassed to provide a concrete serializer. Example:

// serializes YearMonth(2020, 1) as the string "202001"
object IsoBasicYearMonthSerializer :
FormattedYearMonthSerializer("my.package.ISO_BASIC", YearMonth.Format { year(); monthNumber() })

Note that YearMonth is kotlinx.serialization.Serializable by default, so it is not necessary to create custom serializers when the format is not important. Additionally, YearMonthIso8601Serializer is provided for the ISO 8601 format.

Constructors

Link copied to clipboard
constructor(name: String, format: DateTimeFormat<YearMonth>)