@@ -27,8 +27,8 @@ internal object CborElementSerializer : KSerializer<CborElement> {
2727 element(" CborMap" , defer { CborMapSerializer .descriptor })
2828 element(" CborList" , defer { CborListSerializer .descriptor })
2929 element(" CborDouble" , defer { CborDoubleSerializer .descriptor })
30- element(" CborInt" , defer { CborIntSerializer .descriptor })
31- element(" CborUInt" , defer { CborUIntSerializer .descriptor })
30+ element(" CborInt" , defer { CborNegativeIntSerializer .descriptor })
31+ element(" CborUInt" , defer { CborPositiveIntSerializer .descriptor })
3232 }
3333
3434 override fun serialize (encoder : Encoder , value : CborElement ) {
@@ -54,21 +54,17 @@ internal object CborElementSerializer : KSerializer<CborElement> {
5454 */
5555internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
5656 override val descriptor: SerialDescriptor =
57- buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PrimitiveKind . STRING )
57+ buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PolymorphicKind . SEALED )
5858
5959 override fun serialize (encoder : Encoder , value : CborPrimitive <* >) {
60- val cborEncoder = encoder.asCborEncoder()
61-
62- cborEncoder.encodeTags(value)
63-
6460 when (value) {
6561 is CborNull -> encoder.encodeSerializableValue(CborNullSerializer , value)
6662 is CborString -> encoder.encodeSerializableValue(CborStringSerializer , value)
6763 is CborBoolean -> encoder.encodeSerializableValue(CborBooleanSerializer , value)
6864 is CborByteString -> encoder.encodeSerializableValue(CborByteStringSerializer , value)
6965 is CborDouble -> encoder.encodeSerializableValue(CborDoubleSerializer , value)
70- is CborNegativeInt -> encoder.encodeSerializableValue(CborIntSerializer , value)
71- is CborPositiveInt -> encoder.encodeSerializableValue(CborUIntSerializer , value)
66+ is CborNegativeInt -> encoder.encodeSerializableValue(CborNegativeIntSerializer , value)
67+ is CborPositiveInt -> encoder.encodeSerializableValue(CborPositiveIntSerializer , value)
7268 }
7369 }
7470
@@ -89,7 +85,8 @@ internal object CborNullSerializer : KSerializer<CborNull> {
8985 buildSerialDescriptor(" kotlinx.serialization.cbor.CborNull" , SerialKind .ENUM )
9086
9187 override fun serialize (encoder : Encoder , value : CborNull ) {
92- encoder.asCborEncoder()
88+ val cborEncoder= encoder.asCborEncoder()
89+ cborEncoder.encodeTags(value)
9390 encoder.encodeNull()
9491 }
9592
@@ -103,11 +100,30 @@ internal object CborNullSerializer : KSerializer<CborNull> {
103100 }
104101}
105102
106- internal object CborIntSerializer : KSerializer<CborNegativeInt> {
103+
104+ internal object CborIntSerializer : KSerializer<CborInt<*>> {
107105 override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborInt" , PrimitiveKind .LONG )
108106
107+ override fun serialize (encoder : Encoder , value : CborInt <* >) {
108+ when (value) {
109+ is CborNegativeInt -> encoder.encodeSerializableValue(CborNegativeIntSerializer , value)
110+ is CborPositiveInt -> encoder.encodeSerializableValue(CborPositiveIntSerializer , value)
111+ }
112+ }
113+
114+ override fun deserialize (decoder : Decoder ): CborInt <* > {
115+ val result = decoder.asCborDecoder().decodeCborElement()
116+ if (result !is CborInt <* >) throw CborDecodingException (" Unexpected CBOR element, expected CborInt, had ${result::class } " )
117+ return result
118+ }
119+ }
120+
121+ internal object CborNegativeIntSerializer : KSerializer<CborNegativeInt> {
122+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborNegativeInt" , PrimitiveKind .LONG )
123+
109124 override fun serialize (encoder : Encoder , value : CborNegativeInt ) {
110- encoder.asCborEncoder()
125+ val cborEncoder= encoder.asCborEncoder()
126+ cborEncoder.encodeTags(value)
111127 encoder.encodeLong(value.value)
112128 }
113129
@@ -117,12 +133,13 @@ internal object CborIntSerializer : KSerializer<CborNegativeInt> {
117133 }
118134}
119135
120- internal object CborUIntSerializer : KSerializer<CborPositiveInt> {
121- override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" CborUInt " , PrimitiveKind .LONG )
136+ internal object CborPositiveIntSerializer : KSerializer<CborPositiveInt> {
137+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborPositiveInt " , PrimitiveKind .LONG )
122138
123139 override fun serialize (encoder : Encoder , value : CborPositiveInt ) {
124- encoder.asCborEncoder()
125- encoder.encodeInline(descriptor).encodeSerializableValue(ULong .serializer(), value.value)
140+ val cborEncoder= encoder.asCborEncoder()
141+ cborEncoder.encodeTags(value)
142+ encoder.encodeInline(descriptor).encodeSerializableValue(ULong .serializer(), value.value as ULong )
126143 }
127144
128145 override fun deserialize (decoder : Decoder ): CborPositiveInt {
@@ -135,7 +152,8 @@ internal object CborDoubleSerializer : KSerializer<CborDouble> {
135152 override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborDouble" , PrimitiveKind .DOUBLE )
136153
137154 override fun serialize (encoder : Encoder , value : CborDouble ) {
138- encoder.asCborEncoder()
155+ val cborEncoder= encoder.asCborEncoder()
156+ cborEncoder.encodeTags(value)
139157 encoder.encodeDouble(value.value)
140158 }
141159
@@ -154,7 +172,8 @@ internal object CborStringSerializer : KSerializer<CborString> {
154172 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborString" , PrimitiveKind .STRING )
155173
156174 override fun serialize (encoder : Encoder , value : CborString ) {
157- encoder.asCborEncoder()
175+ val cborEncoder= encoder.asCborEncoder()
176+ cborEncoder.encodeTags(value)
158177 encoder.encodeString(value.value)
159178 }
160179
@@ -175,7 +194,8 @@ internal object CborBooleanSerializer : KSerializer<CborBoolean> {
175194 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborBoolean" , PrimitiveKind .BOOLEAN )
176195
177196 override fun serialize (encoder : Encoder , value : CborBoolean ) {
178- encoder.asCborEncoder()
197+ val cborEncoder= encoder.asCborEncoder()
198+ cborEncoder.encodeTags(value)
179199 encoder.encodeBoolean(value.value)
180200 }
181201
@@ -196,7 +216,8 @@ internal object CborByteStringSerializer : KSerializer<CborByteString> {
196216 PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborByteString" , PrimitiveKind .STRING )
197217
198218 override fun serialize (encoder : Encoder , value : CborByteString ) {
199- val cborEncoder = encoder.asCborEncoder()
219+ val cborEncoder= encoder.asCborEncoder()
220+ cborEncoder.encodeTags(value)
200221 cborEncoder.encodeByteString(value.value)
201222 }
202223
0 commit comments