JSON-Patch icon indicating copy to clipboard operation
JSON-Patch copied to clipboard

NaN values cause jsonpatch.observe confusion

Open mbeerta-factset opened this issue 6 years ago • 1 comments

When dealing with NaN values, generated patches always have a replace operation on nodes containing those.

Reproduction:

const jsonpatch = require('fast-json-patch');

class DocumentImpl {

  constructor () {
    this.data = [
      {
        "key" : "value",
        "member" : NaN
      }
    ]
  }
}

const document = new DocumentImpl();
var observer = jsonpatch.observe(document);

document.data.push({
      "key" : "value2",
      "member" : 12345
    });

const patch = jsonpatch.generate(observer);
console.log(patch);

which generates the following javascript object

[ { op: 'replace', path: '/data/0/member', value: NaN },
  { op: 'add',
    path: '/data/1',
    value: { key: 'value2', member: 12345 } } ]

the first op should not be there. Converting the object to a json string leads value to be null , which is probably also the root cause.

mbeerta-factset avatar Nov 19 '18 15:11 mbeerta-factset

Hi! Your diagnosis is on point. JSON-Patch acts like native JSON.parse/stringify where NaN is not supported. That's why NaN causes trouble for it. See

https://github.com/Starcounter-Jack/JSON-Patch#undefineds-js-to-json-projection

I don't think we plan to support Javascript objects. This is only to observse JSON documents. For the same reason we don't support functions and Date objects observation.

alshakero avatar Nov 19 '18 16:11 alshakero