Skip to content

Library only works with stdClass not with associative arrays #17

Closed
@Tobion

Description

@Tobion

We we're looking into replacing our use of https://github.com/mikemccabe/json-patch-php with this library because it seemed to be better maintained. But we found a blocking problem.

It appears this library only works correctly with stdClass representations of decoded json data but not with associative arrays which is what you get when you do json_decode($json, true). It seems very counter-intuitive because it's often easier to work with arrays than with stdClass.

To illustrate the problem I modified the decoding in the example from the readme and it starts failing.

$diff = new JsonDiff(json_decode($originalJson, true), json_decode($newJson, true));
$this->assertEquals(json_decode($patchJson, true), $diff->getPatch()->jsonSerialize()); 
// this does not succeed because it returns the json diff with stdClass instead of array 
// which would work for jsonSerialize as well theoretically, see https://github.com/swaggest/json-diff/blob/master/src/JsonPatch.php#L108

$original = json_decode($originalJson, true);
$patch = JsonPatch::import(json_decode($patchJson, true)); 
// import seems to work also with array, see https://github.com/swaggest/json-diff/blob/dadb65fbc6888c9066c9eb167a98a721eb0f63a1/src/JsonPatch.php#L54
$patch->apply($original); 
// apply fails because the it tries to access fields as properties on $original as stdClass
// instead of as elements of an array

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions