json icon indicating copy to clipboard operation
json copied to clipboard

Generates incorrect JSON string for U+2028/U+2029

Open matthewbjones opened this issue 11 years ago • 2 comments

When the Ruby string contains a Unicode Line Separator character (http://www.fileformat.info/info/unicode/char/2028/index.htm), this library incorrectly generates the final JSON, which causes SyntaxError: Unexpected token ILLEGAL (in Chrome) and Unexpected EOF in Safari, etc.

"Home by Edward Sharpe & the Magnetic Zeros\u2028".to_json
# "\"Home by Edward Sharpe \\u0026 the Magnetic Zeros\u2028\""

should be

# "\"Home by Edward Sharpe \\u0026 the Magnetic Zeros\\u2028\""

As far as I can tell, the JSON spec and browsers treat U+2028/2029 as valid line separator characters, which are not allowed to occur within strings.

matthewbjones avatar Aug 26 '14 18:08 matthewbjones

They aren’t allowed to occur in JavaScript strings, but they are perfectly valid in JSON strings.

kyrias avatar Mar 20 '15 18:03 kyrias

IIUC, the JSON generated is correct, but the resulting is not Javascript code, contrary to what many people would expect. It is indeed preferable to escape those characters to produce equivalent JSON that is also valid Javascript code.

See also https://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character Same issue in golang took 10 days to resolve... https://github.com/golang/go/issues/5836

marcandre avatar Jun 25 '20 23:06 marcandre