AttributeJsonConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. TrueColor? trueColorForeground = null;
  31. TrueColor? trueColorBackground = null;
  32. while (reader.Read ()) {
  33. if (reader.TokenType == JsonTokenType.EndObject) {
  34. if (!attribute.TrueColorForeground.HasValue || !attribute.TrueColorBackground.HasValue) {
  35. throw new JsonException ($"Both Foreground and Background colors must be provided.");
  36. }
  37. return attribute;
  38. }
  39. if (reader.TokenType != JsonTokenType.PropertyName) {
  40. throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}.");
  41. }
  42. string propertyName = reader.GetString ();
  43. reader.Read ();
  44. string color = $"\"{reader.GetString ()}\"";
  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 "truecolorforeground":
  53. trueColorForeground = JsonSerializer.Deserialize<TrueColor> (color, options);
  54. break;
  55. case "truecolorbackground":
  56. trueColorBackground = JsonSerializer.Deserialize<TrueColor> (color, options);
  57. break;
  58. //case "Bright":
  59. // attribute.Bright = reader.GetBoolean ();
  60. // break;
  61. //case "Underline":
  62. // attribute.Underline = reader.GetBoolean ();
  63. // break;
  64. //case "Reverse":
  65. // attribute.Reverse = reader.GetBoolean ();
  66. // break;
  67. default:
  68. throw new JsonException ($"Unknown Attribute property {propertyName}.");
  69. }
  70. if (foreground != (Color)(-1) && background != (Color)(-1)) {
  71. attribute = new Attribute (foreground, background);
  72. }
  73. if (trueColorForeground.HasValue && trueColorBackground.HasValue) {
  74. attribute = new Attribute (trueColorForeground, trueColorBackground);
  75. }
  76. }
  77. throw new JsonException ();
  78. }
  79. public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options)
  80. {
  81. writer.WriteStartObject ();
  82. writer.WritePropertyName (nameof(Attribute.Foreground));
  83. ColorJsonConverter.Instance.Write (writer, value.Foreground, options);
  84. writer.WritePropertyName (nameof (Attribute.Background));
  85. ColorJsonConverter.Instance.Write (writer, value.Background, options);
  86. if (value.TrueColorForeground.HasValue && value.TrueColorBackground.HasValue) {
  87. writer.WritePropertyName (nameof (Attribute.TrueColorForeground));
  88. TrueColorJsonConverter.Instance.Write (writer, value.TrueColorForeground.Value, options);
  89. writer.WritePropertyName (nameof (Attribute.TrueColorBackground));
  90. TrueColorJsonConverter.Instance.Write (writer, value.TrueColorBackground.Value, options);
  91. }
  92. writer.WriteEndObject ();
  93. }
  94. }
  95. }