tui-cs/Terminal.Gui · error · JsonException
{propertyName}: Both Foreground and Background colors must b
Error message
{propertyName}: Both Foreground and Background colors must be provided. What it means
Thrown as JsonException by AttributeJsonConverter.Read at the EndObject token when either Foreground or Background is still null. An Attribute requires both a foreground and a background color; supplying only one is ambiguous. The message includes the last-read propertyName for context. Style is optional.
Source
Thrown at Terminal.Gui/Configuration/AttributeJsonConverter.cs:45
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException ($"Unexpected StartObject token when parsing Attribute: {reader.TokenType}.");
}
var attribute = new Attribute ();
Color? foreground = null;
Color? background = null;
TextStyle? style = null;
string propertyName = string.Empty;
while (reader.Read ())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
if (foreground is null || background is null)
{
throw new JsonException ($"{propertyName}: Both Foreground and Background colors must be provided.");
}
if (style.HasValue)
{
return new Attribute (foreground.Value, background.Value, style.Value);
}
else
{
return new Attribute (foreground.Value, background.Value);
}
}
if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException ($"{propertyName}: Unexpected token when parsing Attribute: {reader.TokenType}.");
}
propertyName = reader.GetString ()!;View on GitHub (pinned to 2e47b11478)
Solutions
- Provide both Foreground and Background in every Attribute object in config JSON.
- Double-check key spelling and casing: 'Foreground' and 'Background' (the parser lowercases for comparison, but typos still miss).
- Validate the config file with a schema or a quick parse test before applying it.
Example fix
// before (config.json)
"Normal": { "Foreground": "red" }
// after
"Normal": { "Foreground": "red", "Background": "black" } Defensive patterns
Strategy: validation
Validate before calling
// Validate config JSON before applying:
// every Attribute object must contain both Foreground and Background.
foreach (var attr in /* locate Attribute objects */)
{
bool hasFg = attr.TryGetProperty ("Foreground", out _);
bool hasBg = attr.TryGetProperty ("Background", out _);
if (!hasFg || !hasBg)
{
throw new InvalidOperationException ("Attribute requires both Foreground and Background.");
}
} Try / catch
try
{
ConfigurationManager.Load (configText);
}
catch (JsonException ex)
{
Logging.Error ($"Incomplete Attribute in config: {ex.Message}");
} Prevention
- Always include both Foreground and Background in every Attribute object.
- Check key spelling and casing (Foreground/Background).
- Test-parse theme JSON before applying it in production.
When it happens
Trigger: Writing an Attribute object in config JSON with only a Foreground key and no Background (or vice versa); omitting a color field; a typo in the key name (e.g. "Foregound") so the parser treats it as an unknown property and never assigns the nullable.
Common situations: Hand-authoring theme JSON and forgetting the second color; copy-paste errors in large scheme definitions; key casing/typos that silently bypass the Foreground/Background cases.
Related errors
- Unexpected StartObject token when parsing Attribute: {reader
- {propertyName}: Json error in ScopeJsonConverter
- {p.Key}: Unsupported configuration converter type "{converte
- {p.Key}: No source-generated JsonTypeInfo is registered for
- {propertyType.FullName}: No source-generated JsonTypeInfo is
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/7235e13723151d24.
Report an issue: GitHub.