using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Dpz.Core.Service.ObjectStorage.VideoHandle;
public class AvOptsConverter : JsonConverter<AvOpts>
{
public override AvOpts? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
var value = reader.GetString();
if (!string.IsNullOrEmpty(value))
{
var buffer = new byte[
(value.Length * 3 + 3) / 4
- (
value.Length > 0 && value[^1] == '='
? value.Length > 1 && value[^2] == '='
? 2
: 1
: 0
)
];
var bytes = Convert.TryFromBase64String(value, buffer, out _) ? buffer : [];
var json = Encoding.UTF8.GetString(bytes);
try
{
var list = JsonSerializer.Deserialize<List<OptionType>>(json) ?? [];
return new AvOpts(list);
}
catch (Exception)
{
// ignored
}
}
return [];
}
public override void Write(Utf8JsonWriter writer, AvOpts value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}