Comments (11)
@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.
@vearutop Sorry... that was a mistake on my part! Now it works perfectly. Thanks a lot!
from json-diff.
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.
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.
@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.
@vearutop yes, that would be great! Please, let me know if I can help...
from json-diff.
@webpunk please check if new v3.8.0
works for you. No new option needed, JsonDiff::REARRANGE_ARRAYS
should suffice.
from json-diff.
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.
from json-diff.
@webpunk thanks for the test case, please check v3.8.1
.
from json-diff.
@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.
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)
- Implement merge diff/patch
- Warning: A non-numeric value encountered HOT 1
- Library only works with stdClass not with associative arrays HOT 4
- JsonDiff from dictionary like array, getting duplicated path removal operations HOT 6
- getModifiedDiff() HOT 4
- Deprecated dependencies HOT 2
- Remove last elemnt followed by add converting to associative array HOT 1
- Thank you 👍 HOT 1
- Issue with comparing arrays HOT 2
- Invalid key for array operation HOT 1
- Wrong indexes of removed array items HOT 1
- Removing an item from the array when option REARRANGE_ARRAYS is enabled HOT 2
- Enhancement: Include failed operation in Exception HOT 2
- Question: Visualise the difference
- getPatch with REARRANGE_ARRAYS is not returning the correct paths when items are removed from an array HOT 1
- Class "Swaggest\JsonDiff\JsonDiff" not found HOT 1
- Improve JsonPatch exceptions HOT 1
- Add option for exclute test operation
- Unwanted count() exception in JsonPointer.php
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-diff.