@@ -11,11 +11,13 @@ import kotlinx.serialization.cbor.*
1111import kotlinx.serialization.descriptors.*
1212import kotlinx.serialization.encoding.*
1313
14+ internal interface CborSerializer
15+
1416/* *
1517 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborElement].
1618 * It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
1719 */
18- internal object CborElementSerializer : KSerializer<CborElement> {
20+ internal object CborElementSerializer : KSerializer<CborElement>, CborSerializer {
1921 override val descriptor: SerialDescriptor =
2022 buildSerialDescriptor(" kotlinx.serialization.cbor.CborElement" , PolymorphicKind .SEALED ) {
2123 // Resolve cyclic dependency in descriptors by late binding
@@ -27,8 +29,8 @@ internal object CborElementSerializer : KSerializer<CborElement> {
2729 element(" CborMap" , defer { CborMapSerializer .descriptor })
2830 element(" CborList" , defer { CborListSerializer .descriptor })
2931 element(" CborDouble" , defer { CborDoubleSerializer .descriptor })
30- element(" CborInt" , defer { CborIntSerializer .descriptor })
31- element(" CborUInt" , defer { CborUIntSerializer .descriptor })
32+ element(" CborInt" , defer { CborNegativeIntSerializer .descriptor })
33+ element(" CborUInt" , defer { CborPositiveIntSerializer .descriptor })
3234 }
3335
3436 override fun serialize (encoder : Encoder , value : CborElement ) {
@@ -52,23 +54,19 @@ internal object CborElementSerializer : KSerializer<CborElement> {
5254 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborPrimitive].
5355 * It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
5456 */
55- internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
57+ internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>>, CborSerializer {
5658 override val descriptor: SerialDescriptor =
57- buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PrimitiveKind . STRING )
59+ buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PolymorphicKind . SEALED )
5860
5961 override fun serialize (encoder : Encoder , value : CborPrimitive <* >) {
60- val cborEncoder = encoder.asCborEncoder()
61-
62- cborEncoder.encodeTags(value)
63-
6462 when (value) {
6563 is CborNull -> encoder.encodeSerializableValue(CborNullSerializer , value)
6664 is CborString -> encoder.encodeSerializableValue(CborStringSerializer , value)
6765 is CborBoolean -> encoder.encodeSerializableValue(CborBooleanSerializer , value)
6866 is CborByteString -> encoder.encodeSerializableValue(CborByteStringSerializer , value)
6967 is CborDouble -> encoder.encodeSerializableValue(CborDoubleSerializer , value)
70- is CborNegativeInt -> encoder.encodeSerializableValue(CborIntSerializer , value)
71- is CborPositiveInt -> encoder.encodeSerializableValue(CborUIntSerializer , value)
68+ is CborNegativeInt -> encoder.encodeSerializableValue(CborNegativeIntSerializer , value)
69+ is CborPositiveInt -> encoder.encodeSerializableValue(CborPositiveIntSerializer , value)
7270 }
7371 }
7472
@@ -83,13 +81,14 @@ internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
8381 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborNull].
8482 * It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
8583 */
86- internal object CborNullSerializer : KSerializer<CborNull> {
84+ internal object CborNullSerializer : KSerializer<CborNull>, CborSerializer {
8785
8886 override val descriptor: SerialDescriptor =
8987 buildSerialDescriptor(" kotlinx.serialization.cbor.CborNull" , SerialKind .ENUM )
9088
9189 override fun serialize (encoder : Encoder , value : CborNull ) {
92- encoder.asCborEncoder().encodeTags(value)
90+ val cborEncoder = encoder.asCborEncoder()
91+ cborEncoder.encodeTags(value)
9392 encoder.encodeNull()
9493 }
9594
@@ -98,16 +97,38 @@ internal object CborNullSerializer : KSerializer<CborNull> {
9897 if (decoder.decodeNotNullMark()) {
9998 throw CborDecodingException (" Expected 'null' literal" )
10099 }
100+
101101 decoder.decodeNull()
102102 return CborNull ()
103103 }
104104}
105105
106- internal object CborIntSerializer : KSerializer<CborNegativeInt> {
107- override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborInt" , PrimitiveKind .LONG )
106+
107+ internal object CborIntSerializer : KSerializer<CborInt<*>>, CborSerializer {
108+ override val descriptor: SerialDescriptor =
109+ PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborInt" , PrimitiveKind .LONG )
110+
111+ override fun serialize (encoder : Encoder , value : CborInt <* >) {
112+ when (value) {
113+ is CborNegativeInt -> encoder.encodeSerializableValue(CborNegativeIntSerializer , value)
114+ is CborPositiveInt -> encoder.encodeSerializableValue(CborPositiveIntSerializer , value)
115+ }
116+ }
117+
118+ override fun deserialize (decoder : Decoder ): CborInt <* > {
119+ val result = decoder.asCborDecoder().decodeCborElement()
120+ if (result !is CborInt <* >) throw CborDecodingException (" Unexpected CBOR element, expected CborInt, had ${result::class } " )
121+ return result
122+ }
123+ }
124+
125+ internal object CborNegativeIntSerializer : KSerializer<CborNegativeInt>, CborSerializer {
126+ override val descriptor: SerialDescriptor =
127+ PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborNegativeInt" , PrimitiveKind .LONG )
108128
109129 override fun serialize (encoder : Encoder , value : CborNegativeInt ) {
110- encoder.asCborEncoder().encodeTags(value)
130+ val cborEncoder = encoder.asCborEncoder()
131+ cborEncoder.encodeTags(value)
111132 encoder.encodeLong(value.value)
112133 }
113134
@@ -117,12 +138,14 @@ internal object CborIntSerializer : KSerializer<CborNegativeInt> {
117138 }
118139}
119140
120- internal object CborUIntSerializer : KSerializer<CborPositiveInt> {
121- override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" CborUInt" , PrimitiveKind .LONG )
141+ internal object CborPositiveIntSerializer : KSerializer<CborPositiveInt>, CborSerializer {
142+ override val descriptor: SerialDescriptor =
143+ PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborPositiveInt" , PrimitiveKind .LONG )
122144
123145 override fun serialize (encoder : Encoder , value : CborPositiveInt ) {
124- encoder.asCborEncoder().encodeTags(value)
125- encoder.encodeInline(descriptor).encodeSerializableValue(ULong .serializer(), value.value)
146+ val cborEncoder = encoder.asCborEncoder()
147+ cborEncoder.encodeTags(value)
148+ encoder.encodeInline(descriptor).encodeSerializableValue(ULong .serializer(), value.value as ULong )
126149 }
127150
128151 override fun deserialize (decoder : Decoder ): CborPositiveInt {
@@ -131,11 +154,13 @@ internal object CborUIntSerializer : KSerializer<CborPositiveInt> {
131154 }
132155}
133156
134- internal object CborDoubleSerializer : KSerializer<CborDouble> {
135- override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborDouble" , PrimitiveKind .DOUBLE )
157+ internal object CborDoubleSerializer : KSerializer<CborDouble>, CborSerializer {
158+ override val descriptor: SerialDescriptor =
159+ PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborDouble" , PrimitiveKind .DOUBLE )
136160
137161 override fun serialize (encoder : Encoder , value : CborDouble ) {
138- encoder.asCborEncoder().encodeTags(value)
162+ val cborEncoder = encoder.asCborEncoder()
163+ cborEncoder.encodeTags(value)
139164 encoder.encodeDouble(value.value)
140165 }
141166
@@ -149,12 +174,13 @@ internal object CborDoubleSerializer : KSerializer<CborDouble> {
149174 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborString].
150175 * It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
151176 */
152- internal object CborStringSerializer : KSerializer<CborString> {
177+ internal object CborStringSerializer : KSerializer<CborString>, CborSerializer {
153178 override val descriptor: SerialDescriptor =
154179 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborString" , PrimitiveKind .STRING )
155180
156181 override fun serialize (encoder : Encoder , value : CborString ) {
157- encoder.asCborEncoder().encodeTags(value)
182+ val cborEncoder = encoder.asCborEncoder()
183+ cborEncoder.encodeTags(value)
158184 encoder.encodeString(value.value)
159185 }
160186
@@ -170,12 +196,13 @@ internal object CborStringSerializer : KSerializer<CborString> {
170196 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborBoolean].
171197 * It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
172198 */
173- internal object CborBooleanSerializer : KSerializer<CborBoolean> {
199+ internal object CborBooleanSerializer : KSerializer<CborBoolean>, CborSerializer {
174200 override val descriptor: SerialDescriptor =
175201 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborBoolean" , PrimitiveKind .BOOLEAN )
176202
177203 override fun serialize (encoder : Encoder , value : CborBoolean ) {
178- encoder.asCborEncoder().encodeTags(value)
204+ val cborEncoder = encoder.asCborEncoder()
205+ cborEncoder.encodeTags(value)
179206 encoder.encodeBoolean(value.value)
180207 }
181208
@@ -191,13 +218,13 @@ internal object CborBooleanSerializer : KSerializer<CborBoolean> {
191218 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborByteString].
192219 * It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
193220 */
194- internal object CborByteStringSerializer : KSerializer<CborByteString> {
221+ internal object CborByteStringSerializer : KSerializer<CborByteString>, CborSerializer {
195222 override val descriptor: SerialDescriptor =
196223 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborByteString" , PrimitiveKind .STRING )
197224
198225 override fun serialize (encoder : Encoder , value : CborByteString ) {
199226 val cborEncoder = encoder.asCborEncoder()
200- cborEncoder.encodeTags(value)
227+ cborEncoder.encodeTags(value)
201228 cborEncoder.encodeByteString(value.value)
202229 }
203230
@@ -213,7 +240,7 @@ internal object CborByteStringSerializer : KSerializer<CborByteString> {
213240 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborMap].
214241 * It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
215242 */
216- internal object CborMapSerializer : KSerializer<CborMap> {
243+ internal object CborMapSerializer : KSerializer<CborMap>, CborSerializer {
217244 private object CborMapDescriptor :
218245 SerialDescriptor by MapSerializer (CborElementSerializer , CborElementSerializer ).descriptor {
219246 @ExperimentalSerializationApi
@@ -238,7 +265,7 @@ internal object CborMapSerializer : KSerializer<CborMap> {
238265 * Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborList].
239266 * It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
240267 */
241- internal object CborListSerializer : KSerializer<CborList> {
268+ internal object CborListSerializer : KSerializer<CborList>, CborSerializer {
242269 private object CborListDescriptor : SerialDescriptor by ListSerializer(CborElementSerializer ).descriptor {
243270 @ExperimentalSerializationApi
244271 override val serialName: String = " kotlinx.serialization.cbor.CborList"
@@ -296,9 +323,7 @@ private fun defer(deferred: () -> SerialDescriptor): SerialDescriptor = object :
296323
297324private fun CborWriter.encodeTags (value : CborElement ) { // Encode tags if present
298325 if (value.tags.isNotEmpty()) {
299- for (tag in value.tags) {
300- encodeTag(tag)
301- }
326+ encodeTags(value.tags)
302327 }
303328
304329}
0 commit comments