cbor-redux icon indicating copy to clipboard operation
cbor-redux copied to clipboard

Add support for encoding numeric keys in maps

Open letmaik opened this issue 2 years ago • 3 comments

The library only accepts plain JS objects to represent CBOR maps, which prevents using numbers as keys. It would be good if JS Map could be supported as that allows any type as key, in which case numbers could be used.

letmaik avatar Jan 13 '22 11:01 letmaik

Assuming this would also encompass binary strings as map keys: I’d also find this useful for Perl-generated CBOR, in which case strings (including map keys) aren’t reliably text or binary.

Could this simply take the form of having CBOR maps become JS Map objects rather than plain-old objects?

FGasper avatar Mar 10 '22 19:03 FGasper

This is a really good feedback. I haven't run into this usecase for my personal projects, but I've seen this in the wild and it's pretty darn common to need non-string keys.

I haven't had a lot of time to devote to CBOR in the last year but I'm hoping to push for some fixes for this as it relates to some server-side work in Deno this month.

I haven't lost sight and plan on getting this in soon.

aaronhuggins avatar Apr 05 '22 16:04 aaronhuggins

Started work on this: https://github.com/aaronhuggins/cbor-redux/commit/5adf15e04d33f9eae480ce9d57d11751693ff87a

I'm also going to provide an options API somehow, so that you can choose to use Map or Object as your dictionary for decode; this would allow you to preserve non-string keys on parsing.

aaronhuggins avatar Apr 05 '22 21:04 aaronhuggins