Giter Site home page Giter Site logo

Comments (4)

vearutop avatar vearutop commented on June 17, 2024 2

@Tobion @j13k please check v3.6.0, it now provides support for associative arrays.

from json-diff.

Tobion avatar Tobion commented on June 17, 2024 1

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.

vearutop avatar vearutop commented on June 17, 2024

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.

j13k avatar j13k commented on June 17, 2024

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)

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.