Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to strip undefined properties on encoding? #71

Open
irakliy81 opened this issue May 22, 2017 · 5 comments
Open

Is it possible to strip undefined properties on encoding? #71

irakliy81 opened this issue May 22, 2017 · 5 comments

Comments

@irakliy81
Copy link

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?

@scmorse
Copy link

scmorse commented Oct 6, 2017

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

@kawanet
Copy link
Owner

kawanet commented Oct 7, 2017

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

@irakliy81
Copy link
Author

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

@simonjeesam
Copy link

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

@simonjeesam
Copy link

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

Pull request here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants