Comments (4)
@Tobion @j13k please check v3.6.0, it now provides support for associative arrays.
from json-diff.
Thanks for the answer. I'm aware of the ambiguities. But most often libraries actually prefer and use associative array likes like Elastica and Doctrine JSON column type. So the only solution would be to re-encode and decode as you pointed out which is pretty bad.
What I also question is the inconsistent support of associative arrays. It works for JsonDiff
and JsonPatch::import
but not for apply
. So it's possible to create a patch with your lib that cannot be applied back. To me, either associative array should not be supported and raise an explicit error or the goal should be to support everywhere (even if it could potentially be ambiguous).
from json-diff.
Unfortunately using associative arrays for JSON data (including JSON patches) is not the greatest idea. Limitation against associative arrays is here intentionally.
The problem is that associative array does not provide enough precision to represent JSON objects and arrays:
[]
and{}
have identical associative array value,[0,1,2]
and{"0":0,"1":1,"2":2}
have identical associative array value.
Though it is possible to come up with some workarounds to allow associative array to mimic JSON objects, I don't feel that would be right thing to do. Misusing types will lead to unnecessary ambiguity with hardly justified benefits.
To resolve JSON type on encoding PHP itself loops through array keys to check if they form a sequence: https://github.com/php/php-src/blob/master/ext/json/json_encoder.c#L39.
In case you really feel the need to represent JSON objects with associative arrays I can only suggest to leverage PHP resolver by json_decode(json_encode($assocArray))
before feeding the value to JsonDiff
. That may impact performance.
Sorry for not being helpful.
from json-diff.
I have encountered this same issue working with documents from CouchDB. The PHP libraries that support posting and retrieving JSON documents tend to return the payloads as associative arrays. I guess the encode/decode workaround here will have to do for now...
from json-diff.
Related Issues (20)
- Implement merge diff/patch
- Warning: A non-numeric value encountered HOT 1
- 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
- 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.