abstract class JsonTransformingSerializer<T : Any> : KSerializer<T>
Base class for custom serializers that allows manipulating an abstract JSON representation of the class before serialization or deserialization.
JsonTransformingSerializer provides capabilities to manipulate JsonElement representation directly instead of interacting with Encoder and Decoder in order to apply a custom transformation to the JSON. Please note that this class expects that Encoder and Decoder are implemented by JsonInput and JsonOutput, i.e. serializers derived from this class work only with Json format.
During serialization, this class first serializes original value with tSerializer to a JsonElement, then calls writeTransform method, which may contain a user-defined transformation, such as wrapping a value into JsonArray, filtering keys, adding keys, etc.
During deserialization, the opposite process happens: first, value from JSON stream is read to a JsonElement, second, user transformation in readTransform is applied, and then JSON tree is deserialized back to T with tSerializer.
Usage example:
@Serializable
data class Example(
@Serializable(UnwrappingJsonListSerializer::class) val data: String
)
// Unwraps a list to a single object
object UnwrappingJsonListSerializer :
JsonTransformingSerializer<String>(String.serializer(), "UnwrappingList") {
override fun readTransform(element: JsonElement): JsonElement {
if (element !is JsonArray) return element
require(element.size == 1) { "Array size must be equal to 1 to unwrap it" }
return element.first()
}
}
// Now these functions both yield correct result:
Json.parse(Example.serializer(), """{"data":["str1"]}""")
Json.parse(Example.serializer(), """{"data":"str1"}""")
T
- A type for Kotlin property for which this serializer could be applied.
Not the type that you may encounter in JSON. (e.g. if you unwrap a list
to a single value T
, use T
, not List<T>
)
tSerializer
- A serializer for type T. Determines JsonElement which is passed to writeTransform.
Should be able to parse JsonElement from readTransform function.
Usually, default serializer is sufficient.
transformationName
- A name for the particular implementation to fulfill SerialDescriptor.serialName uniqueness guarantee.
(common, js, jvm, native)
<init> |
Base class for custom serializers that allows manipulating an abstract JSON representation of the class before serialization or deserialization. <init>(tSerializer: KSerializer<T>, transformationName: String) |
(common, js, jvm, native)
descriptor |
A descriptor for this transformation. By default, it uses the name composed of tSerializer's descriptor and transformation name, kind of tSerializer's descriptor and contains 0 elements. open val descriptor: SerialDescriptor |
(common, js, jvm, native)
deserialize |
Deserializes the value of type T using the format that is represented by the given decoder. deserialize method is format-agnostic and operates with a high-level structured Decoder API. As long as most of the formats imply an arbitrary order of properties, deserializer should be able to decode these properties in an arbitrary order and in a format-agnostic way. For that purposes, CompositeDecoder.decodeElementIndex-based loop is used: decoder firstly signals property at which index it is ready to decode and then expects caller to decode property with the given index. fun deserialize(decoder: Decoder): T |
(common, js, jvm, native)
readTransform |
Transformation that happens during deserialize call. Does nothing by default. open fun readTransform(element: JsonElement): JsonElement |
(common, js, jvm, native)
serialize |
Serializes the value of type T using the format that is represented by the given encoder. serialize method is format-agnostic and operates with a high-level structured Encoder API. Throws SerializationException if value cannot be serialized. fun serialize(encoder: Encoder, value: T): Unit |
(common, js, jvm, native)
writeTransform |
Transformation that happens during serialize call. Does nothing by default. open fun writeTransform(element: JsonElement): JsonElement |