sschuberth / ks3

KotlinX Serialization Standard Serializers (KS3)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KotlinX Serialization Standard Serializers (KS3)

GitHub commit checks state Sonatype Nexus (Snapshots)

This project aims to provide a set of serializers for common types, as well as provide utilities to make it more convenient to build custom serializers for your needs.

⚠️ Consider this project to be Alpha for the time being. We would be happy for you to use it, and appreciate your feedback, but things may be re-written. We hope to reach Beta relatively quick.

Getting started

Add the dependency. With Gradle:

dependencies {
    implementation("io.ks3:ks3-jdk:0.2.0")
}

Maven:

<dependency>
  <groupId>io.ks3</groupId>
  <artifactId>ks3-jdk-jvm</artifactId>
  <version>$version</version>
</dependency>

Note that when using Maven dependencies must specify the multiplatform variant. For example, append -jvm to specify the JVM variant of ks3-jdk.

Now you can start using the provided serializers. There's several possible ways to do this.

Using typealias

typealias LocalDateTimeAsString = @Serializable(LocalDateTimeAsStringSerializer::class) LocalDateTime

data class Appointment(
  val datetime: LocalDateTimeAsString
)

For details, see the kotlinx.serialization guide

This method is most useful when you want to use different serial formats for the same type, or when you can't configure the serializer itself.

Using @Contextual

See the details in kotlinx.serialization guide

@Serializable
class ProgrammingLanguage(
    val name: String,
    @Contextual
    val stableReleaseDate: Date
)

private val module = SerializersModule {
  contextual(DateAsLongSerializer)
}

val format = Json { serializersModule = module }

About

KotlinX Serialization Standard Serializers (KS3)

License:Apache License 2.0


Languages

Language:Kotlin 100.0%