Imagine I want the literal string "\u" stored in a JSON string.
This answer provides a nice visual overview of escape sequences in JSON, but it's not clear from its picture whether they are escaped at the same time or in order.
Consider the following JSON:
{
"foo": "a\\u"
}
This ought - to me at least - be valid JSON, wherein foo
contains the string a\u
.
However \u
is an escape sequence for Unicode characters and must - usually - be followed by four hexadecimal characters.
When I enter the same JSON through Json Parser Online, which I believe uses JSON.stringify
yields the following error: "\u must be followed by 4 hexadecimal characters".
I also tried typing the following into both Firefox's and Chrome's consoles:
var json = JSON.stringify(eval("(" + '{"foo": "a\\u"}' + ")"));
And both yield an Unicode escape sequence error. Even the library superobject for Delphi yields the same error (or actually, it just throws a JSON parse error).
Looking through the RFC 7159 on strings, it does mention these escape sequences, but no mention on order.
It seems to me that the JSON standard does not consider the following sequence \\u
, or at least, the parsers don't handle it well. If \\
is escaped first, \\u
becomes \u
and \u
will be wrong. If \u
is escaped first, then it yields an error before it even gets to \\
.
This is a particularly problem for rich text in JSON, because \uc1
and \uc2
are common control sequences in RTF. The answer in question doesn't really touch on the subject, but the question also seems pretty much abandoned.
So a theoretical question would be: How are (and how should) escape sequences be escaped?
And a more practical question would be: How do I get the string literal \u
into a JSON string beyond simply providing the Unicode sequence for either \
or u
(e.g. a\u005Cu
)?