All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.c0x12c.featureflag.serializer.CustomDurationSerializer.kt Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package com.c0x12c.featureflag.serializer

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.time.Duration

object CustomDurationSerializer : KSerializer {
  override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CustomDuration", PrimitiveKind.STRING)

  override fun serialize(
    encoder: Encoder,
    value: Duration
  ) {
    encoder.encodeString(value.toString()) // Serialize Duration as ISO-8601 string
  }

  override fun deserialize(decoder: Decoder): Duration {
    return Duration.parse(decoder.decodeString()) // Parse ISO-8601 string to Duration
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy