Skip to content

Instantly share code, notes, and snippets.

@tcr
Created October 23, 2011 07:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcr/1306986 to your computer and use it in GitHub Desktop.
Save tcr/1306986 to your computer and use it in GitHub Desktop.
How can you make the Github API accept Unicode characters in JSON?
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