AttributeJsonConverter.cs 2.8 KB

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