Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created January 30, 2012 21:09
Show Gist options
  • Save WebReflection/1706722 to your computer and use it in GitHub Desktop.
Save WebReflection/1706722 to your computer and use it in GitHub Desktop.
Fix incorrect behavior of \u2028 and \u2029 characters in JSON.stringify
// @see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
// fix it via RegExp and feature detection
if (JSON.stringify("\u2028") == '"\u2028"') {
JSON.stringify = function (stringify) {
function place(m) {
return "\\u202" + (m == "\u2028" ? "8" : "9");
}
var re = /\u2028|\u2029/g;
return function fixed(data) {
return stringify(data).replace(re, place);
};
}(JSON.stringify);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment