-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difference ignoring order of elements in an array. #33
Comments
Hello, If your values would have beed like these:
they would produce empty diff. The reason for such an approach is to reduce computational complexity, and main usecase for the initial implementation was to address non-semantic differences in swagger parameters. Solving general case like yours would need some quadratic recursive comparisons to understand that a particular array item is equal to another one. Is this case important for you? I think it can be implemented as some additional option with a performance impact disclaimer. |
Thanks for the swift reply! Unfortunately, I can't influence the format of the JSON object because it is provided by an API. So this would actually be an important feature for me. Maybe I can customize the code of JSONDiff if you show me where to find the corresponding code parts? |
@webpunk I have an idea how to implement rearrangement based on elements equality with reasonable performance, optimistically will make it in couple of days. |
@vearutop yes, that would be great! Please, let me know if I can help... |
Hi @vearutop ! Thanks a lot for the new version. I have just tested it. For the following two JSON objects, JSONDiff still reports a difference that I can't see. |
@vearutop unfortunately, there still seems to be a problem with an array of strings. I've attached another example. Thx! |
@webpunk I've tried this example with a test case: public function testExample2() {
$ex1 = json_decode(file_get_contents(__DIR__ . '/../assets/Example1.json'));
$ex2 = json_decode(file_get_contents(__DIR__ . '/../assets/Example2.json'));
$diff = new JsonDiff($ex1, $ex2, JsonDiff::REARRANGE_ARRAYS);
$this->assertEquals(0, $diff->getDiffCnt());
} and the test passed (no differences found). Could you elaborate the problem that you are seeing with array of strings, or maybe steps to reproduce? |
@vearutop Sorry... that was a mistake on my part! Now it works perfectly. Thanks a lot! |
Sometimes ordering of fields
code
|
Hi,
is there a way to get the difference between two JSON objects while ignoring the order of elements in an array. For instance, the following example currently returns 4 differences but should return 0.
$diff = new \Swaggest\JsonDiff\JsonDiff( json_decode('{"data": [{"A": 1},{"B": 2}]}'), json_decode('{"data": [{"B": 2},{"A": 1}]}'), \Swaggest\JsonDiff\JsonDiff::REARRANGE_ARRAYS ); echo "\n" . $diff->getDiffCnt() . "\n";
Thanks!
The text was updated successfully, but these errors were encountered: