Deserializer
Deserializer is a format agnostic deserialization interface. Specific formats (e.g. JSON, XML, etc) implement this interface and handle the underlying raw decoding process and deal with details specific to that format.
This allows the same deserialization process to work between formats which is useful for code generation.
Deserializing Structured Types
A Kotlin class is represented as a structure with fields. The order the fields present themselves may not be guaranteed or consistent in some formats (e.g. JSON and XML). This requires deserialization to iterate over the fields found in the underlying stream and the deserializer will tell you which field was encountered. This is done by giving the serializer an SdkObjectDescriptor which describes the fields expected.
data class Point(val x: Int, val y: Int)
val struct = deserializer.deserializeStruct()
var x: Int? = null
var y: Int? = null
val X_DESCRIPTOR = SdkFieldDescriptor("x")
val Y_DESCRIPTOR = SdkFieldDescriptor("y")
val OBJ_DESCRIPTOR = SdkObjectDescriptor.build() {
field(X_DESCRIPTOR)
field(Y_DESCRIPTOR)
}
loop@ while(true) {
when(struct.findNextFieldIndexOrNull()) {
X_DESCRIPTOR.index -> x = struct.deserializeInt()
Y_DESCRIPTOR.index -> y = struct.deserializeInt()
null -> break@loop
else -> struct.skipValue() // Unknown Field
}
}
requireNotNull(x)
requireNotNull(y)
val myPoint = Point(x!!, y!!)
Deserializing Collections
Collections such as List and Map work almost the same as deserializing a structured type except iteration is over elements (or entries) instead of fields. Deserialization implementations should drive the iterator until it is exhausted and for each element/entry call the appropriate deserialize*
methods.
Types
Functions
Begin deserialization of a list type. Use the returned ElementIterator to drive the deserialization process of the list to completion.
Begin deserialization of a map type. Use the returned EntryIterator to drive the deserialization process of the map to completion.
Begin deserialization of a structured type. Use the returned FieldIterator to drive the deserialization process of the struct to completion.