tui-cs/Terminal.Gui · error · JsonException

Unexpected end of JSON while reading Key array.

Error message

Unexpected end of JSON while reading Key array.

What it means

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.

Source

Thrown at Terminal.Gui/Configuration/KeyArrayJsonConverter.cs:44

        while (reader.Read ())
        {
            if (reader.TokenType == JsonTokenType.EndArray)
            {
                return keys.ToArray ();
            }

            if (reader.TokenType != JsonTokenType.String)
            {
                throw new JsonException ($"Expected string token in Key array, got {reader.TokenType}.");
            }

            string keyString = reader.GetString ()!;

            keys.Add (Key.TryParse (keyString, out Key key) ? key : Key.Empty);
        }

        throw new JsonException ("Unexpected end of JSON while reading Key array.");
    }

    /// <inheritdoc/>
    public override void Write (Utf8JsonWriter writer, Key []? value, JsonSerializerOptions options)
    {
        if (value is null)
        {
            writer.WriteNullValue ();

            return;
        }

        writer.WriteStartArray ();

        foreach (Key key in value)
        {
            writer.WriteStringValue (key.ToString ());
        }

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Add the missing closing ']' for the Key[] array.
  2. 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.
  3. Ensure config files are written atomically (write to temp, then rename) to avoid partial writes.
  4. If streaming, buffer the complete document before deserialization.

Example fix

// before (truncated)
"Accept": ["Ctrl+A", "Enter"

// after
"Accept": ["Ctrl+A", "Enter"]
Defensive patterns

Strategy: validation

Validate before calling

using System.Text.Json;

static bool JsonIsCompleteAndValid (string json)
{
    try
    {
        using JsonDocument doc = JsonDocument.Parse (json);
        return true;
    }
    catch (JsonException) { return false; }
}

if (!JsonIsCompleteAndValid (configJson))
{
    // The JSON is truncated/malformed; do not hand it to ConfigurationManager.
}

Type guard

static bool IsCompleteJson (string json)
{
    try { using JsonDocument.Parse (json); return true; } catch { return false; }
}

Try / catch

try
{
    ConfigurationManager.Load (configJson);
}
catch (JsonException ex) when (ex.Message.Contains ("Unexpected end of JSON while reading Key array"))
{
    // The Key[] array is missing its closing ']'. Repair the JSON and reload.
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/ebceaa90b6c1a0de. Report an issue: GitHub.