{"record":{"id":"ebceaa90b6c1a0de","repo":"tui-cs/Terminal.Gui","slug":"unexpected-end-of-json-while-reading-key-array","errorCode":null,"errorMessage":"Unexpected end of JSON while reading Key array.","messagePattern":"Unexpected end of JSON while reading Key array\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Configuration/KeyArrayJsonConverter.cs","lineNumber":44,"sourceCode":"\n        while (reader.Read ())\n        {\n            if (reader.TokenType == JsonTokenType.EndArray)\n            {\n                return keys.ToArray ();\n            }\n\n            if (reader.TokenType != JsonTokenType.String)\n            {\n                throw new JsonException ($\"Expected string token in Key array, got {reader.TokenType}.\");\n            }\n\n            string keyString = reader.GetString ()!;\n\n            keys.Add (Key.TryParse (keyString, out Key key) ? key : Key.Empty);\n        }\n\n        throw new JsonException (\"Unexpected end of JSON while reading Key array.\");\n    }\n\n    /// <inheritdoc/>\n    public override void Write (Utf8JsonWriter writer, Key []? value, JsonSerializerOptions options)\n    {\n        if (value is null)\n        {\n            writer.WriteNullValue ();\n\n            return;\n        }\n\n        writer.WriteStartArray ();\n\n        foreach (Key key in value)\n        {\n            writer.WriteStringValue (key.ToString ());\n        }","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Configuration/KeyArrayJsonConverter.cs#L26-L62","documentation":"Thrown by KeyArrayJsonConverter.Read (KeyArrayJsonConverter.cs:44) when reader.Read() returns false (end of stream) before an EndArray token is encountered. The JSON is truncated or missing the closing bracket of a Key[] array.","triggerScenarios":"The config JSON stream ends mid-array: missing ']' after the last key string, or the file/stream was cut off. reader.Read() returns false inside the while loop, exiting without hitting EndArray.","commonSituations":"A truncated config file (incomplete write, copy-paste missing closing bracket). Streaming deserialization over a network/socket where the buffer was flushed early. A templating system that dropped trailing content.","solutions":["Add the missing closing ']' for the Key[] array.","Validate the JSON parses with a standard JSON parser (System.Text.Json.JsonDocument.Parse) before handing it to ConfigurationManager, to catch truncation early with a clearer error.","Ensure config files are written atomically (write to temp, then rename) to avoid partial writes.","If streaming, buffer the complete document before deserialization."],"exampleFix":"// before (truncated)\n\"Accept\": [\"Ctrl+A\", \"Enter\"\n\n// after\n\"Accept\": [\"Ctrl+A\", \"Enter\"]","handlingStrategy":"validation","validationCode":"using System.Text.Json;\n\nstatic bool JsonIsCompleteAndValid (string json)\n{\n    try\n    {\n        using JsonDocument doc = JsonDocument.Parse (json);\n        return true;\n    }\n    catch (JsonException) { return false; }\n}\n\nif (!JsonIsCompleteAndValid (configJson))\n{\n    // The JSON is truncated/malformed; do not hand it to ConfigurationManager.\n}","typeGuard":"static bool IsCompleteJson (string json)\n{\n    try { using JsonDocument.Parse (json); return true; } catch { return false; }\n}","tryCatchPattern":"try\n{\n    ConfigurationManager.Load (configJson);\n}\ncatch (JsonException ex) when (ex.Message.Contains (\"Unexpected end of JSON while reading Key array\"))\n{\n    // The Key[] array is missing its closing ']'. Repair the JSON and reload.\n}","preventionTips":["Always close Key[] arrays with ']'.","Pre-validate config JSON with JsonDocument.Parse before loading to get a clearer truncation error.","Write config files atomically (temp + rename) to avoid partial writes.","Buffer streamed JSON completely before deserialization."],"tags":["json","configuration","keybindings","malformed-json","serialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}