@@ -11,11 +11,13 @@ import kotlinx.serialization.cbor.*
11
11
import kotlinx.serialization.descriptors.*
12
12
import kotlinx.serialization.encoding.*
13
13
14
+ internal interface CborSerializer
15
+
14
16
/* *
15
17
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborElement].
16
18
* It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
17
19
*/
18
- internal object CborElementSerializer : KSerializer<CborElement> {
20
+ internal object CborElementSerializer : KSerializer<CborElement>, CborSerializer {
19
21
override val descriptor: SerialDescriptor =
20
22
buildSerialDescriptor(" kotlinx.serialization.cbor.CborElement" , PolymorphicKind .SEALED ) {
21
23
// Resolve cyclic dependency in descriptors by late binding
@@ -27,8 +29,8 @@ internal object CborElementSerializer : KSerializer<CborElement> {
27
29
element(" CborMap" , defer { CborMapSerializer .descriptor })
28
30
element(" CborList" , defer { CborListSerializer .descriptor })
29
31
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 })
32
34
}
33
35
34
36
override fun serialize (encoder : Encoder , value : CborElement ) {
@@ -52,23 +54,19 @@ internal object CborElementSerializer : KSerializer<CborElement> {
52
54
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborPrimitive].
53
55
* It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
54
56
*/
55
- internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
57
+ internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>>, CborSerializer {
56
58
override val descriptor: SerialDescriptor =
57
- buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PrimitiveKind . STRING )
59
+ buildSerialDescriptor(" kotlinx.serialization.cbor.CborPrimitive" , PolymorphicKind . SEALED )
58
60
59
61
override fun serialize (encoder : Encoder , value : CborPrimitive <* >) {
60
- val cborEncoder = encoder.asCborEncoder()
61
-
62
- cborEncoder.encodeTags(value)
63
-
64
62
when (value) {
65
63
is CborNull -> encoder.encodeSerializableValue(CborNullSerializer , value)
66
64
is CborString -> encoder.encodeSerializableValue(CborStringSerializer , value)
67
65
is CborBoolean -> encoder.encodeSerializableValue(CborBooleanSerializer , value)
68
66
is CborByteString -> encoder.encodeSerializableValue(CborByteStringSerializer , value)
69
67
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)
72
70
}
73
71
}
74
72
@@ -83,13 +81,14 @@ internal object CborPrimitiveSerializer : KSerializer<CborPrimitive<*>> {
83
81
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborNull].
84
82
* It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
85
83
*/
86
- internal object CborNullSerializer : KSerializer<CborNull> {
84
+ internal object CborNullSerializer : KSerializer<CborNull>, CborSerializer {
87
85
88
86
override val descriptor: SerialDescriptor =
89
87
buildSerialDescriptor(" kotlinx.serialization.cbor.CborNull" , SerialKind .ENUM )
90
88
91
89
override fun serialize (encoder : Encoder , value : CborNull ) {
92
- encoder.asCborEncoder().encodeTags(value)
90
+ val cborEncoder = encoder.asCborEncoder()
91
+ cborEncoder.encodeTags(value)
93
92
encoder.encodeNull()
94
93
}
95
94
@@ -98,16 +97,38 @@ internal object CborNullSerializer : KSerializer<CborNull> {
98
97
if (decoder.decodeNotNullMark()) {
99
98
throw CborDecodingException (" Expected 'null' literal" )
100
99
}
100
+
101
101
decoder.decodeNull()
102
102
return CborNull ()
103
103
}
104
104
}
105
105
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 )
108
128
109
129
override fun serialize (encoder : Encoder , value : CborNegativeInt ) {
110
- encoder.asCborEncoder().encodeTags(value)
130
+ val cborEncoder = encoder.asCborEncoder()
131
+ cborEncoder.encodeTags(value)
111
132
encoder.encodeLong(value.value)
112
133
}
113
134
@@ -117,12 +138,14 @@ internal object CborIntSerializer : KSerializer<CborNegativeInt> {
117
138
}
118
139
}
119
140
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 )
122
144
123
145
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 )
126
149
}
127
150
128
151
override fun deserialize (decoder : Decoder ): CborPositiveInt {
@@ -131,11 +154,13 @@ internal object CborUIntSerializer : KSerializer<CborPositiveInt> {
131
154
}
132
155
}
133
156
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 )
136
160
137
161
override fun serialize (encoder : Encoder , value : CborDouble ) {
138
- encoder.asCborEncoder().encodeTags(value)
162
+ val cborEncoder = encoder.asCborEncoder()
163
+ cborEncoder.encodeTags(value)
139
164
encoder.encodeDouble(value.value)
140
165
}
141
166
@@ -149,12 +174,13 @@ internal object CborDoubleSerializer : KSerializer<CborDouble> {
149
174
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborString].
150
175
* It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
151
176
*/
152
- internal object CborStringSerializer : KSerializer<CborString> {
177
+ internal object CborStringSerializer : KSerializer<CborString>, CborSerializer {
153
178
override val descriptor: SerialDescriptor =
154
179
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborString" , PrimitiveKind .STRING )
155
180
156
181
override fun serialize (encoder : Encoder , value : CborString ) {
157
- encoder.asCborEncoder().encodeTags(value)
182
+ val cborEncoder = encoder.asCborEncoder()
183
+ cborEncoder.encodeTags(value)
158
184
encoder.encodeString(value.value)
159
185
}
160
186
@@ -170,12 +196,13 @@ internal object CborStringSerializer : KSerializer<CborString> {
170
196
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborBoolean].
171
197
* It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
172
198
*/
173
- internal object CborBooleanSerializer : KSerializer<CborBoolean> {
199
+ internal object CborBooleanSerializer : KSerializer<CborBoolean>, CborSerializer {
174
200
override val descriptor: SerialDescriptor =
175
201
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborBoolean" , PrimitiveKind .BOOLEAN )
176
202
177
203
override fun serialize (encoder : Encoder , value : CborBoolean ) {
178
- encoder.asCborEncoder().encodeTags(value)
204
+ val cborEncoder = encoder.asCborEncoder()
205
+ cborEncoder.encodeTags(value)
179
206
encoder.encodeBoolean(value.value)
180
207
}
181
208
@@ -191,13 +218,13 @@ internal object CborBooleanSerializer : KSerializer<CborBoolean> {
191
218
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborByteString].
192
219
* It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
193
220
*/
194
- internal object CborByteStringSerializer : KSerializer<CborByteString> {
221
+ internal object CborByteStringSerializer : KSerializer<CborByteString>, CborSerializer {
195
222
override val descriptor: SerialDescriptor =
196
223
PrimitiveSerialDescriptor (" kotlinx.serialization.cbor.CborByteString" , PrimitiveKind .STRING )
197
224
198
225
override fun serialize (encoder : Encoder , value : CborByteString ) {
199
226
val cborEncoder = encoder.asCborEncoder()
200
- cborEncoder.encodeTags(value)
227
+ cborEncoder.encodeTags(value)
201
228
cborEncoder.encodeByteString(value.value)
202
229
}
203
230
@@ -213,7 +240,7 @@ internal object CborByteStringSerializer : KSerializer<CborByteString> {
213
240
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborMap].
214
241
* It can only be used by with [Cbor] format and its input ([CborDecoder] and [CborEncoder]).
215
242
*/
216
- internal object CborMapSerializer : KSerializer<CborMap> {
243
+ internal object CborMapSerializer : KSerializer<CborMap>, CborSerializer {
217
244
private object CborMapDescriptor :
218
245
SerialDescriptor by MapSerializer (CborElementSerializer , CborElementSerializer ).descriptor {
219
246
@ExperimentalSerializationApi
@@ -238,7 +265,7 @@ internal object CborMapSerializer : KSerializer<CborMap> {
238
265
* Serializer object providing [SerializationStrategy] and [DeserializationStrategy] for [CborList].
239
266
* It can only be used by with [Cbor] format an its input ([CborDecoder] and [CborEncoder]).
240
267
*/
241
- internal object CborListSerializer : KSerializer<CborList> {
268
+ internal object CborListSerializer : KSerializer<CborList>, CborSerializer {
242
269
private object CborListDescriptor : SerialDescriptor by ListSerializer(CborElementSerializer ).descriptor {
243
270
@ExperimentalSerializationApi
244
271
override val serialName: String = " kotlinx.serialization.cbor.CborList"
@@ -296,9 +323,7 @@ private fun defer(deferred: () -> SerialDescriptor): SerialDescriptor = object :
296
323
297
324
private fun CborWriter.encodeTags (value : CborElement ) { // Encode tags if present
298
325
if (value.tags.isNotEmpty()) {
299
- for (tag in value.tags) {
300
- encodeTag(tag)
301
- }
326
+ encodeTags(value.tags)
302
327
}
303
328
304
329
}
0 commit comments