Giter Site home page Giter Site logo

Comments (11)

webpunk avatar webpunk commented on June 22, 2024 2

@vearutop unfortunately, there still seems to be a problem with an array of strings. I've attached another example. Thx!
Example2.zip

from json-diff.

webpunk avatar webpunk commented on June 22, 2024 1

@vearutop Sorry... that was a mistake on my part! Now it works perfectly. Thanks a lot!

from json-diff.

vearutop avatar vearutop commented on June 22, 2024

Hello, REARRANGE_ARRAYS is a best effort feature that targets homogenic arrays.
In order to be rearranged, both arrays have to have at least one key that is avaialble in all items and that key needs to have unique values in original array.

If your values would have beed like these:

{"data": [{"A": 1, "C": 1},{"B": 2, "C": 2}]}
{"data": [{"B": 2, "C": 2},{"A": 1, "C": 1}]}

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.

from json-diff.

webpunk avatar webpunk commented on June 22, 2024

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?

from json-diff.

vearutop avatar vearutop commented on June 22, 2024

@webpunk I have an idea how to implement rearrangement based on elements equality with reasonable performance, optimistically will make it in couple of days.

from json-diff.

webpunk avatar webpunk commented on June 22, 2024

@vearutop yes, that would be great! Please, let me know if I can help...

from json-diff.

vearutop avatar vearutop commented on June 22, 2024

@webpunk please check if new v3.8.0 works for you. No new option needed, JsonDiff::REARRANGE_ARRAYS should suffice.

from json-diff.

webpunk avatar webpunk commented on June 22, 2024

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.

Example.zip

from json-diff.

vearutop avatar vearutop commented on June 22, 2024

@webpunk thanks for the test case, please check v3.8.1.

from json-diff.

vearutop avatar vearutop commented on June 22, 2024

@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?

from json-diff.

melnikaite avatar melnikaite commented on June 22, 2024

Sometimes ordering of fields name and code swaps, having REARRANGE_ARRAYS it supposed to be ignored, but in fact treated as difference

{
    "id": 826907,
    "contracts": [
        {
            "id": 715559,
            "product": {
                "name": "Friends",
                "code": "friends"
            }
        }
    ]
}
code
<?php

require __DIR__ . '/../vendor/autoload.php';

use Swaggest\JsonDiff\JsonDiff;

$v1 = <<<JSON
{
    "id": 826907,
    "contracts": [
        {
            "id": 715559,
            "product": {
                "code": "friends",
                "name": "Friends"
            }
        }
    ]
}
JSON;
$v2 = <<<JSON
{
    "id": 826907,
    "contracts": [
        {
            "id": 715559,
            "product": {
                "name": "Friends",
                "code": "friends"
            }
        }
    ]
}
JSON;

$diff = new JsonDiff($v1, $v2, JsonDiff::REARRANGE_ARRAYS);
$patch = json_encode($diff->getPatch()->jsonSerialize());
echo $patch;

from json-diff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.