mozilla-services / canonicaljson-rs

Rust implementation of a Canonical JSON serializer
MIT License
5 stars 2 forks source link

Keys are in wrong order if `preserve_order` feature is active in `serde_json` #5

Open obmarg opened 4 years ago

obmarg commented 4 years ago

I've got two services using canonical_json to output some JSON for signing. Unfortunately, they disagree on the order of keys in their output. One of them correctly outputs in lexicographical order, one of them does not.

I've tracked this down to the preserve_order feature flag in serde_json. If this is present then serde_json::map::Map uses an IndexMap rather than a BTreeMap, and it seems canonical_json relies on this BTreeMap to output data in the correct order.

This would be relatively easy to fix if I'd requested the preserve_order flag myself but I didn't. There's a transient dependency somewhere in my tree that's requested it...

alexforster commented 1 year ago

This appears to be a limitation in the serde_json::ser::Formatter trait. It does not allow implementors to control the order of keys. See serde-rs/json#945.