question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing object that has line breaks or other control characters in it

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nkbtcommented, Aug 19, 2016

Unfortunately this does not help

    dangerouslySetInnerHTML={{__html: `window.__data='${JSON.stringify(toJSON(data))}';`}}

20160819-172701

1reaction
sebastiancarlssoncommented, Aug 17, 2016

I solved the issue by adding an extra set of slashes:

let serializedStore = transit.toJSON(store.getState());
serializedStore = serializedStore
    .replace(/\\/g, '\\\\')
    .replace(/\'/g, '\\\'')
    .replace(/\"/g, '\\"')
    .replace(/\0/g, '\\0')

Picked it up from here: https://magp.ie/2011/01/20/addslashes-and-stripslashes-in-javascript/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found