Skip to content

Instantly share code, notes, and snippets.

@mel4tonin4
Created May 4, 2020 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mel4tonin4/1605d28e9bd2471c5bfb013b2d31f0ef to your computer and use it in GitHub Desktop.
Save mel4tonin4/1605d28e9bd2471c5bfb013b2d31f0ef to your computer and use it in GitHub Desktop.
package io.arrowkt.example.inline_serialization
import kotlinx.serialization.*
import io.arrowkt.example.ChannelCount
@Serializer(forClass = ChannelCount::class)
class ChannelCountSerializer : KSerializer<ChannelCount> {
override val descriptor: SerialDescriptor =
PrimitiveDescriptor("ChannelCount", PrimitiveKind.INT)
override fun serialize(encoder: Encoder, value: ChannelCount) {
encoder.encodeInt(value.value)
}
override fun deserialize(decoder: Decoder): ChannelCount {
return ChannelCount(decoder.decodeInt())
}
}
package io.arrowkt.example
//metadebug
@InlineSerializable
data class ChannelCount(val value : Int) {
override fun toString(): String = "$value channels"
operator fun plus(other : ChannelCount) =
ChannelCount(value + other.value)
}
package io.arrowkt.example
//meta: Mon May 04 16:52:49 CEST 2020
@arrow.synthetic
@kotlinx.serialization.Serializable(with = io.arrowkt.example.inline_serialization.ChannelCountSerializer::class)
@InlineSerializable
data class ChannelCount(val value: Int) {
override fun toString(): String = "$value channels"
operator fun plus(other: ChannelCount) = ChannelCount(value + other.value)
}
package io.arrowkt.example.inline_serialization
import kotlinx.serialization.*
import io.arrowkt.example.UserId
@Serializer(forClass = UserId::class)
class UserIdSerializer : KSerializer<UserId> {
override val descriptor: SerialDescriptor =
PrimitiveDescriptor("UserId", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: UserId) {
encoder.encodeString(value.value)
}
override fun deserialize(decoder: Decoder): UserId {
return UserId(decoder.decodeString())
}
}
package io.arrowkt.example
@InlineSerializable
data class UserId(val value : String) {
companion object {
fun unique() =
UserId(java.util.UUID.randomUUID().toString())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment