shamblett / cbor

A CBOR implementation for Dart
MIT License
36 stars 15 forks source link

Support for packed CBOR #16

Closed jaredwolff closed 1 year ago

jaredwolff commented 3 years ago

I'm using some packed CBOR where the keys are set as integers rather than strings. As far as I can tell in my testing, this is functionality this is not supported by this package. It'd be a great addition though!

If I have a chance I'll check out the codebase and see if I can make that improvement myself.

Thanks for this awesome package!

shamblett commented 3 years ago

From what I recall the package does support the encoding of integer keys using the MapBuilder class but not decoding to json of any map keys other than integer. As far as I remember this is a limitation of the the encode function from the convert.json package, from the docs -

/// Converts [value] to a JSON string.
  ///
  /// If value contains objects that are not directly encodable to a JSON
  /// string (a value that is not a number, boolean, string, null, list or a map
  /// with string keys), the [toEncodable] function is used to convert it to an
  /// object that must be directly encodable.
  ///
  /// If [toEncodable] is omitted, it defaults to a function that returns the
  /// result of calling `.toJson()` on the unencodable object.

This however is only a limitation I believe if you decode to JSON, a couple of solutions are to write an toEncodable function for integer keys or use Cbor.getDecodedData to get the list representation and perform your own processing on it.

You are correct in that this area is somewhat confusing at best and could do with some looking at, please feel free to submit any pull requests you wish.

nicbn commented 2 years ago

It should now be possible to use any CBOR value (including integers) as keys for dictionaries