@@ -27,8 +27,8 @@ internal object CborElementSerializer : KSerializer<CborElement> {
27
27
element(" CborMap" , defer { CborMapSerializer .descriptor })
28
28
element(" CborList" , defer { CborListSerializer .descriptor })
29
29
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 })
32
32
}
33
33
34
34
override fun serialize (encoder : Encoder , value : CborElement ) {
@@ -54,21 +54,17 @@ internal object CborElementSerializer : KSerializer<CborElement> {
54
54
*/
55
55
internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
56
56
override val descriptor: SerialDescriptor =
57
- buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PrimitiveKind . STRING )
57
+ buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PolymorphicKind . SEALED )
58
58
59
59
override fun serialize (encoder : Encoder , value : CborPrimitive <* >) {
60
- val cborEncoder = encoder.asCborEncoder()
61
-
62
- cborEncoder.encodeTags(value)
63
-
64
60
when (value) {
65
61
is CborNull -> encoder.encodeSerializableValue(CborNullSerializer , value)
66
62
is CborString -> encoder.encodeSerializableValue(CborStringSerializer , value)
67
63
is CborBoolean -> encoder.encodeSerializableValue(CborBooleanSerializer , value)
68
64
is CborByteString -> encoder.encodeSerializableValue(CborByteStringSerializer , value)
69
65
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)
72
68
}
73
69
}
74
70
@@ -89,7 +85,8 @@ internal object CborNullSerializer : KSerializer<CborNull> {
89
85
buildSerialDescriptor(" kotlinx.serialization.cbor.CborNull" , SerialKind .ENUM )
90
86
91
87
override fun serialize (encoder : Encoder , value : CborNull ) {
92
- encoder.asCborEncoder()
88
+ val cborEncoder= encoder.asCborEncoder()
89
+ cborEncoder.encodeTags(value)
93
90
encoder.encodeNull()
94
91
}
95
92
@@ -103,11 +100,30 @@ internal object CborNullSerializer : KSerializer<CborNull> {
103
100
}
104
101
}
105
102
106
- internal object CborIntSerializer : KSerializer<CborNegativeInt> {
103
+
104
+ internal object CborIntSerializer : KSerializer<CborInt<*>> {
107
105
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborInt" , PrimitiveKind .LONG )
108
106
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
+
109
124
override fun serialize (encoder : Encoder , value : CborNegativeInt ) {
110
- encoder.asCborEncoder()
125
+ val cborEncoder= encoder.asCborEncoder()
126
+ cborEncoder.encodeTags(value)
111
127
encoder.encodeLong(value.value)
112
128
}
113
129
@@ -117,12 +133,13 @@ internal object CborIntSerializer : KSerializer<CborNegativeInt> {
117
133
}
118
134
}
119
135
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 )
122
138
123
139
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 )
126
143
}
127
144
128
145
override fun deserialize (decoder : Decoder ): CborPositiveInt {
@@ -135,7 +152,8 @@ internal object CborDoubleSerializer : KSerializer<CborDouble> {
135
152
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborDouble" , PrimitiveKind .DOUBLE )
136
153
137
154
override fun serialize (encoder : Encoder , value : CborDouble ) {
138
- encoder.asCborEncoder()
155
+ val cborEncoder= encoder.asCborEncoder()
156
+ cborEncoder.encodeTags(value)
139
157
encoder.encodeDouble(value.value)
140
158
}
141
159
@@ -154,7 +172,8 @@ internal object CborStringSerializer : KSerializer<CborString> {
154
172
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborString" , PrimitiveKind .STRING )
155
173
156
174
override fun serialize (encoder : Encoder , value : CborString ) {
157
- encoder.asCborEncoder()
175
+ val cborEncoder= encoder.asCborEncoder()
176
+ cborEncoder.encodeTags(value)
158
177
encoder.encodeString(value.value)
159
178
}
160
179
@@ -175,7 +194,8 @@ internal object CborBooleanSerializer : KSerializer<CborBoolean> {
175
194
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborBoolean" , PrimitiveKind .BOOLEAN )
176
195
177
196
override fun serialize (encoder : Encoder , value : CborBoolean ) {
178
- encoder.asCborEncoder()
197
+ val cborEncoder= encoder.asCborEncoder()
198
+ cborEncoder.encodeTags(value)
179
199
encoder.encodeBoolean(value.value)
180
200
}
181
201
@@ -196,7 +216,8 @@ internal object CborByteStringSerializer : KSerializer<CborByteString> {
196
216
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborByteString" , PrimitiveKind .STRING )
197
217
198
218
override fun serialize (encoder : Encoder , value : CborByteString ) {
199
- val cborEncoder = encoder.asCborEncoder()
219
+ val cborEncoder= encoder.asCborEncoder()
220
+ cborEncoder.encodeTags(value)
200
221
cborEncoder.encodeByteString(value.value)
201
222
}
202
223
0 commit comments