Skip to content

Support definite-length Arrays and Maps in CBOR #1955

@wverdese

Description

@wverdese

What is your use-case and why do you need this feature?
I want to use this library to communicate with an API that uses CBOR for payloads. This API is strictly requesting Arrays and Maps to be encoded with a definite length, because of / lack of dynamic memory allocation.
Encoding a Kotlin Array or a Kotlin Map will always result in their indefinite-length counterpart, so the API will fail when decoding my payload.

Describe the solution you'd like
This line:

Cbor.encodeToByteArray(arrayOf(mapOf(\* stuff *\)))

now serializes to:

9F                                  # array(*)
   BF                               # map(*)
      /* stuff */
      FF                            # primitive(*)
   FF                               # primitive(*)

I'd like to be able to, e.g., pass a parameter:

Cbor.encodeToByteArray(arrayOf(mapOf(\* stuff *\)), Lengths.DEFINITE)

to be able to change the serialization to:

81                          # array(1)
   A1                       # map(1)
      /* stuff */

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureup for grabsA good issue for external contribution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions