Skip to content

Commit

Permalink
imp - Color templates support numbers!
Browse files Browse the repository at this point in the history
---

We've added support for literal integers for deserializing colors from JSON.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 17, 2024
1 parent ed34c28 commit 31bffa5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Terminaux.Console/Fixtures/Cases/Writer/PrintTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void RunFixture()
"Name": "{{name}}",
"Components": {
"Text": "#876543",
"Component": "#345678"
"Component": 168555
}
}
""";
Expand Down
9 changes: 7 additions & 2 deletions Terminaux/Colors/ColorSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ public override bool CanConvert(Type objectType) =>
/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
var result = reader.Value is string value ? value : "";
var color = new Color(result);
Color color;
if (reader.Value is string colorValueString)
color = new Color(colorValueString);
else if (reader.Value is long colorValueLong)
color = new Color((int)colorValueLong);
else
throw new TerminauxException($"Can't determine how to convert a(n) {reader.TokenType} of {reader.ValueType?.Name ?? "an unknown type"} to a color.");
return color;
}

Expand Down

0 comments on commit 31bffa5

Please sign in to comment.