transit-immutable-js: Serializing object that has line breaks or other control characters in it
Not sure if this is the right place to ask this. I’m building a universal React app and I’m using this module to serialize my immutable Redux store, which is then printed as a javascript variable and picked up by my client code:
Server:
const serializedStore = transit.toJSON(store.getState());
// later:
window.INITIAL_STATE = serializedStore;
Client:
const initialState = window.INITIAL_STATE ? transit.fromJSON( window.INITIAL_STATE ) : new Map();
However, if there are any line breaks or other control characters the browser will complain about “unexpected token in JSON”. Do I also have to escape the string before printing it to the browser? All examples I’ve found of using this library doesn’t do this but also doesn’t seem to take into account the possibility of line breaks etc. in the JSON content.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (5 by maintainers)
Unfortunately this does not help
I solved the issue by adding an extra set of slashes:
Picked it up from here: https://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/