sonnyp / JSON8

JSON toolkit for JavaScript.
ISC License
105 stars 13 forks source link

Objects with arrays that change create wordy patches #58

Open bozzaj opened 6 years ago

bozzaj commented 6 years ago

I noticed that json8-patch seems to be quite fast, with the exception of arrays. Consider the following:

var obj = {
    "x": "SomeVal",
    "y": [{ "z": 2}, {"z": 3}, {"z": 4}, {"z": 5}]
}

var obj2 = {
    "x": "SomeVal",
    "y": [{ "z": 2}, {"z": 6}, {"z": 4}, {"z": 5}]
}

I would expect this to return a patch of:

[
  {
    "op": "replace",
    "path": "/y/1/z",
    "value": 6
  }
]

Instead, it returns:

[
  {
    "op": "replace",
    "path": "/y",
    "value": [
      {
        "z": 2
      },
      {
        "z": 6
      },
      {
        "z": 4
      },
      {
        "z": 5
      }
    ]
  }
]

Are they any plans for supporting objects within arrays without replacing the entire array?

smeijer commented 5 years ago

Same for replacing an object:

original

{
    "x": "SomeVal",
    "y": { 
      "a": 2,
      "b": 3,
      "c": 4
    }
}

edited

{
    "x": "SomeVal",
    "y": {}
}

patch

[
  {
    "op": "remove",
    "path": "/y/a"
  },
  {
    "op": "remove",
    "path": "/y/b"
  },
  {
    "op": "remove",
    "path": "/y/c"
  }
]

expected

[
  {
    "op": "replace",
    "path": "/y",
    "value": {}
  }
]
sonnyp commented 5 years ago

What would be the expected behavior? Somehow determinate which option would produce the smallest patch?