{"record":{"id":"1b950d0fb5354dac","repo":"XINCGer/Unity3DTraining","slug":"expected-object-value-for-any","errorCode":null,"errorMessage":"Expected object value for Any","messagePattern":"Expected object value for Any","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":491,"sourceCode":"            {\n                throw new InvalidProtocolBufferException(\"Expected object value for Struct\");\n            }\n            tokenizer.PushBack(token);\n\n            var field = message.Descriptor.Fields[Struct.FieldsFieldNumber];\n            MergeMapField(message, field, tokenizer);\n        }\n\n        private void MergeAny(IMessage message, JsonTokenizer tokenizer)\n        {\n            // Record the token stream until we see the @type property. At that point, we can take the value, consult\n            // the type registry for the relevant message, and replay the stream, omitting the @type property.\n            var tokens = new List<JsonToken>();\n\n            var token = tokenizer.Next();\n            if (token.Type != JsonToken.TokenType.StartObject)\n            {\n                throw new InvalidProtocolBufferException(\"Expected object value for Any\");\n            }\n            int typeUrlObjectDepth = tokenizer.ObjectDepth;\n\n            // The check for the property depth protects us from nested Any values which occur before the type URL\n            // for *this* Any.\n            while (token.Type != JsonToken.TokenType.Name ||\n                token.StringValue != JsonFormatter.AnyTypeUrlField ||\n                tokenizer.ObjectDepth != typeUrlObjectDepth)\n            {\n                tokens.Add(token);\n                token = tokenizer.Next();\n\n                if (tokenizer.ObjectDepth < typeUrlObjectDepth)\n                {\n                    throw new InvalidProtocolBufferException(\"Any message with no @type\");\n                }\n            }\n","sourceCodeStart":473,"sourceCodeEnd":509,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L473-L509","documentation":"An Any value's canonical JSON is an object containing at minimum an \"@type\" key (plus the packed inner message fields). MergeAny reads the first token and, if it is not StartObject, throws this InvalidProtocolBufferException before it can scan for @type.","triggerScenarios":"Calling JsonParser.Parse where a google.protobuf.Any field receives a JSON string (e.g. the bare type URL), a number, an array, or null instead of an object, e.g. {\"detail\": \"type.googleapis.com/foo.Bar\"}.","commonSituations":"APIs that serialize Any as just the type URL or the inner message without the object wrapper; clients that stringify the Any value; confusion between Any and StringValue/bytes fields.","solutions":["Emit Any as a JSON object: {\"@type\": \"type.googleapis.com/pkg.Msg\", ...inner fields}.","If you have a raw inner message JSON, wrap it: merge the type URL key into the inner object before parsing.","If a plain string was intended, change the schema field type from Any to string.","Catch InvalidProtocolBufferException and inspect the payload to confirm the Any field's JSON shape."],"exampleFix":"// before: Any as a bare string\n{\"detail\": \"type.googleapis.com/foo.Bar\"}\n\n// after\n{\"detail\": {\"@type\": \"type.googleapis.com/foo.Bar\", \"id\": 42}}","handlingStrategy":"validation","validationCode":"// Any fields must be objects containing @type\nfunction validateAnyField(payload, name) {\n  const v = payload[name];\n  if (v !== undefined && (typeof v !== 'object' || v === null || Array.isArray(v))) {\n    throw new Error(`${name} (google.protobuf.Any) must be a JSON object with @type`);\n  }\n}","typeGuard":"function isAnyLike(v) { return v !== null && typeof v === 'object' && !Array.isArray(v) && typeof v['@type'] === 'string'; }","tryCatchPattern":"try { var msg = JsonParser.Default.Parse<T>(json); }\ncatch (InvalidProtocolBufferException ex) when (ex.Message == \"Expected object value for Any\") { /* reject payload: Any must be an object */ }","preventionTips":["Always wrap Any values as {\"@type\": url, ...fields}","Never serialize Any as just the type URL string","Add ingress validation for Any-typed fields"],"tags":["protobuf","json-parsing","any","well-known-type"],"backgroundTag":"protobuf-unmarshal-failed","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"}