msgpack-lite icon indicating copy to clipboard operation
msgpack-lite copied to clipboard

Is it possible to strip undefined properties on encoding?

Open irakliy81 opened this issue 8 years ago • 5 comments

In the docs it says that both null and undefined will come through as null on the other end. Is it possible to configure something to make undefined properties to be removed completely during encoding?

irakliy81 avatar May 22 '17 01:05 irakliy81

IMO it is a bug to have undefined encoded as null. undefined should not be defined in the encoding. Just like with JSON:

> JSON.parse(JSON.stringify({a: undefined}))
{}
> JSON.parse(JSON.stringify({a: undefined})).a
undefined

Yet with this lib:

> msgpack.decode(msgpack.encode({a: undefined}))
{ a: null }
> msgpack.decode(msgpack.encode({a: undefined})).a
null

scmorse avatar Oct 06 '17 13:10 scmorse

Interesting. The library could have an option strip_undef, ignore_undef or something.

The msgpack has null but no undefined vlaue. The library encodes JS's undefined as msgpack's null to follow the same behavior of other JS msgpack libraries.

https://www.npmjs.com/package/msgpack-js https://www.npmjs.com/package/msgpack

JSON.stringify() removes Object property when its value isundefined but translate Array value to null by the way.

JSON.stringify([undefined]); // -> '[null]'
JSON.stringify({a: undefined}); // -> '{}'

kawanet avatar Oct 07 '17 00:10 kawanet

It would be awesome if there was such an option. I think under such option undefined should be treated exactly like it is treated by JSON.stringify (stripped from objects, and converted to null in arrays).

It would also be very helpful if this option could be set via environment variable (e.g. MSGPACK_LITE_STRIP_UNDEF) - this way, it would be possible to enforce this behavior even for higher level libraries (socket.io in my case).

irakliy81 avatar Nov 01 '17 18:11 irakliy81

I'm also keen to have this option. Is there any progress on it?

simonjeesam avatar Oct 16 '18 04:10 simonjeesam

I went ahead and tried a fix myself based on the work done by @brentd here.

Pull request here.

simonjeesam avatar Oct 16 '18 06:10 simonjeesam