| | | 1 | | using System; |
| | | 2 | | using System.Buffers; |
| | | 3 | | using System.Buffers.Text; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Extensions.Json.Converters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Converts a string to a boolean. |
| | | 11 | | /// This is needed for FFprobe. |
| | | 12 | | /// </summary> |
| | | 13 | | public class JsonBoolStringConverter : JsonConverter<bool> |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | | 17 | | { |
| | 10 | 18 | | if (reader.TokenType == JsonTokenType.String) |
| | | 19 | | { |
| | 10 | 20 | | ReadOnlySpan<byte> utf8Span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan; |
| | 10 | 21 | | if (Utf8Parser.TryParse(utf8Span, out bool val, out _, 'l')) |
| | | 22 | | { |
| | 10 | 23 | | return val; |
| | | 24 | | } |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | return reader.GetBoolean(); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) |
| | 2 | 32 | | => writer.WriteBooleanValue(value); |
| | | 33 | | } |