Comments (6)
var_dump(json_decode('{}', true) === json_decode('[]', true)); // true
var_dump(json_decode('{"0":0,"1":1}', true) === json_decode('[0,1]')); // true
In your last example you are not decoding json into associative array, this precisely represents json.
In your original post you are decoding json into associative array, this can lead to ambiguous representation in json.
Please check #17 discussion for additional information.
from json-diff.
The library does not support associative arrays by default because they can not precisely represent JSON. Though there is a compatibility option JsonDiff::TOLERATE_ASSOCIATIVE_ARRAYS
.
Please try
$array1 = json_decode('{"someData":{"1":{"s":"e","val":0.01009999999999999960309526869650653679855167865753173828125,"eIds":["38","191"]},"2":{"s":"y"},"3":{"s":"y"},"4":{"s":"y"},"5":{"s":"y"},"6":{"s":"y"},"24":{"s":"n"},"32":{"s":"y"},"37":{"s":"y"},"61":{"s":"y"},"62":{"s":"y"},"63":{"s":"y"},"64":{"s":"y"},"79":{"s":"y"},"80":{"s":"y"},"81":{"s":"y"},"82":{"s":"y"},"83":{"s":"y"},"84":{"s":"y"},"85":{"s":"y"},"86":{"s":"y"},"88":{"s":"e","eIds":["54"]},"89":{"s":"e","eIds":["54"]},"107":{"s":"y"},"108":{"s":"y"},"109":{"s":"y"},"113":{"s":"y"},"114":{"s":"y"},"122":{"s":"y"},"123":{"s":"y"},"126":{"s":"y"},"127":{"s":"y"},"128":{"s":"y"},"129":{"s":"y"},"130":{"s":"y"},"131":{"s":"y"},"132":{"s":"y"},"133":{"s":"y"},"158":{"s":"y"},"159":{"s":"y"},"160":{"s":"y"},"161":{"s":"y"},"162":{"s":"y"},"165":{"s":"y"},"166":{"s":"y"},"173":{"s":"y"},"174":{"s":"y"},"175":{"s":"y"},"176":{"s":"y"},"177":{"s":"y"},"178":{"s":"y"},"179":{"s":"y"},"181":{"s":"y"},"184":{"s":"y"},"185":{"s":"y"},"186":{"s":"y"},"187":{"s":"y"},"188":{"s":"y"},"191":{"s":"e","eIds":["54"]},"196":{"s":"y"},"197":{"s":"y"},"198":{"s":"y"},"200":{"s":"y"},"202":{"s":"y"},"203":{"s":"y"},"204":{"s":"y"},"205":{"s":"y"},"206":{"s":"y"},"207":{"s":"y"},"208":{"s":"y"},"209":{"s":"y"},"210":{"s":"y"},"211":{"s":"y"},"220":{"s":"y"},"221":{"s":"y"},"222":{"s":"y"},"223":{"s":"y"},"224":{"s":"y"},"225":{"s":"y"},"226":{"s":"y"},"227":{"s":"y"},"228":{"s":"y"},"229":{"s":"y"},"257":{"s":"y"},"258":{"s":"y"},"259":{"s":"y"},"260":{"s":"y"},"261":{"s":"y"},"262":{"s":"y"},"263":{"s":"y"},"264":{"s":"y"},"265":{"s":"y"},"266":{"s":"y"},"267":{"s":"y"},"268":{"s":"y"},"269":{"s":"y"},"270":{"s":"y"},"271":{"s":"y"},"272":{"s":"y"},"273":{"s":"y"},"274":{"s":"y"},"275":{"s":"y"},"276":{"s":"y"},"277":{"s":"y"},"278":{"s":"y"},"279":{"s":"y"},"280":{"s":"y"},"281":{"s":"y"},"282":{"s":"y"},"283":{"s":"y"},"284":{"s":"y"},"285":{"s":"y"},"286":{"s":"y"},"287":{"s":"y"},"308":{"s":"y"},"309":{"s":"y"},"317":{"s":"y"},"320":{"s":"y"},"321":{"s":"y"},"322":{"s":"y"},"327":{"s":"y"},"328":{"s":"y"},"329":{"s":"y"},"330":{"s":"y"},"331":{"s":"y"},"336":{"s":"y"},"337":{"s":"y"},"342":{"s":"y"},"343":{"s":"n","val":100},"350":{"s":"y"},"352":{"s":"y"},"353":{"s":"y"},"358":{"s":"y"},"364":{"s":"y"},"365":{"s":"y"},"371":{"s":"e","eIds":["54"]},"380":{"s":"y"},"381":{"s":"y"},"382":{"s":"y"},"408":{"s":"y"},"409":{"s":"y"},"410":{"s":"y"},"411":{"s":"y"},"419":{"s":"y"},"420":{"s":"y"},"421":{"s":"n"},"430":{"s":"y"},"431":{"s":"y"},"432":{"s":"y"},"433":{"s":"y"},"434":{"s":"y"},"435":{"s":"y"},"436":{"s":"y"},"437":{"s":"y"}}}', true);
$array2 = json_decode('{"someData":{"22":{"s":"e","eIds":["48"]},"23":{"s":"e","eIds":["235"]}}}', true);
$r = new JsonDiff($array1, $array2, JsonDiff::REARRANGE_ARRAYS + JsonDiff::TOLERATE_ASSOCIATIVE_ARRAYS);
$patchArray = json_decode(json_encode($r->getPatch()), true);
foreach ($patchArray as $array) {
echo json_encode($array) . "\n\n";
}
from json-diff.
Thanks, it did solve the problem.
What do you mean by saying?
The library does not support associative arrays by default because they can not precisely represent JSON.
It's actually the JSON which can't represent fully the associative arrays (with their respective order) not vice versa.
from json-diff.
JSON values of {}
and []
have identical ambiguous representation with associative array. Somewhat similar problems happen to {"0":0,"1":1}
and [0,1]
.
from json-diff.
I am still trying to understand the problem. I am sure you've spent plenty of time and are familiar with lots of specifics and gotchas that I am missing, but I don't think your statement is true: "JSON values of {} and [] have identical ambiguous representation with associative array." at least for PHP 7.2.17 that I am using.
Please find below results:
json_encode(new stdClass()); -> {}
json_encode([]); -> []
json_decode('[]'); -> {array} [0]
json_decode('{}'); -> {stdClass} [0]
json_decode('{"0":0,"1":1}'); -> {stdClass} [2]
0 = 0
1 = 1
json_decode('[0,1]'); -> {array} [2]
0 = 0
1 = 1
from json-diff.
Wow, thank you for that explanation. It's clear now. Personally I find conventions pretty hard topic.
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
- getModifiedDiff() HOT 4
- Deprecated dependencies HOT 2
- Remove last elemnt followed by add converting to associative array HOT 1
- Difference ignoring order of elements in an array. HOT 11
- 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.