Serializing object that has line breaks or other control characters in it
See original GitHub issueNot 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.
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (5 by maintainers)
Top Results From Across the Web
Serialization of Control characters - Stack Overflow
In my program I wish to save a list of objects the user might have. Each object will have a particular value, much...
Read more >How to customize character encoding with System.Text.Json
To serialize all language sets without escaping, use UnicodeRanges.All. Serialize specific characters. An alternative is to specify individual ...
Read more >Rust: Beware of Escape Sequences \n | by Dominic E. - Medium
The escape sequence for a newline is \n . A line break is a control sequence that tells the rendering device, for example,...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
A string or number that's used to insert white space (including indentation, line break characters, etc.) into the output JSON string for ...
Read more >YAML Ain't Markup Language (YAML) (tm) 1.0 - YAML.org
In YAML, single line breaks may be folded into a single space, ... all non-printable Unicode characters if a character table is readily ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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/