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

undefined Map key is serialized to null #33

Open
nkbt opened this issue Mar 2, 2017 · 10 comments
Open

undefined Map key is serialized to null #33

nkbt opened this issue Mar 2, 2017 · 10 comments

Comments

@nkbt
Copy link

nkbt commented Mar 2, 2017

Codepen: http://codepen.io/nkbt/pen/xgebxL

This code

const {toJSON, fromJSON} = pkgzip.transitImmutableJs;
const {Map} = pkgzip.immutable;


document.querySelector('#app').innerHTML = `
  toJSON(Map().set(undefined, 'whatever')): ${toJSON(Map().set(undefined, 'whatever'))}
  toJSON(Map().set(null, 'whatever')):      ${toJSON(Map().set(null, 'whatever'))}
`;

Resulted in

  toJSON(Map().set(undefined, 'whatever')): ["~#iM",[null,"whatever"]]
  toJSON(Map().set(null, 'whatever')):      ["~#iM",[null,"whatever"]]
@nkbt
Copy link
Author

nkbt commented Mar 2, 2017

Not like I am trying to abuse this library (though probably it looks like I very much do...), but just spent another hour debugging one edge case, where we rely on key being undefined.

@glenjamin
Copy link
Owner

There is no undefined in JSON, so there's not much I can do about this unfortunately.

@nkbt
Copy link
Author

nkbt commented Mar 2, 2017

It is possible.

Something like:

function __UNDEFINED () {}
const value = typeof input === 'undefined' ? new __UNDEFINED() : value

But I feel like it may be done on transit-js level...

@glenjamin
Copy link
Owner

I'm not following that last post - the json spec doesn't include undefined, so short of another fancy encoding I'm not really sure what your options are.

Can you change the code so you don't rely on undefined and null needing to be distinct keys?

@nkbt
Copy link
Author

nkbt commented Mar 2, 2017

Sure, I've changed the code first thing, so this is definitely not a blocker.
And yes jdon does not support undefined. But I assumed that transit is meant to be used to overcome json limitation on transferring valid JS structures, so from that prospective this issue still looks valid to me

@glenjamin
Copy link
Owner

Ah, I see what you mean - yeah.

I think transit mostly expects to deal with classes and instances, but the default fallback handler might be a way to encode undefined.

@lukaswelinder
Copy link

@nkbt This might help you: https://github.com/deanlandolt/bytewise

@nkbt
Copy link
Author

nkbt commented Mar 3, 2017

@lukaswelinder that's really cool, thank you!

@lukaswelinder
Copy link

@nkbt Glad I could help!

Ran into this issue at work. We built out an Immutable.js data store w/ type and order persistence on top of LevelUP and Thread.js and needed a way to serialize Immutable structures for both the DB and to pass messages on Thread.js.

In short, our solution was to break down structures into keyPath (arrays) serialized using bytewise w/ our own semantic for identifying structure type.

The result takes up more memory than transit-immutable-js however, so you would probably want to only use it for undefined.

@nkbt
Copy link
Author

nkbt commented Mar 3, 2017

So far the easy solution was to switch to null instead. It did not make too much difference for the code around it.

But I see bytewise lib as potentially very helpful in some near future for our projects.

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

3 participants