AttributeJsonConverter.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public 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. /// <inheritdoc/>
  23. public override Attribute Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  24. {
  25. if (reader.TokenType != JsonTokenType.StartObject) {
  26. throw new JsonException ($"Unexpected StartObject token when parsing Attribute: {reader.TokenType}.");
  27. }
  28. Attribute attribute = new Attribute ();
  29. Color foreground = (Color)(-1);
  30. Color background = (Color)(-1);
  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. switch (propertyName.ToLower ()) {
  45. case "foreground":
  46. foreground = JsonSerializer.Deserialize<Color> (color, options);
  47. break;
  48. case "background":
  49. background = JsonSerializer.Deserialize<Color> (color, options);
  50. break;
  51. //case "Bright":
  52. // attribute.Bright = reader.GetBoolean ();
  53. // break;
  54. //case "Underline":
  55. // attribute.Underline = reader.GetBoolean ();
  56. // break;
  57. //case "Reverse":
  58. // attribute.Reverse = reader.GetBoolean ();
  59. // break;
  60. default:
  61. throw new JsonException ($"Unknown Attribute property {propertyName}.");
  62. }
  63. attribute = new Attribute (foreground, background);
  64. }
  65. throw new JsonException ();
  66. }
  67. /// <inheritdoc/>
  68. public override void Write (Utf8JsonWriter writer, Attribute value, JsonSerializerOptions options)
  69. {
  70. writer.WriteStartObject ();
  71. writer.WritePropertyName ("Foreground");
  72. ColorJsonConverter.Instance.Write (writer, value.Foreground, options);
  73. writer.WritePropertyName ("Background");
  74. ColorJsonConverter.Instance.Write (writer, value.Background, options);
  75. writer.WriteEndObject ();
  76. }
  77. }
  78. }