2020-09-01 07:12:36 -07:00
|
|
|
|
using System;
|
2020-09-01 08:19:22 -07:00
|
|
|
|
using System.ComponentModel;
|
2020-09-01 07:12:36 -07:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Json.Converters
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-09-01 08:19:22 -07:00
|
|
|
|
/// Converts a nullable int64 object or value to/from JSON.
|
2020-09-01 07:12:36 -07:00
|
|
|
|
/// Required - some clients send an empty string.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class JsonNullableInt64Converter : JsonConverter<long?>
|
|
|
|
|
{
|
2020-09-01 08:19:22 -07:00
|
|
|
|
private readonly JsonConverter<long?> _baseJsonConverter;
|
|
|
|
|
|
2020-09-01 07:12:36 -07:00
|
|
|
|
/// <summary>
|
2020-09-01 08:19:22 -07:00
|
|
|
|
/// Initializes a new instance of the <see cref="JsonNullableInt64Converter"/> class.
|
2020-09-01 07:12:36 -07:00
|
|
|
|
/// </summary>
|
2020-09-01 08:19:22 -07:00
|
|
|
|
/// <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)
|
2020-09-01 07:12:36 -07:00
|
|
|
|
{
|
2020-09-01 08:19:22 -07:00
|
|
|
|
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
|
2020-09-01 07:12:36 -07:00
|
|
|
|
{
|
2020-09-01 08:19:22 -07:00
|
|
|
|
return null;
|
2020-09-01 07:12:36 -07:00
|
|
|
|
}
|
2020-09-01 08:19:22 -07:00
|
|
|
|
|
|
|
|
|
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
|
2020-09-01 07:12:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 08:19:22 -07:00
|
|
|
|
/// <inheritdoc />
|
2020-09-01 07:12:36 -07:00
|
|
|
|
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
|
|
|
|
{
|
2020-09-01 08:19:22 -07:00
|
|
|
|
_baseJsonConverter.Write(writer, value, options);
|
2020-09-01 07:12:36 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|