Skip to content

Commit 674e7ef

Browse files
committed
polishing
1 parent 10748f9 commit 674e7ef

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

formats/cbor/commonMain/src/kotlinx/serialization/cbor/CborDecoder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface CborDecoder : Decoder {
3636
* Decodes the next element in the current input as [CborElement].
3737
* The type of the decoded element depends on the current state of the input and, when received
3838
* by [serializer][KSerializer] in its [KSerializer.serialize] method, the type of the token directly matches
39-
* the [kind][SerialDescriptor.kind].
39+
* the [kind][kotlinx.serialization.descriptors.SerialDescriptor.kind].
4040
*
4141
* This method is allowed to invoke only as the part of the whole deserialization process of the class,
4242
* calling this method after invoking [beginStructure] or any `decode*` method will lead to unspecified behaviour.

formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/Decoder.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ internal class StructuredCborParser(internal val element: CborElement, private v
595595
isMap = false
596596
val list = currentElement as CborList
597597
listIterator = list.listIterator()
598-
return -1 //just let the iterator run out of elements
598+
return list.size
599599
}
600600

601601
override fun startMap(tags: ULongArray?): Int {
@@ -608,9 +608,10 @@ internal class StructuredCborParser(internal val element: CborElement, private v
608608
isMap = true
609609

610610
val map = currentElement as CborMap
611-
//zip key, value, key, value, ... pairs to mirror byte-layout of CBOR map
611+
// zip key, value, key, value, ... pairs to mirror byte-layout of CBOR map, so decoding this here works the same
612+
// as decoding from bytes
612613
listIterator = map.entries.flatMap { listOf(it.key, it.value) }.listIterator()
613-
return -1// just let the iterator run out of elements
614+
return map.size
614615
}
615616

616617
override fun nextNull(tags: ULongArray?): Nothing? {
@@ -677,9 +678,14 @@ internal class StructuredCborParser(internal val element: CborElement, private v
677678
}
678679
}
679680

681+
/**
682+
* Verify the current element's object tags and advance to the next element if inside a list/map.
683+
* The reason this method mixes two behaviours is that decoding a primitive is invoked on a single element.
684+
* `decodeElementIndex`, etc. is invoked on an iterable and there are key tags and value tags
685+
*/
680686
private fun processTags(tags: ULongArray?): ULongArray? {
681687

682-
// If we're in a list, advance to the next element
688+
// If we're in a list/map, advance to the next element
683689
if (listIterator != null && listIterator!!.hasNext()) {
684690
currentElement = listIterator!!.next()
685691
}
@@ -707,7 +713,6 @@ internal class StructuredCborParser(internal val element: CborElement, private v
707713

708714
override fun skipElement(tags: ULongArray?) {
709715
// Process tags but don't do anything with the element
710-
//TODO check for maps
711716
processTags(tags)
712717
}
713718
}

0 commit comments

Comments
 (0)