ColorSchemeJsonConverter.cs 2.9 KB

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