@@ -38,7 +38,7 @@ internal sealed class CborWriter(
3838
3939 class Data (val bytes : ByteArrayOutput , var elementCount : Int )
4040
41- internal abstract fun getDestination (): ByteArrayOutput
41+ protected abstract fun getDestination (): ByteArrayOutput
4242
4343 override val serializersModule: SerializersModule
4444 get() = cbor.serializersModule
@@ -148,7 +148,7 @@ internal sealed class CborWriter(
148148 return true
149149 }
150150
151- internal fun encodeTag ( tag : ULong ) = getDestination().encodeTag(tag )
151+ internal abstract fun encodeTags ( tags : ULongArray )
152152}
153153
154154
@@ -183,6 +183,8 @@ internal class IndefiniteLengthCborWriter(cbor: Cbor, private val output: ByteAr
183183 override fun incrementChildren () {/* NOOP*/
184184 }
185185
186+ override fun encodeTags (tags : ULongArray ) = tags.forEach { getDestination().encodeTag(it) }
187+
186188}
187189
188190// optimized indefinite length encoder
@@ -234,19 +236,19 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
234236
235237 private val stack = ArrayDeque <CborContainer >()
236238 private var currentElement: CborContainer ? = null
239+
237240 // value tags are collects inside beginStructure, so we need to cache them here and write them in beginStructure or encodeXXX
238241 // and then null them out, so there are no leftovers
239- private var nextValueTags : ULongArray = ulongArrayOf()
242+ private var nextValueTags: ULongArray = ulongArrayOf()
240243 get() {
241- val ret= field
242- field= ulongArrayOf()
244+ val ret = field
245+ field = ulongArrayOf()
243246 return ret
244247 }
245248
246249 fun finalize () = currentElement!! .finalize()
247250
248251 override fun beginStructure (descriptor : SerialDescriptor ): CompositeEncoder {
249- // TODO check if cborelement and be done
250252 val tags = nextValueTags + (descriptor.getObjectTags() ? : ulongArrayOf())
251253 val element = if (descriptor.hasArrayTag()) {
252254 CborContainer .List (tags)
@@ -270,7 +272,7 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
270272 }
271273 }
272274
273- override fun getDestination () = throw IllegalStateException (" There is not byteArrayOutput " )
275+ override fun getDestination () = throw IllegalStateException (" There is no byteArrayInput " )
274276
275277 override fun incrementChildren () {
276278 /* NOOP*/
@@ -295,67 +297,76 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(cbor) {
295297 if (cbor.configuration.preferCborLabelsOverNames && cborLabel != null ) {
296298 currentElement + = CborInt (value = cborLabel, tags = keyTags ? : ulongArrayOf())
297299 } else {
298- currentElement + = CborString (name, keyTags ? : ulongArrayOf())
300+ currentElement + = CborString (name, tags = keyTags ? : ulongArrayOf())
299301 }
300302 }
301303 }
302304
303305 if (cbor.configuration.encodeValueTags) {
304- descriptor.getValueTags(index)? .let { valueTags ->
306+ descriptor.getValueTags(index).let { valueTags ->
305307 // collect them for late encoding in beginStructure or encodeXXX
306- nextValueTags = valueTags? : ulongArrayOf()
308+ nextValueTags = valueTags ? : ulongArrayOf()
307309 }
308310 }
309311 return true
310312 }
311313
314+
315+ override fun encodeTags (tags : ULongArray ) {
316+ nextValueTags = tags
317+ }
318+
312319 override fun encodeBoolean (value : Boolean ) {
313320 currentElement + = CborBoolean (value, nextValueTags)
314321 }
315322
316323 override fun encodeByte (value : Byte ) {
317- currentElement + = CborInt (value.toLong(), nextValueTags)
324+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
318325 }
319326
320327 override fun encodeChar (value : Char ) {
321- currentElement + = CborInt (value.code.toLong(), nextValueTags)
328+ currentElement + = CborInt (value.code.toLong(), tags = nextValueTags)
322329 }
323330
324331 override fun encodeDouble (value : Double ) {
325- currentElement + = CborDouble (value, nextValueTags)
332+ currentElement + = CborDouble (value, tags = nextValueTags)
326333 }
327334
328335 override fun encodeFloat (value : Float ) {
329- currentElement + = CborDouble (value.toDouble(), nextValueTags)
336+ currentElement + = CborDouble (value.toDouble(), tags = nextValueTags)
330337 }
331338
332339 override fun encodeInt (value : Int ) {
333- currentElement + = CborInt (value.toLong(), nextValueTags)
340+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
334341 }
335342
336343 override fun encodeLong (value : Long ) {
337- currentElement + = CborInt (value, nextValueTags)
344+ currentElement + = CborInt (value, tags = nextValueTags)
338345 }
339346
340347 override fun encodeShort (value : Short ) {
341- currentElement + = CborInt (value.toLong(), nextValueTags)
348+ currentElement + = CborInt (value.toLong(), tags = nextValueTags)
342349 }
343350
344351 override fun encodeString (value : String ) {
345- currentElement + = CborString (value, nextValueTags)
352+ currentElement + = CborString (value, tags = nextValueTags)
346353 }
347354
348355 override fun encodeByteString (byteArray : ByteArray ) {
349- currentElement + = CborByteString (byteArray, nextValueTags)
356+ currentElement + = CborByteString (byteArray, tags = nextValueTags)
350357 }
351358
352359 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)
355366 }
356367
357368 override fun encodeEnum (enumDescriptor : SerialDescriptor , index : Int ) {
358- currentElement + = CborString (enumDescriptor.getElementName(index), nextValueTags)
369+ currentElement + = CborString (enumDescriptor.getElementName(index), tags = nextValueTags)
359370 }
360371
361372}
@@ -372,6 +383,8 @@ internal class DefiniteLengthCborWriter(cbor: Cbor, output: ByteArrayOutput) : C
372383 structureStack.peek().elementCount++
373384 }
374385
386+ override fun encodeTags (tags : ULongArray ) = tags.forEach { getDestination().encodeTag(it) }
387+
375388 override fun beginStructure (descriptor : SerialDescriptor ): CompositeEncoder {
376389 val current = Data (ByteArrayOutput (), 0 )
377390 structureStack.push(current)
0 commit comments