tui-cs/Terminal.Gui · error · JsonException

{propertyName}: Expected an array of modifiers, but got "{re

Error message

{propertyName}: Expected an array of modifiers, but got "{reader.TokenType}".

What it means

Thrown by KeyCodeJsonConverter.Read (KeyCodeJsonConverter.cs:100-105) when the "Modifiers" property value is not a JSON StartArray token. Modifiers must be an array of strings (["Ctrl", "Shift"]); providing a single string "Ctrl" or an object triggers this.

Source

Thrown at Terminal.Gui/Configuration/KeyCodeJsonConverter.cs:102

                                    {
                                        break;
                                    }

                                    string mod = reader.GetString ();

                                    try
                                    {
                                        modifiers.Add (modifierDict [mod]);
                                    }
                                    catch (KeyNotFoundException e)
                                    {
                                        throw new JsonException ($"{propertyName}: \"{mod}\" is not a valid modifier.", e);
                                    }
                                }
                            }
                            else
                            {
                                throw new JsonException (
                                                         $"{propertyName}: Expected an array of modifiers, but got \"{reader.TokenType}\"."
                                                        );
                            }

                            break;

                        default:
                            throw new JsonException ($"{propertyName}: Unexpected Key property.");
                    }
                }
            }

            foreach (KeyCode modifier in modifiers)
            {
                key |= modifier;
            }

            return key;

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Always write Modifiers as a JSON array, even for a single modifier: ["Ctrl"].
  2. For no modifiers, use an empty array [] or omit the Modifiers property.
  3. Validate the Modifiers property is an array type before serializing config.

Example fix

// before
{ "Key": "A", "Modifiers": "Ctrl" }

// after
{ "Key": "A", "Modifiers": ["Ctrl"] }
Defensive patterns

Strategy: validation

Validate before calling

using System.Text.Json;

static bool ModifiersIsArray (string json, string propertyName)
{
    using JsonDocument doc = JsonDocument.Parse (json);
    // locate the property; illustrative
    return true;
}

// Ensure "Modifiers" is a JSON array in the raw config.

Type guard

static bool IsModifiersArray (JsonElement el) => el.ValueKind == JsonValueKind.Array;

Try / catch

try
{
    ConfigurationManager.Load (configJson);
}
catch (JsonException ex) when (ex.Message.Contains ("Expected an array of modifiers"))
{
    // "Modifiers" was a scalar/object. Rewrite it as ["..."] and reload.
}

Prevention

When it happens

Trigger: Config JSON has "Modifiers": "Ctrl" (scalar string) or "Modifiers": { ... } instead of "Modifiers": ["Ctrl"].

Common situations: User writes a single modifier as a bare string for brevity. Tooling collapses single-element arrays to scalars. Copy-paste from a format that uses comma-separated strings.

Related errors


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