using System; using System.Text.Json.Serialization; using System.Text.Json; using System.Text.RegularExpressions; namespace Terminal.Gui { /// /// Json converter for the class. /// internal class ColorJsonConverter : JsonConverter { private static ColorJsonConverter _instance; /// /// Singleton /// public static ColorJsonConverter Instance { get { if (_instance == null) { _instance = new ColorJsonConverter (); } return _instance; } } public override Color Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { // Check if the value is a string if (reader.TokenType == JsonTokenType.String) { // Get the color string var colorString = reader.GetString (); // Check if the color string is a color name if (Enum.TryParse (colorString, ignoreCase: true, out ColorNames color)) { // Return the parsed color return new Color(color); } if (Color.TryParse (colorString, out Color parsedColor)) { return parsedColor; } throw new JsonException ($"Unexpected color name: {colorString}."); } else { throw new JsonException ($"Unexpected token when parsing Color: {reader.TokenType}"); } } public override void Write (Utf8JsonWriter writer, Color value, JsonSerializerOptions options) { writer.WriteStringValue (value.ToString()); } } }