| // This file was automatically generated from formats.md by Knit tool. Do not edit. |
| package example.exampleFormats16 |
| |
| import kotlinx.serialization.* |
| import kotlinx.serialization.Serializable |
| import kotlinx.serialization.descriptors.* |
| import kotlinx.serialization.encoding.* |
| import kotlinx.serialization.modules.* |
| import java.io.* |
| |
| @ExperimentalSerializationApi |
| class DataOutputEncoder(val output: DataOutput) : AbstractEncoder() { |
| override val serializersModule: SerializersModule = EmptySerializersModule() |
| override fun encodeBoolean(value: Boolean) = output.writeByte(if (value) 1 else 0) |
| override fun encodeByte(value: Byte) = output.writeByte(value.toInt()) |
| override fun encodeShort(value: Short) = output.writeShort(value.toInt()) |
| override fun encodeInt(value: Int) = output.writeInt(value) |
| override fun encodeLong(value: Long) = output.writeLong(value) |
| override fun encodeFloat(value: Float) = output.writeFloat(value) |
| override fun encodeDouble(value: Double) = output.writeDouble(value) |
| override fun encodeChar(value: Char) = output.writeChar(value.code) |
| override fun encodeString(value: String) = output.writeUTF(value) |
| override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int) = output.writeInt(index) |
| |
| override fun beginCollection(descriptor: SerialDescriptor, collectionSize: Int): CompositeEncoder { |
| encodeInt(collectionSize) |
| return this |
| } |
| |
| override fun encodeNull() = encodeBoolean(false) |
| override fun encodeNotNullMark() = encodeBoolean(true) |
| } |
| |
| @ExperimentalSerializationApi |
| fun <T> encodeTo(output: DataOutput, serializer: SerializationStrategy<T>, value: T) { |
| val encoder = DataOutputEncoder(output) |
| encoder.encodeSerializableValue(serializer, value) |
| } |
| |
| @ExperimentalSerializationApi |
| inline fun <reified T> encodeTo(output: DataOutput, value: T) = encodeTo(output, serializer(), value) |
| |
| @ExperimentalSerializationApi |
| class DataInputDecoder(val input: DataInput, var elementsCount: Int = 0) : AbstractDecoder() { |
| private var elementIndex = 0 |
| override val serializersModule: SerializersModule = EmptySerializersModule() |
| override fun decodeBoolean(): Boolean = input.readByte().toInt() != 0 |
| override fun decodeByte(): Byte = input.readByte() |
| override fun decodeShort(): Short = input.readShort() |
| override fun decodeInt(): Int = input.readInt() |
| override fun decodeLong(): Long = input.readLong() |
| override fun decodeFloat(): Float = input.readFloat() |
| override fun decodeDouble(): Double = input.readDouble() |
| override fun decodeChar(): Char = input.readChar() |
| override fun decodeString(): String = input.readUTF() |
| override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = input.readInt() |
| |
| override fun decodeElementIndex(descriptor: SerialDescriptor): Int { |
| if (elementIndex == elementsCount) return CompositeDecoder.DECODE_DONE |
| return elementIndex++ |
| } |
| |
| override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder = |
| DataInputDecoder(input, descriptor.elementsCount) |
| |
| override fun decodeSequentially(): Boolean = true |
| |
| override fun decodeCollectionSize(descriptor: SerialDescriptor): Int = |
| decodeInt().also { elementsCount = it } |
| |
| override fun decodeNotNullMark(): Boolean = decodeBoolean() |
| } |
| |
| @ExperimentalSerializationApi |
| fun <T> decodeFrom(input: DataInput, deserializer: DeserializationStrategy<T>): T { |
| val decoder = DataInputDecoder(input) |
| return decoder.decodeSerializableValue(deserializer) |
| } |
| |
| @ExperimentalSerializationApi |
| inline fun <reified T> decodeFrom(input: DataInput): T = decodeFrom(input, serializer()) |
| |
| fun ByteArray.toAsciiHexString() = joinToString("") { |
| if (it in 32..127) it.toInt().toChar().toString() else |
| "{${it.toUByte().toString(16).padStart(2, '0').uppercase()}}" |
| } |
| |
| @Serializable |
| data class Project(val name: String, val language: String) |
| |
| @OptIn(ExperimentalSerializationApi::class) |
| fun main() { |
| val data = Project("kotlinx.serialization", "Kotlin") |
| val output = ByteArrayOutputStream() |
| encodeTo(DataOutputStream(output), data) |
| val bytes = output.toByteArray() |
| println(bytes.toAsciiHexString()) |
| val input = ByteArrayInputStream(bytes) |
| val obj = decodeFrom<Project>(DataInputStream(input)) |
| println(obj) |
| } |