serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.7k stars 536 forks source link

impl `Hash` for `Value` #1127

Closed edwardycl closed 2 weeks ago

edwardycl commented 2 months ago

IndexMap is the only type that is used within Value that does not implement Hash, and it is by design: https://github.com/indexmap-rs/indexmap/issues/288#issuecomment-1804735361

If "preserve_order" feature is not set, Value::Object uses BTreeMap instead of IndexMap so it is possible to auto-impl Hash for Value.

edwardycl commented 2 months ago

Now that I think about it, since the type of key is fixed to String, IndexMap can also be hashed by sorting its key-value in a consistent order. It doesn't break the Eq-Hash relationship, and we can implement Hash regardless of the "preserve_order" feature. What do you think?