JsonVersionConverter.cs 842 B

1234567891011121314151617181920212223
  1. using System;
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace MediaBrowser.Common.Json.Converters
  5. {
  6. /// <summary>
  7. /// Converts a Version object or value to/from JSON.
  8. /// </summary>
  9. /// <remarks>
  10. /// Required to send <see cref="Version"/> as a string instead of an object.
  11. /// </remarks>
  12. public class JsonVersionConverter : JsonConverter<Version>
  13. {
  14. /// <inheritdoc />
  15. public override Version Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  16. => new Version(reader.GetString()!); // Will throw ArgumentNullException on null
  17. /// <inheritdoc />
  18. public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
  19. => writer.WriteStringValue(value.ToString());
  20. }
  21. }