Unity-Technologies/UnityCsReference · error · JSONParseException

End of json while parsing while parsing unicode char near {}

Error message

End of json while parsing while parsing unicode char near {}

What it means

For a \u escape, ParseString requires four hex digits; if endidx + 4 >= len (fewer than four characters remain after \u), it throws JSONParseException ("End of json while parsing while parsing unicode char near <pos>"). A separate FormatException-based message ("Invalid unicode escape char near") handles the case where four characters exist but are not hex. The doubled 'while parsing while parsing' wording is a typo in the original.

Source

Thrown at Editor/Mono/AssetStore/Json.cs:569

                        res += '\b';
                        break;
                    case 'f':
                        res += '\f';
                        break;
                    case 'n':
                        res += '\n';
                        break;
                    case 'r':
                        res += '\r';
                        break;
                    case 't':
                        res += '\t';
                        break;
                    case 'u':
                        // Unicode char specified by 4 hex digits
                        string digit = "";
                        if (endidx + 4 >= len)
                            throw new JSONParseException("End of json while parsing while parsing unicode char near " + PosMsg());
                        digit += json[endidx + 1];
                        digit += json[endidx + 2];
                        digit += json[endidx + 3];
                        digit += json[endidx + 4];
                        try
                        {
                            int d = System.Int32.Parse(digit, System.Globalization.NumberStyles.AllowHexSpecifier);
                            res += (char)d;
                        }
                        catch (FormatException)
                        {
                            throw new JSONParseException("Invalid unicode escape char near " + PosMsg());
                        }
                        endidx += 4;
                        break;
                    default:
                        throw new JSONParseException("Invalid escape char '" + ncur + "' near " + PosMsg());
                }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Provide the full four-hex-digit form for every \u escape (e.g. \u00E9).
  2. Verify input completeness before parsing.
  3. Catch JSONParseException and report the position so the truncated escape can be located.

Example fix

// before
// { "city": "S\u00E3" }   // truncated, only 2 hex digits

// after
// { "city": "S\u00E3o Paulo" }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every \u escape is followed by exactly four hex digits.
// Generated JSON should satisfy this; validate truncated input by full-body reads.

Try / catch

try { v = JSON.Parse(json); }
catch (JSONParseException ex) { /* position near the truncated \u sequence */ throw; }
catch (System.FormatException) { /* 'Invalid unicode escape char' for non-hex digits */ throw; }

Prevention

When it happens

Trigger: Truncated input ending inside a \u escape (e.g. \u00<EOF>); a hand-typed escape with fewer than four hex digits; encoding corruption cutting the sequence.

Common situations: Truncated network/file input; copy/paste that cut a unicode escape; manual authoring of \u sequences.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/2bfecc681a898f71. Report an issue: GitHub.