algesten / jsondiff

Structural JSON diff/patch tool.
Other
66 stars 27 forks source link

Doesn't diff changes in array of objects correctly #1

Closed deverton closed 12 years ago

deverton commented 12 years ago

These two tests demonstrate an issue with objects changing in arrays. Deletions aren't detected correctly and the generated diff doesn't actually update the JSON object properly. This doesn't appear to be specific to my port to Jackson but I haven't fully ruled that out yet.

    @Test
    public void testArrayObjectsRotateLeft() {
        String from = "{\"a\":[{\"b\":1},{\"c\":2},{\"d\":3}]}";
        String to = "{\"a\":[{\"c\":2},{\"d\":3},{\"e\":4}]}";
        String diff = "{\"~a[0]\":{\"-b\":0},\"-a[1]\":0,\"a[+2]\":{\"e\":4}}";

        String d = JsonDiff.diff(from, to);
        Assert.assertEquals(diff, d);

        String p = JsonPatch.apply(from, diff);
        Assert.assertEquals(to, p);

    }

    @Test
    public void testArrayObjectsRotateRight() {

        String from = "{\"a\":[{\"c\":2},{\"d\":3},{\"e\":4}]}";
        String to = "{\"a\":[{\"b\":1},{\"c\":2},{\"d\":3}]}";
        String diff = "{\"~a[0]\":{\"b\":1},\"a[+1]\":{\"c\":2},\"-a[2]\":0}";

        String d = JsonDiff.diff(from, to);
        Assert.assertEquals(diff, d);

        String p = JsonPatch.apply(from, diff);
        Assert.assertEquals(to, p);
    }
algesten commented 12 years ago

Hey! Thanks for the test case. I'll look into this today.