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

alakazam.kotlin.serialization.SimpleSerializer.kt Maven / Gradle / Ivy

The newest version!
package alakazam.kotlin.serialization

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

@Suppress("UnnecessaryAbstractClass")
public abstract class SimpleSerializer(
  serialName: String,
  private val constructor: (String) -> T,
) : KSerializer {
  override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(serialName, PrimitiveKind.STRING)

  override fun serialize(encoder: Encoder, value: T) {
    encoder.encodeString(value.toString())
  }

  override fun deserialize(decoder: Decoder): T = constructor(decoder.decodeString())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy