KBigInt Serialization
The serialization module of the KBigInt library.
Add the dependency
dependencies {
implementation("io.github.observeroftime.kbigint:kbigint-serialization")
}
repositories {
mavenCentral()
}
Content copied to clipboard
Use the library
There are three ways to enable (de)serialization of KBigInt
classes. The examples use kotlinx-serialization-json
but any format should work.
Serializers module
You can declare a new encoder/decoder instance using the serializers module.
import io.github.observeroftime.kbigint.KBigInt
import io.github.observeroftime.kbigint.serialization.module
import kotlinx.serialization.json.*
val json = Json { serializersModule = module }
val number = KBigInt("9007199254740991")
val encoded = json.encodeToJsonElement(number)
val decoded = json.decodeFromJsonElement(encoded)
Content copied to clipboard
Serializable type alias
The serializable type alias can be used explicitly in type parameters.
NOTE: this method may or may not work.
import io.github.observeroftime.kbigint.serialization.KBigInt
import kotlinx.serialization.json.*
val number = KBigInt("9007199254740991")
val encoded = Json.encodeToJsonElement<KBigInt>(number)
val decoded = Json.decodeFromJsonElement<KBigInt>(encoded)
Content copied to clipboard
Serialization strategy
You can use the serializer as the serialization strategy parameter.
import io.github.observeroftime.kbigint.KBigInt
import io.github.observeroftime.kbigint.serialization.KBigIntSerializer
import kotlinx.serialization.json.*
val number = KBigInt("9007199254740991")
val encoded = Json.encodeToJsonElement(KBigIntSerializer, number)
val decoded = Json.decodeFromJsonElement(KBigIntSerializer, encoded)
Content copied to clipboard