jellyfin/MediaBrowser.Common/Json/Converters/JsonNullableInt64Converter.cs
2020-09-01 09:19:22 -06:00

43 lines
1.5 KiB
C#

using System;
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MediaBrowser.Common.Json.Converters
{
/// <summary>
/// Converts a nullable int64 object or value to/from JSON.
/// Required - some clients send an empty string.
/// </summary>
public class JsonNullableInt64Converter : JsonConverter<long?>
{
private readonly JsonConverter<long?> _baseJsonConverter;
/// <summary>
/// Initializes a new instance of the <see cref="JsonNullableInt64Converter"/> class.
/// </summary>
/// <param name="baseJsonConverter">The base json converter.</param>
public JsonNullableInt64Converter(JsonConverter<long?> baseJsonConverter)
{
_baseJsonConverter = baseJsonConverter;
}
/// <inheritdoc />
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
{
return null;
}
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
{
_baseJsonConverter.Write(writer, value, options);
}
}
}