JsonBoolNumberConverter.cs 1012 B

123456789101112131415161718192021222324252627282930313233
  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 number to a boolean.
  8. /// This is needed for HDHomerun.
  9. /// </summary>
  10. /// <remarks>
  11. /// Adding this to the JsonConverter list causes recursion.
  12. /// </remarks>
  13. public class JsonBoolNumberConverter : JsonConverter<bool>
  14. {
  15. /// <inheritdoc />
  16. public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  17. {
  18. if (reader.TokenType == JsonTokenType.Number)
  19. {
  20. return Convert.ToBoolean(reader.GetInt32());
  21. }
  22. return JsonSerializer.Deserialize<bool>(ref reader, options);
  23. }
  24. /// <inheritdoc />
  25. public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
  26. {
  27. JsonSerializer.Serialize(writer, value, options);
  28. }
  29. }
  30. }