tui-cs/Terminal.Gui · error · JsonException

{propertyName}: Expected a string value.

Error message

{propertyName}: Expected a string value.

What it means

Thrown by AttributeJsonConverter.Read when the "style" property of an Attribute object is present but its JSON value is not a string token. TextStyle is parsed case-insensitively from a string, so a number, boolean, object, or array value is rejected here. The message names the offending propertyName.

Source

Thrown at Terminal.Gui/Configuration/AttributeJsonConverter.cs:84

                                  ? $"\"{reader.GetString ()}\""
                                  : $"<{reader.TokenType}>";

            try
            {
                switch (propertyName?.ToLower ())
                {
                    case "foreground":
                        foreground = JsonSerializer.Deserialize (ref reader, TuiSerializerContext.Instance.Color);

                        break;
                    case "background":
                        background = JsonSerializer.Deserialize (ref reader, TuiSerializerContext.Instance.Color);

                        break;
                    case "style":
                        if (reader.TokenType != JsonTokenType.String)
                        {
                            throw new JsonException ($"{propertyName}: Expected a string value.");
                        }

                        try
                        {
                            style = Enum.Parse<TextStyle> (reader.GetString ()!, ignoreCase: true);
                        }
                        catch (ArgumentException ex)
                        {
                            throw new JsonException ("Expected a valid text style value.", ex);
                        }

                        break;

                    default:
                        throw new JsonException ($"{propertyName}: Unknown Attribute property .");
                }
            }
            catch (JsonException ex)

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Change the Style value to a TextStyle enum name string (case-insensitive), e.g. "Bold", "Italic", "Underline", "None", or a comma-joined combination the parser accepts.
  2. If your exporter emits numbers, re-export with string enum serialization.
  3. Validate with a JSON schema that types Style as string.

Example fix

// before
{ "Foreground": "Red", "Background": "Black", "Style": 1 }
// after
{ "Foreground": "Red", "Background": "Black", "Style": "Bold" }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure Style is a string token before deserializing.
using var doc = JsonDocument.Parse (jsonString);
foreach (var attr in doc.RootElement.DescendantElements ().Where (e => e.TryGetProperty ("Style", out _)))
{
    if (attr.GetProperty ("Style").ValueKind != JsonValueKind.String)
        throw new FormatException ("Attribute.Style must be a string.");
}

Try / catch

try { ConfigurationManager.Apply (); }
catch (JsonException ex) when (ex.Message.Contains ("Expected a string value"))
{ /* fix the Style value in config */ }

Prevention

When it happens

Trigger: A config Attribute has "Style": 123, "Style": true, or "Style": { ... } instead of a string enum name like "Bold".

Common situations: Editor auto-completed a numeric value; config exported from a system that serializes enum-as-number (Terminal.Gui expects enum-as-string); misremembering TextStyle as a numeric flags value.

Related errors


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