json icon indicating copy to clipboard operation
json copied to clipboard

`PartialEq` implementation of `Value` takes order of items in an array into account

Open Erik1000 opened this issue 1 year ago • 1 comments

I didn't find anything specific to array equality in the JSON spec, but in my opinion, this should be considered equal:

use serde_json::json;

fn main() {
  let a = json!({
    "an_array": ["foo", "bar"]
  });
  let b = json!({
    "an_array": ["bar", "foo"]
  });
  assert_eq!(a, b);
}

but it's not:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Object {"an_array": Array [String("foo"), String("bar")]}`,
 right: `Object {"an_array": Array [String("bar"), String("foo")]}`', src/main.rs:10:3

If you think this should not be considered equal in the PartialEq implementation, I would at least suggest a special method which ignores the order if elements.

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ac36a37ac68e6997178562eb47f2b1fb

Erik1000 avatar Sep 07 '22 00:09 Erik1000

Actually, since I dealt with sets, I ignored the fact that the index of array members does matter. In this case the implementation is probably correct.

Erik1000 avatar Sep 07 '22 01:09 Erik1000