|
| 1 | +/* |
| 2 | + * Copyright (c) 2025. |
| 3 | + * |
| 4 | + * This file is part of xmlutil. |
| 5 | + * |
| 6 | + * This file is licenced to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You should have received a copy of the license with the source distribution. |
| 9 | + * Alternatively, you may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package net.devrieze.serialization.examples.boxingTag |
| 22 | + |
| 23 | +import kotlinx.serialization.KSerializer |
| 24 | +import kotlinx.serialization.Serializable |
| 25 | +import kotlinx.serialization.builtins.serializer |
| 26 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 27 | +import kotlinx.serialization.descriptors.buildClassSerialDescriptor |
| 28 | +import kotlinx.serialization.encodeToString |
| 29 | +import kotlinx.serialization.encoding.CompositeDecoder |
| 30 | +import kotlinx.serialization.encoding.Decoder |
| 31 | +import kotlinx.serialization.encoding.Encoder |
| 32 | +import kotlinx.serialization.encoding.decodeStructure |
| 33 | +import kotlinx.serialization.encoding.encodeStructure |
| 34 | +import nl.adaptivity.xmlutil.serialization.XML |
| 35 | +import nl.adaptivity.xmlutil.serialization.XmlSerialName |
| 36 | + |
| 37 | +/* |
| 38 | + * This is an example for implementing #80 in a more compact way. |
| 39 | + */ |
| 40 | + |
| 41 | +@Serializable |
| 42 | +class Header( |
| 43 | + @XmlSerialName("ModelInfo") val modelInfo: BoxedString, |
| 44 | + @XmlSerialName("ModelVersion") val modelVersion: BoxedString, |
| 45 | + @XmlSerialName("PersistencyVersion") val persistencyVersion: BoxedString, |
| 46 | +) |
| 47 | + |
| 48 | +fun main() { |
| 49 | + val data = Header("foo", "bar", "baz") |
| 50 | + |
| 51 | + println(XML{ recommended_0_90_2()}.encodeToString(data)) |
| 52 | +} |
| 53 | + |
| 54 | +typealias BoxedString = @Serializable(BoxedSerializer::class) String |
| 55 | + |
| 56 | +object BoxedSerializer: KSerializer<String> { |
| 57 | + override val descriptor: SerialDescriptor = buildClassSerialDescriptor(BoxedSerializer::class.qualifiedName!!) { |
| 58 | + element("value", String.serializer().descriptor) |
| 59 | + } |
| 60 | + |
| 61 | + override fun serialize(encoder: Encoder, value: String) { |
| 62 | + encoder.encodeStructure(descriptor) { |
| 63 | + encodeStringElement(descriptor, 0, value) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override fun deserialize(decoder: Decoder): String { |
| 68 | + return decoder.decodeStructure(descriptor) { |
| 69 | + lateinit var value: String |
| 70 | + do { |
| 71 | + when (val i = this.decodeElementIndex(descriptor)) { |
| 72 | + CompositeDecoder.DECODE_DONE -> break |
| 73 | + 0 -> value = decodeStringElement(descriptor, 0) |
| 74 | + else -> throw IllegalArgumentException("Unknown index $i") |
| 75 | + } |
| 76 | + } while (true) |
| 77 | + value |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments