{"record":{"id":"3a08ac9925520770","repo":"XINCGer/Unity3DTraining","slug":"invalid-first-character-of-token-next-value","errorCode":null,"errorMessage":"Invalid first character of token: {next.Value}","messagePattern":"Invalid first character of token: (.+?)","errorType":"exception","errorClass":"InvalidJsonException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonTokenizer.cs","lineNumber":280,"sourceCode":"                            ConsumeLiteral(\"false\");\n                            ValidateAndModifyStateForValue(\"Invalid state to read a false literal: \");\n                            return JsonToken.False;\n                        case '-': // Start of a number\n                        case '0':\n                        case '1':\n                        case '2':\n                        case '3':\n                        case '4':\n                        case '5':\n                        case '6':\n                        case '7':\n                        case '8':\n                        case '9':\n                            double number = ReadNumber(next.Value);\n                            ValidateAndModifyStateForValue(\"Invalid state to read a number token: \");\n                            return JsonToken.Value(number);\n                        default:\n                            throw new InvalidJsonException(\"Invalid first character of token: \" + next.Value);\n                    }\n                }\n            }\n\n            private void ValidateState(State validStates, string errorPrefix)\n            {\n                if ((validStates & state) == 0)\n                {\n                    throw reader.CreateException(errorPrefix + state);\n                }\n            }\n\n            /// <summary>\n            /// Reads a string token. It is assumed that the opening \" has already been read.\n            /// </summary>\n            private string ReadString()\n            {\n                var value = new StringBuilder();","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonTokenizer.cs#L262-L298","documentation":"The internal JsonTokenizer (lexer for JSON used by JsonParser) throws InvalidJsonException when it encounters a character that cannot begin any JSON token (value, string, number, object, array, or literal). Only '-', digits, 't'/'f'/'n', '\"', '{', '[' etc. are legal start characters; anything else (e.g. a stray quote, single-quote, or control character) is rejected here.","triggerScenarios":"Calling JsonParser.Parse<T>(string/json) or JsonFormatter-independent parsing where the input JSON contains a character outside the JSON grammar at a value position — e.g. single-quoted strings ('abc'), trailing commas followed by junk, an unquoted identifier (undefined), or a Unicode character like '\\u2019' at a token start.","commonSituations":"Pasting JSON from a log or email with smart quotes; JavaScript-style input with single quotes or unquoted keys; string encoding issues (BOM or curly quotes) from Windows tools; reading JSON from a file saved with the wrong encoding.","solutions":["Inspect the character at the reported position and replace it with valid JSON syntax (double quotes, plain ASCII digits/letters).","Sanitize input: replace curly/smart quotes with straight quotes and strip BOM before parsing.","Ensure keys and string values are double-quoted and identifiers like true/false/null are unquoted literals, not bare words with wrong casing.","If the input comes from another system, fix it at the source to emit standards-compliant JSON (test with a strict JSON linter)."],"exampleFix":"// before\nstring json = \"{'name': 'x'}\"; // single quotes -> InvalidJsonException\nvar msg = JsonParser.Default.Parse<Message>(json);\n// after\nstring json = \"{\\\"name\\\": \\\"x\\\"}\";\nvar msg = JsonParser.Default.Parse<Message>(json);","handlingStrategy":"try-catch","validationCode":"// Pre-validate JSON before parsing\nstatic bool IsLikelyValidJson(string s)\n{\n    s = s.TrimStart('\\uFEFF');\n    return s.Length > 0 && (s[0] == '{' || s[0] == '[' || s[0] == '\"');\n}\n// or use a strict parser first: System.Text.Json.JsonDocument.Parse(s)","typeGuard":null,"tryCatchPattern":"try\n{\n    var msg = JsonParser.Default.Parse<T>(json);\n}\ncatch (InvalidJsonException ex)\n{\n    logger.LogError(ex, \"Malformed JSON at token start\");\n    // report position / sanitize and re-validate\n}","preventionTips":["Strip BOM and normalize smart quotes to ASCII before parsing","Validate with a strict JSON parser (System.Text.Json) before handing to JsonParser","Only accept JSON from trusted, well-tested serializers"],"tags":["protobuf","json","lexer","csharp"],"backgroundTag":"json-parse-error","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}