AttributeJsonConverter.cs 3.0 KB

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