@@ -38,7 +38,7 @@ internal sealed class CborWriter(
38
38
39
39
class Data (val bytes : ByteArrayOutput , var elementCount : Int )
40
40
41
- internal abstract fun getDestination (): ByteArrayOutput
41
+ protected abstract fun getDestination (): ByteArrayOutput
42
42
43
43
override val serializersModule: SerializersModule
44
44
get() = cbor.serializersModule
@@ -148,7 +148,7 @@ internal sealed class CborWriter(
148
148
return true
149
149
}
150
150
151
- internal fun encodeTag ( tag : ULong ) = getDestination().encodeTag(tag )
151
+ internal abstract fun encodeTags ( tags : ULongArray )
152
152
}
153
153
154
154
@@ -183,6 +183,8 @@ internal class IndefiniteLengthCborWriter(cbor: Cbor, private val output: ByteAr
183
183
override fun incrementChildren () {/* NOOP*/
184
184
}
185
185
186
+ override fun encodeTags (tags : ULongArray ) = tags.forEach { getDestination().encodeTag(it) }
187
+
186
188
}
187
189
188
190
// optimized indefinite length encoder
@@ -234,19 +236,19 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
234
236
235
237
private val stack = ArrayDeque <CborContainer >()
236
238
private var currentElement: CborContainer ? = null
239
+
237
240
// value tags are collects inside beginStructure, so we need to cache them here and write them in beginStructure or encodeXXX
238
241
// and then null them out, so there are no leftovers
239
- private var nextValueTags : ULongArray = ulongArrayOf()
242
+ private var nextValueTags: ULongArray = ulongArrayOf()
240
243
get() {
241
- val ret= field
242
- field= ulongArrayOf()
244
+ val ret = field
245
+ field = ulongArrayOf()
243
246
return ret
244
247
}
245
248
246
249
fun finalize () = currentElement!! .finalize()
247
250
248
251
override fun beginStructure (descriptor : SerialDescriptor ): CompositeEncoder {
249
- // TODO check if cborelement and be done
250
252
val tags = nextValueTags + (descriptor.getObjectTags() ? : ulongArrayOf())
251
253
val element = if (descriptor.hasArrayTag()) {
252
254
CborContainer .List (tags)
@@ -270,7 +272,7 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
270
272
}
271
273
}
272
274
273
- override fun getDestination () = throw IllegalStateException (" There is not byteArrayOutput " )
275
+ override fun getDestination () = throw IllegalStateException (" There is no byteArrayInput " )
274
276
275
277
override fun incrementChildren () {
276
278
/* NOOP*/
@@ -295,67 +297,76 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
295
297
if (cbor.configuration.preferCborLabelsOverNames && cborLabel != null ) {
296
298
currentElement + = CborInt (value = cborLabel, tags = keyTags ? : ulongArrayOf())
297
299
} else {
298
- currentElement + = CborString (name, keyTags ? : ulongArrayOf())
300
+ currentElement + = CborString (name, tags = keyTags ? : ulongArrayOf())
299
301
}
300
302
}
301
303
}
302
304
303
305
if (cbor.configuration.encodeValueTags) {
304
- descriptor.getValueTags(index)? .let { valueTags ->
306
+ descriptor.getValueTags(index).let { valueTags ->
305
307
// collect them for late encoding in beginStructure or encodeXXX
306
- nextValueTags = valueTags? : ulongArrayOf()
308
+ nextValueTags = valueTags ? : ulongArrayOf()
307
309
}
308
310
}
309
311
return true
310
312
}
311
313
314
+
315
+ override fun encodeTags (tags : ULongArray ) {
316
+ nextValueTags = tags
317
+ }
318
+
312
319
override fun encodeBoolean (value : Boolean ) {
313
320
currentElement + = CborBoolean (value, nextValueTags)
314
321
}
315
322
316
323
override fun encodeByte (value : Byte ) {
317
- currentElement + = CborInt (value.toLong(), nextValueTags)
324
+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
318
325
}
319
326
320
327
override fun encodeChar (value : Char ) {
321
- currentElement + = CborInt (value.code.toLong(), nextValueTags)
328
+ currentElement + = CborInt (value.code.toLong(), tags = nextValueTags)
322
329
}
323
330
324
331
override fun encodeDouble (value : Double ) {
325
- currentElement + = CborDouble (value, nextValueTags)
332
+ currentElement + = CborDouble (value, tags = nextValueTags)
326
333
}
327
334
328
335
override fun encodeFloat (value : Float ) {
329
- currentElement + = CborDouble (value.toDouble(), nextValueTags)
336
+ currentElement + = CborDouble (value.toDouble(), tags = nextValueTags)
330
337
}
331
338
332
339
override fun encodeInt (value : Int ) {
333
- currentElement + = CborInt (value.toLong(), nextValueTags)
340
+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
334
341
}
335
342
336
343
override fun encodeLong (value : Long ) {
337
- currentElement + = CborInt (value, nextValueTags)
344
+ currentElement + = CborInt (value, tags = nextValueTags)
338
345
}
339
346
340
347
override fun encodeShort (value : Short ) {
341
- currentElement + = CborInt (value.toLong(), nextValueTags)
348
+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
342
349
}
343
350
344
351
override fun encodeString (value : String ) {
345
- currentElement + = CborString (value, nextValueTags)
352
+ currentElement + = CborString (value, tags = nextValueTags)
346
353
}
347
354
348
355
override fun encodeByteString (byteArray : ByteArray ) {
349
- currentElement + = CborByteString (byteArray, nextValueTags)
356
+ currentElement + = CborByteString (byteArray, tags = nextValueTags)
350
357
}
351
358
352
359
override fun encodeNull () {
353
- currentElement + = if (isClass) CborMap (mapOf (), nextValueTags) /* NOT CBOR-COMPLIANT, KxS-proprietary behaviour*/
354
- else CborNull (nextValueTags)
360
+ /* NOT CBOR-COMPLIANT, KxS-proprietary behaviour*/
361
+ currentElement + = if (isClass) CborMap (
362
+ mapOf (),
363
+ tags = nextValueTags
364
+ )
365
+ else CborNull (tags = nextValueTags)
355
366
}
356
367
357
368
override fun encodeEnum (enumDescriptor : SerialDescriptor , index : Int ) {
358
- currentElement + = CborString (enumDescriptor.getElementName(index), nextValueTags)
369
+ currentElement + = CborString (enumDescriptor.getElementName(index), tags = nextValueTags)
359
370
}
360
371
361
372
}
@@ -372,6 +383,8 @@ internal class DefiniteLengthCborWriter(cbor: Cbor, output: ByteArrayOutput) : C
372
383
structureStack.peek().elementCount++
373
384
}
374
385
386
+ override fun encodeTags (tags : ULongArray ) = tags.forEach { getDestination().encodeTag(it) }
387
+
375
388
override fun beginStructure (descriptor : SerialDescriptor ): CompositeEncoder {
376
389
val current = Data (ByteArrayOutput (), 0 )
377
390
structureStack.push(current)
0 commit comments