What a backslash-u escape names in a string
A \u escape plus exactly four hexadecimal digits is the JSON, JavaScript, and Java spelling of one UTF-16 code unit. For characters in the Basic Multilingual Plane, that unit is the Unicode code point, from U+0000 through U+FFFF. ASCII characters, U+0000 through U+007F, are already safe in a source file, so this form leaves them as the characters themselves. A and a in the four digits are the same value.
A code point above U+FFFF does not fit in four hex digits. UTF-16 stores it as a surrogate pair: a high unit in D800-DBFF and a low unit in DC00-DFFF. The escape form is therefore two \u sequences in a row, not one. An emoji is the usual case. A lone surrogate is not a character. The pair has to be read together.
Those four digits are not UTF-8 bytes and not an HTML character reference. The ideograph U+4E2D is \u4e2d as a code point, and the three bytes E4 B8 AD as UTF-8. Byte dumps belong with hex encode and decode. A markup parser wants a form such as 中, which is HTML escape.
How four hex digits, surrogate pairs, and other escapes differ
Why \u takes four digits and leaves ASCII characters alone
- Each \u introduces one UTF-16 code unit. Four hex digits cover 0000 through FFFF, which is the whole Basic Multilingual Plane.
- Characters at or below U+007F are already ASCII. Writing them as \u0041 instead of A does not change the character, and the common encoder keeps the literal character.
- Fewer than four digits is not this escape. \u4e2 is a backslash, a u, and three digits, not the code point U+04E2. Extra digits stay as the following characters.
How a character above U+FFFF becomes two \u escapes
- Subtract 0x10000 from the code point. The high ten bits, plus 0xD800, are the first unit. The low ten bits, plus 0xDC00, are the second.
- The text then contains two escapes in order, high then low. Decoding recognizes that order and rebuilds one code point. Reversing the two units does not.
- A high unit without a following low unit, or a low unit alone, is an unpaired surrogate. It is not an emoji and not a letter. Well-formed text never contains one.
How this escape differs from UTF-8 hex and from HTML numeric references
- UTF-8 encodes the same code point as one to four bytes. Hex of those bytes is two digits per byte. \u is four digits for a code unit, not for a byte.
- U+4E2D is one BMP character. Its UTF-8 is E4 B8 AD. Treating E4B8 as a \u value, or 4E2D as three UTF-8 bytes, produces a different string.
- HTML writes a code point as 中 or 中. The x, the semicolon, and the allowed length are different from \u. A markup parser does not read \u, and a JSON parser does not read &#x.
Which other backslash letters are not a \u code unit
- JSON also uses \n, \t, \r, \\, and \" . Those name controls and punctuation, not a four-digit code unit. A decoder that only looks for \u leaves them unchanged.
- Python and several C-family languages use \U plus eight hex digits for a whole code point, including characters past U+FFFF. That is one escape, not a surrogate pair of two \u escapes.
- \x plus two hex digits usually names one byte, not a Unicode scalar. \u00e9 is the character é. The UTF-8 bytes of é are C3 A9, which a \x spelling would list as two bytes.
Why running the escape twice does not add another backslash
- After one escape, the text is ASCII: a backslash, the letter u, and four hex digits. Every one of those characters is at or below U+007F.
- An encoder that leaves ASCII literal therefore returns the same \u text. It does not turn the backslash into another escape.
- Seeing the six characters backslash, u, 4, e, 2, d in a file means the escape has not been decoded. Seeing the one character U+4E2D means it has. A second encode pass cannot tell those apart once the result is already ASCII.
Where a \u sequence is a code unit and not a byte
JSON that stays in ASCII while holding non-ASCII text
JSON strings may contain raw UTF-8 or \u escapes. Both are the same characters after a JSON parse. Escaping every non-ASCII unit keeps the file in ASCII, which some logs and older transports prefer. The escapes are still code units. A later step that treats the file as a hex dump of UTF-8 bytes will not match hex encode and decode.
An emoji stored as a surrogate pair in Java or JavaScript source
Source languages that predate easy non-BMP literals write an emoji as two \u escapes. The high unit is in D800-DBFF and the low unit is in DC00-DFFF. Dropping either half, or inserting a character between them, leaves an unpaired surrogate. The pair is one code point only when the two units stay adjacent and in order.
A code point copied from a chart into the wrong escape syntax
Charts print U+1F389 or the HTML form 🎉. The \u form of that same emoji is not \u1F389, because \u only accepts four digits. It is the surrogate pair. Pasting the chart number into \u, or into a UTF-8 hex dump, uses the right number in the wrong spelling. Markup uses the HTML numeric form from HTML escape.