FormattedInstantSerializer

abstract class FormattedInstantSerializer(name: String, format: DateTimeFormat<DateTimeComponents>) : KSerializer<Instant> (source)

An abstract serializer for Instant values that uses a custom DateTimeFormat for serializing to and deserializing.

format should be a format that includes enough components to unambiguously define a date, a time, and a UTC offset. See Instant.parse for details of how deserialization is performed.

name is the name of the serializer. The SerialDescriptor.serialName of the resulting serializer is kotlinx.datetime.Instant/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.

When serializing, the Instant value is formatted as a string using the specified format in the UtcOffset.ZERO UTC offset.

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

// serializes LocalDateTime(2008, 6, 30, 11, 5, 30).toInstant(TimeZone.UTC)
// as the string "Mon, 30 Jun 2008 11:05:30 GMT"
object Rfc1123InstantSerializer : FormattedInstantSerializer(
"my.package.RFC1123", DateTimeComponents.Formats.RFC_1123
)

Note that kotlinx.serialization already provides kotlinx.serialization.Serializable support for Instant, so Instant values can be serialized and deserialized using the default serializer.

Constructors

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

Properties

Link copied to clipboard
open override val descriptor: SerialDescriptor

Functions

Link copied to clipboard
open override fun deserialize(decoder: Decoder): Instant
Link copied to clipboard
open override fun serialize(encoder: Encoder, value: Instant)
Link copied to clipboard
open override fun toString(): String