{"record":{"id":"696a4cce414e2b7d","repo":"tui-cs/Terminal.Gui","slug":"duplicate-key-key-in-dictionary","errorCode":null,"errorMessage":"Duplicate key '{key}' in dictionary.","messagePattern":"Duplicate key '(.+?)' in dictionary\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Configuration/ConcurrentDictionaryJsonConverter.cs","lineNumber":41,"sourceCode":"                                                          options.PropertyNameCaseInsensitive\n                                                              ? StringComparer.InvariantCultureIgnoreCase\n                                                              : StringComparer.InvariantCulture);\n\n        while (reader.Read ())\n        {\n            if (reader.TokenType == JsonTokenType.StartObject)\n            {\n                reader.Read ();\n\n                if (reader.TokenType == JsonTokenType.PropertyName)\n                {\n                    string key = reader.GetString ();\n                    reader.Read ();\n                    object value = JsonSerializer.Deserialize (ref reader, typeof (T), TuiSerializerContext.Instance);\n\n                    if (!dictionary.TryAdd (key, (T)value))\n                    {\n                        throw new JsonException ($\"Duplicate key '{key}' in dictionary.\");\n                    }\n                }\n            }\n            else if (reader.TokenType == JsonTokenType.EndArray)\n            {\n                break;\n            }\n        }\n\n        return dictionary;\n    }\n\n    public override void Write (Utf8JsonWriter writer, ConcurrentDictionary<string, T> value, JsonSerializerOptions options)\n    {\n        writer.WriteStartArray ();\n\n        foreach (KeyValuePair<string, T> item in value)\n        {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Configuration/ConcurrentDictionaryJsonConverter.cs#L23-L59","documentation":"Thrown by ConcurrentDictionaryJsonConverter<T>.Read when dictionary.TryAdd(key, value) returns false, meaning two entries in the JSON array used the same key string. The comparer is invariant-culture (case-insensitive only if options.PropertyNameCaseInsensitive), so \"Default\" and \"default\" may collide depending on options.","triggerScenarios":"A Themes array contains two objects with the same key: [ {\"Default\": {...}}, {\"Default\": {...}} ].","commonSituations":"Merging two config files that both define the same theme; case-variant duplicates when case-insensitive matching is on; copy-paste of a theme block without renaming.","solutions":["Search the config for the duplicated key (case-insensitively) and remove or rename the extra entry.","If you intended to override, keep only one definition per key.","Check PropertyNameCaseInsensitive setting on your JsonSerializerOptions to know whether case variants count as duplicates."],"exampleFix":"// before\n\"Themes\": [ {\"Default\": {...}}, {\"Default\": {...}} ]\n// after\n\"Themes\": [ {\"Default\": {...}}, {\"Dark\": {...}} ]","handlingStrategy":"validation","validationCode":"// Detect duplicate theme keys before Load.\nusing var doc = JsonDocument.Parse (jsonString);\nvar seen = new HashSet<string> (StringComparer.OrdinalIgnoreCase);\nforeach (var obj in doc.RootElement.GetProperty (\"Themes\").EnumerateArray ())\n{\n    foreach (var p in obj.EnumerateObject ())\n        if (!seen.Add (p.Name))\n            throw new FormatException ($\"Duplicate theme key: {p.Name}\");\n}","typeGuard":null,"tryCatchPattern":"try { ConfigurationManager.Load (locations); }\ncatch (JsonException ex) when (ex.Message.Contains (\"Duplicate key\"))\n{ /* remove the duplicate entry */ }","preventionTips":["Dedupe theme keys when merging config files.","Be aware of the invariant-culture (and optional case-insensitive) comparer."],"tags":["configuration","json","theming","dictionary","duplicate-key"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}