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

Allow to pass object to fromJSON #27

Open
gajus opened this issue Sep 5, 2016 · 5 comments
Open

Allow to pass object to fromJSON #27

gajus opened this issue Sep 5, 2016 · 5 comments

Comments

@gajus
Copy link

gajus commented Sep 5, 2016

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
Copy link
Owner

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 #25 (comment) It's currently not possible to encode into an object due to cognitect/transit-js#23

@gajus
Copy link
Author

gajus commented Sep 5, 2016

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
Copy link
Owner

glenjamin commented Sep 5, 2016

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
Copy link
Author

gajus commented Sep 5, 2016

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

@glenjamin
Copy link
Owner

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

No worries, I know the feeling!

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

2 participants