JsonGuidConverter.cs 819 B

1234567891011121314151617181920212223242526272829
  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 GUID object or value to/from JSON.
  8. /// </summary>
  9. public class JsonGuidConverter : JsonConverter<Guid>
  10. {
  11. /// <inheritdoc />
  12. public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  13. => new Guid(reader.GetString());
  14. /// <inheritdoc />
  15. public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
  16. {
  17. if (value == Guid.Empty)
  18. {
  19. writer.WriteNullValue();
  20. }
  21. else
  22. {
  23. writer.WriteStringValue(value);
  24. }
  25. }
  26. }
  27. }