Skip to content

Support encoding and decoding CBOR Byte Strings (major type 2) #842

@Chris-Corea

Description

@Chris-Corea

What is your use-case and why do you need this feature?

Use Case: I need to send a CBOR payload to an external device, who's firmware expects certain data to be in the Byte String format.

As I can't modify the firmware to accept an array of bytes as opposed to a byte string, I need this library to support encoding CBOR's Byte String type.

Describe the solution you'd like

As described by the Major Types section in CBOR RFC-7049, CBOR includes a major type specifically for byte strings. In instances where a service or receiver expects a byte string, a user of this library's existing CBOR serialization implementation would be unable to send a byte string as the Cbor class only includes support for the Text, Negative Integer, Array and Map major types.

Ideally, as a user of the library, it would be great to be able to either

  1. Wrap my ByteArray in a type that has its own serializer (could probably be the same as the ByteArraySerializer) and a descriptor that defaults to a CBOR specific SerialKind (BYTE_STRING); or

  2. Use a ByteStringSerializer() to provide to my own custom serializer; or

  3. Create my own serializer within my class and instruct the encoder to explicitly encode a provided ByteArray as a ByteString.

The Cbor class would need to include the byte string header type (0b010_00000) in its Default companion object and update the CborWriter, CborEncoder, CborReader, and CborDecoder to handle that type appropriately.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions