Skip to content

Instantly share code, notes, and snippets.

@atupal
Forked from tcr/code.js
Created March 31, 2014 10:16
Show Gist options
  • Save atupal/9889309 to your computer and use it in GitHub Desktop.
Save atupal/9889309 to your computer and use it in GitHub Desktop.
function JSON_stringify(s, emit_unicode)
{
var json = JSON.stringify(s);
return emit_unicode ? json : json.replace(/[\u007f-\uffff]/g,
function(c) {
return '\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4);
}
);
}

Github doesn't seem to accept unescaped Unicode characters in JSON. If your JSON encoder doesn't escape unicode characters, you can do this manually using the \uXXXX syntax of JavaScript string literals.

Rationale: Since it's not defined as a part of the spec, characters are not required to be escaped by JSON.stringify, which is the only JSON encoder/decoder provided as a part of Node.js. Thus, a wrapper function is required to get this functionality.

["javascript","github","api","unicode"]
http://stackoverflow.com/questions/4901133/json-and-escaping-characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment