{"record":{"id":"87db9f1f9642ede1","repo":"XINCGer/Unity3DTraining","slug":"dictionary-has-entry-with-null-key","errorCode":null,"errorMessage":"Dictionary has entry with null key","messagePattern":"Dictionary has entry with null key","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs","lineNumber":666,"sourceCode":"                }\n                string keyText;\n                if (pair.Key is string)\n                {\n                    keyText = (string)pair.Key;\n                }\n                else if (pair.Key is bool)\n                {\n                    keyText = (bool)pair.Key ? \"true\" : \"false\";\n                }\n                else if (pair.Key is int || pair.Key is uint | pair.Key is long || pair.Key is ulong)\n                {\n                    keyText = ((IFormattable)pair.Key).ToString(\"d\", CultureInfo.InvariantCulture);\n                }\n                else\n                {\n                    if (pair.Key == null)\n                    {\n                        throw new ArgumentException(\"Dictionary has entry with null key\");\n                    }\n                    throw new ArgumentException(\"Unhandled dictionary key type: \" + pair.Key.GetType());\n                }\n                WriteString(writer, keyText);\n                writer.Write(NameValueSeparator);\n                WriteValue(writer, pair.Value);\n                first = false;\n            }\n            writer.Write(first ? \"}\" : \" }\");\n        }\n\n        /// <summary>\n        /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required.\n        /// </summary>\n        /// <remarks>\n        /// Other than surrogate pair handling, this code is mostly taken from src/google/protobuf/util/internal/json_escaping.cc.\n        /// </remarks>\n        internal static void WriteString(TextWriter writer, string text)","sourceCodeStart":648,"sourceCodeEnd":684,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs#L648-L684","documentation":"JsonFormatter.WriteDictionary throws ArgumentException when formatting a map field whose dictionary contains a null key. Protobuf map keys are scalars and can never be null; the formatter refuses to serialize such an entry.","triggerScenarios":"A C# Dictionary used as a protobuf map field has a null key inserted (possible for string-keyed maps) and the message is then formatted to JSON.","commonSituations":"Building map fields from user-supplied or external dictionaries that permit null keys; deserializing exotic formats into maps; LINQ projections producing null keys.","solutions":["Remove or reject null keys before adding entries to the map field","Validate the source dictionary: any k => k == null entries must be handled","Normalize null keys to a sentinel value only if legal for your schema","Add a guard before JsonFormatter.Format when maps originate from external data"],"exampleFix":"// before\nmapMsg.Fields[keyFromUser] = v; // keyFromUser may be null\n// after\nif (keyFromUser != null) { mapMsg.Fields[keyFromUser] = v; }","handlingStrategy":"validation","validationCode":"if (sourceDict.Keys.Any(k => k == null)) { throw new ArgumentException(\"Source dictionary contains null keys\"); }","typeGuard":"static bool HasNullKey<TKey,TValue>(Dictionary<TKey,TValue> d) where TKey : class => d.Keys.Any(k => k == null);","tryCatchPattern":"try { json = formatter.Format(msg); } catch (ArgumentException ex) when (ex.Message.Contains(\"null key\")) { /* drop/fix null-key entries and retry */ }","preventionTips":["Validate external dictionaries before filling map fields","Never insert user-supplied keys without a null check","Prefer TryAdd after key sanitization"],"tags":["csharp","protobuf","json","maps"],"backgroundTag":"null-argument","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"}