node-jsonpointer icon indicating copy to clipboard operation
node-jsonpointer copied to clipboard

Setting value to undefined removes property

Open danrot opened this issue 6 years ago • 5 comments

I am setting a value of undefined on an object using your library, and this removes the property completely. I would like to keep the property and really set its property to undefined.

I also put an example here:

const a = {value: 5};
jsonpointer.set(a, 'value', undefined);
console.log(a); // outputs something like {}, but I would like to have {value: undefined}

danrot avatar May 08 '19 07:05 danrot

Looks like it is intentional: https://github.com/janl/node-jsonpointer/blob/2a9875e14983712fbde05b4710fcdc53d4d6b575/jsonpointer.js#L42

jperasmus avatar May 30 '19 08:05 jperasmus

yeah, this is by design https://github.com/janl/node-jsonpointer/pull/27

I’d accept a PR that implements set(obj, pointer, value, {allowUndefinedValue: true}). Bonus points for a way to instantiate a set fun that has that option bound to it for each invocation.

Please file a PR directly.

janl avatar Jul 03 '20 18:07 janl

What about exposing a new setup or settings function that can be called to globally apply this and possible future config options?

jsonpointer.settings({ allowUndefinedValue: true })

Is this something you would be interested in?

jperasmus avatar Jul 04 '20 11:07 jperasmus

sure, all of this sounds good

janl avatar Nov 01 '21 08:11 janl

One thing to note is there is NOT undefined in JSON, while undefined is a valid value in JavaScript object. So for [1, undefined, 2] in JavaScript, when it's converted to JSON, it becomes [1, null, 2], which may have undesired effect.

johnmiroki avatar Dec 31 '21 05:12 johnmiroki