32 lines
894 B
C#
32 lines
894 B
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic.Yandex;
|
|
|
|
/// <summary>
|
|
/// Reads numbers that Yandex sometimes returns as strings.
|
|
/// </summary>
|
|
public sealed class FlexibleLongConverter : JsonConverter<long>
|
|
{
|
|
/// <inheritdoc />
|
|
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.Number)
|
|
{
|
|
return reader.GetInt64();
|
|
}
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
var value = reader.GetString();
|
|
return long.TryParse(value, out var result) ? result : 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
|
|
=> writer.WriteNumberValue(value);
|
|
}
|