AttributeJsonConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Text.Json;
  2. using System.Text.Json.Serialization;
  3. namespace Terminal.Gui;
  4. /// <summary>Json converter from the <see cref="Attribute"/> class.</summary>
  5. internal class AttributeJsonConverter : JsonConverter<Attribute>
  6. {
  7. private static AttributeJsonConverter _instance;
  8. /// <summary></summary>
  9. public static AttributeJsonConverter Instance
  10. {
  11. get
  12. {
  13. if (_instance is null)
  14. {
  15. _instance = new AttributeJsonConverter ();
  16. }
  17. return _instance;
  18. }
  19. }
  20. public override Attribute Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  21. {
  22. if (reader.TokenType != JsonTokenType.StartObject)
  23. {
  24. throw new JsonException ($"Unexpected StartObject token when parsing Attribute: {reader.TokenType}.");
  25. }
  26. var attribute = new Attribute ();
  27. Color? foreground = null;
  28. Color? background = null;
  29. while (reader.Read ())
  30. {
  31. if (reader.TokenType == JsonTokenType.EndObject)
  32. {
  33. if (foreground is null || background is null)
  34. {
  35. throw new JsonException ("Both Foreground and Background colors must be provided.");
  36. }
  37. return new Attribute (foreground.Value, background.Value);
  38. }
  39. if (reader.TokenType != JsonTokenType.PropertyName)
  40. {
  41. throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}.");
  42. }
  43. string propertyName = reader.GetString ();
  44. reader.Read ();
  45. var color = $"\"{reader.GetString ()}\"";
  46. switch (propertyName?.ToLower ())
  47. {
  48. case "foreground":
  49. foreground = JsonSerializer.Deserialize (color, _serializerContext.Color);
  50. break;
  51. case "background":
  52. background = JsonSerializer.Deserialize (color, _serializerContext.Color);
  53. break;
  54. //case "bright":
  55. //case "bold":
  56. // attribute.Bright = reader.GetBoolean ();
  57. // break;
  58. //case "dim":
  59. // attribute.Dim = reader.GetBoolean ();
  60. // break;
  61. //case "underline":
  62. // attribute.Underline = reader.GetBoolean ();
  63. // break;
  64. //case "blink":
  65. // attribute.Blink = reader.GetBoolean ();
  66. // break;
  67. //case "reverse":
  68. // attribute.Reverse = reader.GetBoolean ();
  69. // break;
  70. //case "hidden":
  71. // attribute.Hidden = reader.GetBoolean ();
  72. // break;
  73. //case "strike-through":
  74. // attribute.StrikeThrough = reader.GetBoolean ();
  75. // break;
  76. default:
  77. throw new JsonException ($"Unknown Attribute property {propertyName}.");
  78. }
  79. }
  80. throw new JsonException ();
  81. }
  82. public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options)
  83. {
  84. writer.WriteStartObject ();
  85. writer.WritePropertyName (nameof (Attribute.Foreground));
  86. ColorJsonConverter.Instance.Write (writer, value.Foreground, options);
  87. writer.WritePropertyName (nameof (Attribute.Background));
  88. ColorJsonConverter.Instance.Write (writer, value.Background, options);
  89. writer.WriteEndObject ();
  90. }
  91. }