ColorSchemeJsonConverter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Text.Json;
  2. using System.Text.Json.Serialization;
  3. namespace Terminal.Gui;
  4. /// <summary>Implements a JSON converter for <see cref="ColorScheme"/>.</summary>
  5. internal class ColorSchemeJsonConverter : JsonConverter<ColorScheme>
  6. {
  7. private static ColorSchemeJsonConverter instance;
  8. /// <summary>Singleton</summary>
  9. public static ColorSchemeJsonConverter Instance
  10. {
  11. get
  12. {
  13. if (instance is null)
  14. {
  15. instance = new ColorSchemeJsonConverter ();
  16. }
  17. return instance;
  18. }
  19. }
  20. /// <inheritdoc/>
  21. public override ColorScheme Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  22. {
  23. if (reader.TokenType != JsonTokenType.StartObject)
  24. {
  25. throw new JsonException ($"Unexpected StartObject token when parsing ColorScheme: {reader.TokenType}.");
  26. }
  27. var normal = Attribute.Default;
  28. var focus = Attribute.Default;
  29. var hotNormal = Attribute.Default;
  30. var hotFocus = Attribute.Default;
  31. var disabled = Attribute.Default;
  32. while (reader.Read ())
  33. {
  34. if (reader.TokenType == JsonTokenType.EndObject)
  35. {
  36. var colorScheme = new ColorScheme
  37. {
  38. Normal = normal,
  39. Focus = focus,
  40. HotNormal = hotNormal,
  41. HotFocus = hotFocus,
  42. Disabled = disabled
  43. };
  44. return colorScheme;
  45. }
  46. if (reader.TokenType != JsonTokenType.PropertyName)
  47. {
  48. throw new JsonException ($"Unexpected token when parsing Attribute: {reader.TokenType}.");
  49. }
  50. string propertyName = reader.GetString ();
  51. reader.Read ();
  52. var attribute = JsonSerializer.Deserialize (ref reader, _serializerContext.Attribute);
  53. switch (propertyName.ToLower ())
  54. {
  55. case "normal":
  56. normal = attribute;
  57. break;
  58. case "focus":
  59. focus = attribute;
  60. break;
  61. case "hotnormal":
  62. hotNormal = attribute;
  63. break;
  64. case "hotfocus":
  65. hotFocus = attribute;
  66. break;
  67. case "disabled":
  68. disabled = attribute;
  69. break;
  70. default:
  71. throw new JsonException ($"Unrecognized ColorScheme Attribute name: {propertyName}.");
  72. }
  73. }
  74. throw new JsonException ();
  75. }
  76. /// <inheritdoc/>
  77. public override void Write (Utf8JsonWriter writer, ColorScheme value, JsonSerializerOptions options)
  78. {
  79. writer.WriteStartObject ();
  80. writer.WritePropertyName ("Normal");
  81. AttributeJsonConverter.Instance.Write (writer, value.Normal, options);
  82. writer.WritePropertyName ("Focus");
  83. AttributeJsonConverter.Instance.Write (writer, value.Focus, options);
  84. writer.WritePropertyName ("HotNormal");
  85. AttributeJsonConverter.Instance.Write (writer, value.HotNormal, options);
  86. writer.WritePropertyName ("HotFocus");
  87. AttributeJsonConverter.Instance.Write (writer, value.HotFocus, options);
  88. writer.WritePropertyName ("Disabled");
  89. AttributeJsonConverter.Instance.Write (writer, value.Disabled, options);
  90. writer.WriteEndObject ();
  91. }
  92. }