Text containing unicode characters are encoded as \x(hex) with json_encode(). This is invalid JSON and won't be parsed by JS.
Code Snippet (if applicable) to Reproduce Problem:
/client/proc/sendUni()
var/list/test = list("foo" = "Toupé")
src << output(json_encode(test), "someWindow:someFunction")
And in the browser...
function someFunction(test) {
try {
test = JSON.parse(test);
} catch (e) {
alert(e);
}
}
Expected Results:
Success parsing the JSON, clearly
Actual Results:
JS throws a JSON parse exception of "Error: JSON parse error for: (json)".
The JSON received by the JS function in the example above is: "{"foo": "Toup\xe9"}".
Workarounds:
Sanitize the JSON before parsing:
test = test.replace(/\\x[\dA-F]{2}/ig, function(match) {
return String.fromCharCode(parseInt(match.replace(/\\x/g, ''), 16));
});