{"record":{"id":"9e99595965fe97b5","repo":"XINCGer/Unity3DTraining","slug":"unhandled-dictionary-key-type","errorCode":null,"errorMessage":"Unhandled dictionary key type: ","messagePattern":"Unhandled dictionary key type: ","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs","lineNumber":668,"sourceCode":"                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)\n        {\n            writer.Write('\"');","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs#L650-L686","documentation":"Google.Protobuf's JsonFormatter can only serialize dictionary keys that are strings, booleans, or integers (it converts them to a JSON string key). If a message contains a map field whose key type has no JSON representation (e.g. float, double, bytes, or message keys), WriteDictionary throws this ArgumentException. It also throws when a dictionary entry has a null key.","triggerScenarios":"Serializing (JsonFormatter.Format / WriteTo) a proto message containing a map field whose key type is not string/bool/int32/int64/uint32/uint64 — e.g. map<float, string> or map<bytes, X> — or a map instance built in code with a null key.","commonSituations":"Hand-written .proto files using unsupported key types (float/double/bytes keys are not allowed by proto spec but may appear via reflection or dynamic messages); runtime-constructed maps with null keys; switching key type of a map field during a schema migration.","solutions":["Change the map key type in the .proto file to a scalar string/bool/integer type and regenerate the C# code","If a float/bytes key is needed, restructure as a repeated message with key and value fields instead of a map","Guard against null keys before formatting; sanitize or remove entries with null keys","Use JsonFormatter with diagnostics or pre-convert unsupported keys to strings before serialization"],"exampleFix":"// before\nmessage Config {\n  map<float, string> thresholds = 1; // unsupported key type\n}\n// after\nmessage Config {\n  map<string, string> thresholds = 1; // keys as strings, e.g. \"0.5\"\n}","handlingStrategy":"validation","validationCode":"static bool IsSerializableKey(object k) => k != null && (k is string || k is bool || k is int || k is long || k is uint || k is ulong);","typeGuard":"static bool IsSerializableKey(object k) => k != null && (k is string || k is bool || k is int || k is long || k is uint || k is ulong);","tryCatchPattern":"try { formatter.Format(message); } catch (ArgumentException ex) when (ex.Message.StartsWith(\"Unhandled dictionary key type\")) { log.Error(\"Map field has unsupported key type\", ex); throw new SerializationFailureException(ex); }","preventionTips":["Only use string/bool/integer key types in proto map fields","Never put arbitrary bytes or floats in map keys","Validate runtime-built maps for null keys before formatting"],"tags":["protobuf","json-serialization","map-field","unsupported-key-type"],"backgroundTag":"unsupported-operation","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"}