{"record":{"id":"a9e78acb93deae24","repo":"XINCGer/Unity3DTraining","slug":"unexpected-token-type","errorCode":null,"errorMessage":"Unexpected token type ","messagePattern":"Unexpected token type ","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":185,"sourceCode":"            {\n                throw new InvalidProtocolBufferException(\"Expected an object\");\n            }\n            var descriptor = message.Descriptor;\n            var jsonFieldMap = descriptor.Fields.ByJsonName();\n            // All the oneof fields we've already accounted for - we can only see each of them once.\n            // The set is created lazily to avoid the overhead of creating a set for every message\n            // we parsed, when oneofs are relatively rare.\n            HashSet<OneofDescriptor> seenOneofs = null;\n            while (true)\n            {\n                token = tokenizer.Next();\n                if (token.Type == JsonToken.TokenType.EndObject)\n                {\n                    return;\n                }\n                if (token.Type != JsonToken.TokenType.Name)\n                {\n                    throw new InvalidOperationException(\"Unexpected token type \" + token.Type);\n                }\n                string name = token.StringValue;\n                FieldDescriptor field;\n                if (jsonFieldMap.TryGetValue(name, out field))\n                {\n                    if (field.ContainingOneof != null)\n                    {\n                        if (seenOneofs == null)\n                        {\n                            seenOneofs = new HashSet<OneofDescriptor>();\n                        }\n                        if (!seenOneofs.Add(field.ContainingOneof))\n                        {\n                            throw new InvalidProtocolBufferException(\"Multiple values specified for oneof \" + field.ContainingOneof.Name);\n                        }\n                    }\n                    MergeField(message, field, tokenizer);\n                }","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L167-L203","documentation":"Inside a message object, JsonParser.Merge expects alternating name/value tokens; after a value, the next token must be a field name or end-object. Any other token type indicates malformed JSON structure, so it throws InvalidOperationException (a parser invariant violation rather than a data error).","triggerScenarios":"Feeding structurally invalid JSON to JsonParser where an object member name is missing — e.g. two consecutive values without a key, an unterminated object, or a tokenizer/reader mismatch such as reusing a tokenizer across messages.","commonSituations":"Hand-built or template-generated JSON with missing keys; string concatenation producing malformed JSON; parser bugs or misuse of PushBack/tokenizer API in custom code paths.","solutions":["Validate the JSON with a standard parser (JsonDocument.Parse / JsonConvert) before feeding it to JsonParser to get a clearer error","Fix the JSON producer so every object value is preceded by a member name","Ensure one tokenizer per Merge call; do not interleave Next/PushBack incorrectly","If parsing untrusted input, catch InvalidOperationException/InvalidProtocolBufferException and return a 4xx-style parse failure"],"exampleFix":"// before\nstring json = \"{ 'a': 1, 2 }\"; // missing member name\n// after\nstring json = \"{ 'a': 1, 'b': 2 }\";","handlingStrategy":"try-catch","validationCode":"// Pre-validate JSON structure with the platform parser\nusing var doc = System.Text.Json.JsonDocument.Parse(json); // throws clearer errors on malformed JSON","typeGuard":null,"tryCatchPattern":"try { return parser.Parse<T>(json); } catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Unexpected token type\")) { log.Error(\"Malformed JSON structure fed to protobuf parser\", ex); throw new BadRequestException(\"Malformed JSON\", ex); }","preventionTips":["Validate JSON with a general-purpose parser before JsonParser","Never hand-build JSON via string concatenation; use a serializer","Use one tokenizer per Merge and do not misuse PushBack"],"tags":["protobuf","json-parsing","malformed-json","invalid-state"],"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"}