AttributeJsonConverter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. int valuePair = 0;
  31. while (reader.Read ()) {
  32. if (reader.TokenType == JsonTokenType.EndObject) {
  33. if (foreground == (Color)(-1) || background == (Color)(-1)) {
  34. throw new JsonException ($"Both Foreground and Background colors must be provided.");
  35. }
  36. return attribute;
  37. }
  38. if (reader.TokenType != JsonTokenType.PropertyName) {
  39. throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}.");
  40. }
  41. string propertyName = reader.GetString ();
  42. reader.Read ();
  43. string color = $"\"{reader.GetString ()}\"";
  44. valuePair++;
  45. switch (propertyName.ToLower ()) {
  46. case "foreground":
  47. foreground = JsonSerializer.Deserialize<Color> (color, options);
  48. break;
  49. case "background":
  50. background = JsonSerializer.Deserialize<Color> (color, options);
  51. break;
  52. //case "Bright":
  53. // attribute.Bright = reader.GetBoolean ();
  54. // break;
  55. //case "Underline":
  56. // attribute.Underline = reader.GetBoolean ();
  57. // break;
  58. //case "Reverse":
  59. // attribute.Reverse = reader.GetBoolean ();
  60. // break;
  61. default:
  62. throw new JsonException ($"Unknown Attribute property {propertyName}.");
  63. }
  64. if (valuePair == 2) {
  65. attribute = new Attribute (foreground, background);
  66. valuePair = 0;
  67. }
  68. }
  69. throw new JsonException ();
  70. }
  71. public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options)
  72. {
  73. writer.WriteStartObject ();
  74. writer.WritePropertyName ("Foreground");
  75. ColorJsonConverter.Instance.Write (writer, value.Foreground, options);
  76. writer.WritePropertyName ("Background");
  77. ColorJsonConverter.Instance.Write (writer, value.Background, options);
  78. writer.WriteEndObject ();
  79. }
  80. }
  81. }