glenjamin / transit-immutable-js

Transit serialisation for Immutable.js
MIT License
250 stars 31 forks source link

Allow to pass object to fromJSON #27

Open gajus opened 8 years ago

gajus commented 8 years ago

Use case scenario:

Server renders:

<script>
window.__PRELOADED_STATE__ = ${transit.toJSON(preloadedState)};
</script>

Client picks up __PRELOADED_STATE__, which is a valid JSON, therefore it is converted to an object. To use it, I need to cast it back to string, e.g.

console.log('window.__PRELOADED_STATE__', transit.fromJSON(JSON.stringify(window.__PRELOADED_STATE__)));

Alternatively, I can stringify the object twice on server: once using transit-immutable-js and second time using JSON.stringify, e.g.

<script>
window.__PRELOADED_STATE__ = ${JSON.stringify(transit.toJSON(preloadedState))};
</script>

Preferably though,transit-immutable-js should be able to accept an object.

glenjamin commented 8 years ago

I think this could be considered a dupe of #25 - but this has a nice summary so i'm going to close that one instead.

I think it is possible to allow decoding of an object as mentioned in https://github.com/glenjamin/transit-immutable-js/issues/25#issuecomment-240679696 It's currently not possible to encode into an object due to https://github.com/cognitect/transit-js/issues/23

gajus commented 8 years ago

What about a hacky interim solution that would check if input is an object and cast it to a string using JSON.stringify?

This would abstract the issue at the transit-immutable-js API level.

There is no performance trade-off, because if you are not doing it – the user of the API will need to do it anyway.

glenjamin commented 8 years ago

I don't think it'd be that difficult to create a transit.decoder instance and accept an object without the additional stringify - this issue isn't currently affecting me so it's not high up my list of things to do, but I'd definitely take a PR (and help with landing it).

If it does cross my path requirement-wise in the near future I'll probably tackle it myself.

gajus commented 8 years ago

Sorry, I don't have time to make contributions to new OS projects. Have my hands full.

glenjamin commented 8 years ago

Sorry, I don't have time to make contributions to new OS projects. Have my hands full.

No worries, I know the feeling!