kawanet/msgpack-lite

Is it possible to strip undefined properties on encoding?

Opened this issue · 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?

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

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}); // -> '{}'

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).

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

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

Pull request here.