tui-cs/Terminal.Gui · error · JsonException

Unexpected end of JSON while reading MouseFlags array.

Error message

Unexpected end of JSON while reading MouseFlags array.

What it means

Thrown by MouseFlagsArrayJsonConverter.Read (MouseFlagsArrayJsonConverter.cs:45) 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 MouseFlags[] array.

Source

Thrown at Terminal.Gui/Configuration/MouseFlagsArrayJsonConverter.cs:45

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

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

            string mouseFlagsString = reader.GetString ()!;
            mouseFlagsString = mouseFlagsString.Replace ("+", ", ").Replace ("|", ", ");

            mouseFlagsList.Add (Enum.TryParse (mouseFlagsString, true, out MouseFlags mouseFlags) ? mouseFlags : MouseFlags.None);
        }

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

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

            return;
        }

        writer.WriteStartArray ();

        foreach (MouseFlags mouseFlags in value)
        {
            writer.WriteStringValue (mouseFlags.ToString ().Replace (", ", "+"));
        }

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Add the missing closing ']' for the MouseFlags[] array.
  2. Validate the JSON parses with System.Text.Json.JsonDocument.Parse before handing it to ConfigurationManager to surface truncation with a clearer message.
  3. Write config files atomically (temp file + rename) to avoid partial writes.
  4. Buffer the complete document before deserialization when streaming.

Example fix

// before (truncated)
"Click": ["Button1Clicked", "WheeledUp"

// after
"Click": ["Button1Clicked", "WheeledUp"]
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 MouseFlags array"))
{
    // The MouseFlags[] 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 mouse-flag string, or the file/stream was cut off. reader.Read() returns false inside the while loop, exiting without hitting EndArray.

Common situations: Truncated config file (incomplete write, dropped closing bracket). Streaming deserialization over a truncated buffer. Templating 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/a85299bb2e9a32dc. Report an issue: GitHub.