cbor_py icon indicating copy to clipboard operation
cbor_py copied to clipboard

make sort_keys=True work

Open brianolson opened this issue 8 years ago • 2 comments

So that people can have repeatable canonical binary serializations of a json/cbor object, add a sort_keys=True feature to dump()/dumps() Follow "Canonical CBOR" section 3.9 rules for sorting

brianolson avatar Apr 10 '17 14:04 brianolson

The sorting rules in 3.9 of RFC7049 are rather explicit, relate to the encoded version of the key elements, and are most importantly not compatible with sorting unicode keys, because they specify that short representations come before long ones (something like len(a) < len(b) if len(a) != len(b) else a < b).

chrysn avatar Apr 10 '17 15:04 chrysn

ok, from section 3.9, "Canonical CBOR"

The keys in every map must be sorted lowest value to highest.
      Sorting is performed on the bytes of the representation of the key
      data items without paying attention to the 3/5 bit splitting for
      major types.  (Note that this rule allows maps that have keys of
      different types, even though that is probably a bad practice that
      could lead to errors in some canonicalization implementations.)
      The sorting rules are:

      *  If two keys have different lengths, the shorter one sorts
         earlier;

      *  If two keys have the same length, the one with the lower value
         in (byte-wise) lexical order sorts earlier.

brianolson avatar Apr 10 '17 15:04 brianolson