{"record":{"id":"b449f1c1b95f3a41","repo":"XINCGer/Unity3DTraining","slug":"struct-fields-cannot-have-an-empty-key-or-a-null-v","errorCode":null,"errorMessage":"Struct fields cannot have an empty key or a null value.","messagePattern":"Struct fields cannot have an empty key or a null value\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs","lineNumber":576,"sourceCode":"            writer.Write(NameValueSeparator);\n            writer.Write('\"');\n            writer.Write(data.ToBase64());\n            writer.Write('\"');\n            writer.Write(\" }\");\n        }\n\n        private void WriteStruct(TextWriter writer, IMessage message)\n        {\n            writer.Write(\"{ \");\n            IDictionary fields = (IDictionary)message.Descriptor.Fields[Struct.FieldsFieldNumber].Accessor.GetValue(message);\n            bool first = true;\n            foreach (DictionaryEntry entry in fields)\n            {\n                string key = (string)entry.Key;\n                IMessage value = (IMessage)entry.Value;\n                if (string.IsNullOrEmpty(key) || value == null)\n                {\n                    throw new InvalidOperationException(\"Struct fields cannot have an empty key or a null value.\");\n                }\n\n                if (!first)\n                {\n                    writer.Write(PropertySeparator);\n                }\n                WriteString(writer, key);\n                writer.Write(NameValueSeparator);\n                WriteStructFieldValue(writer, value);\n                first = false;\n            }\n            writer.Write(first ? \"}\" : \" }\");\n        }\n\n        private void WriteStructFieldValue(TextWriter writer, IMessage message)\n        {\n            var specifiedField = message.Descriptor.Oneofs[0].Accessor.GetCaseFieldDescriptor(message);\n            if (specifiedField == null)","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs#L558-L594","documentation":"JsonFormatter.WriteStruct throws InvalidOperationException when a google.protobuf.Struct's fields map contains an empty string key or a null value entry. Struct is defined to have non-empty keys and non-null Value messages; the formatter enforces this invariant.","triggerScenarios":"Manually mutating the Struct's fields dictionary (Struct.Fields[key] = null, or adding \"\"); deserializing malformed data into a Struct and then formatting it; reflection-based code inserting null Values.","commonSituations":"Hand-constructing Struct values from user dictionaries that contain empty keys or nulls; bridges from JSON.NET/other serializers that preserve null entries; corrupted payloads.","solutions":["Remove null-valued and empty-key entries before formatting, or represent nulls as Value.ForNull()","Validate the source dictionary before converting it into a Struct","Use the generated Struct/Value APIs (Value.ForString etc.) instead of raw map mutation","Sanitize third-party JSON before packing into a Struct"],"exampleFix":"// before\nstructMsg.Fields[\"\"] = null;\n// after\nstructMsg.Fields[\"key\"] = Value.ForNull(); // non-empty key; use Value.ForNull for JSON null","handlingStrategy":"validation","validationCode":"bool structIsValid = structMsg.Fields.All(kv => !string.IsNullOrEmpty(kv.Key) && kv.Value != null);","typeGuard":"static bool IsValidStructEntry(KeyValuePair<string, Value> e) => !string.IsNullOrEmpty(e.Key) && e.Value != null;","tryCatchPattern":"try { json = formatter.Format(structMsg); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Struct fields\")) { /* sanitize struct and retry */ }","preventionTips":["Sanitize source dictionaries: drop null values and empty keys","Use Value.ForNull() to represent JSON nulls","Build Structs via generated APIs, not raw map mutation"],"tags":["csharp","protobuf","json","struct"],"backgroundTag":"empty-required-field","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"}