KeyJsonConverter.cs 675 B

123456789101112131415161718
  1. #nullable disable
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui.Configuration;
  5. /// <summary>Support for <see cref="Key"/> in JSON in the form of "Ctrl-X" or "Alt-Shift-F1".</summary>
  6. public class KeyJsonConverter : JsonConverter<Key>
  7. {
  8. /// <inheritdoc/>
  9. public override Key Read (ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  10. {
  11. return Key.TryParse (reader.GetString (), out Key key) ? key : Key.Empty;
  12. }
  13. /// <inheritdoc/>
  14. public override void Write (Utf8JsonWriter writer, Key value, JsonSerializerOptions options) { writer.WriteStringValue (value.ToString ()); }
  15. }