Shouldn't JSON.stringify escape Unicode characters?
The JSON spec does not demand the conversion from unicode characters to escape-sequences. "Any UNICODE character except " or \ or control character." is defined to be a valid JSON-serialized string:
The short answer for your question is NO; JSON.stringify
shouldn't escape your string.
Although, handling utf8 strings can seem strange if you save your HTML file with utf-8
encoding but don't declare it to be an utf8 file.
For example:
<!doctype html>
<html>
<head>
<title></title>
<script>
var data="árvíztűrő tükörfúrógép ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP";
alert(JSON.stringify(data));
</script>
</head>
</html>
This would alert "árvÃztűrÅ‘ tükörfúrógép ÃRVÃZTÅ°RÅ TÃœKÖRFÚRÓGÉP"
.
But if you add the following line to the header:
<meta charset="UTF-8">
Then, the alert will be what one could expect: "árvíztűrő tükörfúrógép ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"
.
No. The preferred encoding for JSON is UTF-8, so those characters do not need to be escaped.
You are allowed to escape unicode characters if you want to be safer or explicitly send the JSON in a different encoding (that is, pure ASCII), but it is against recommendations.