KeyJsonConverter.cs 643 B

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