mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 22:07:13 -04:00
56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Buffers.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace MediaBrowser.Common.Json.Converters
|
|
{
|
|
/// <summary>
|
|
/// Converts a nullable int32 object or value to/from JSON.
|
|
/// </summary>
|
|
public class JsonNullableInt32Converter : JsonConverter<int?>
|
|
{
|
|
/// <inheritdoc />
|
|
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
{
|
|
ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
|
if (Utf8Parser.TryParse(span, out int number, out int bytesConsumed) && span.Length == bytesConsumed)
|
|
{
|
|
return number;
|
|
}
|
|
|
|
var stringValue = reader.GetString().AsSpan();
|
|
|
|
// value is null or empty, just return null.
|
|
if (stringValue.IsEmpty)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (int.TryParse(stringValue, out number))
|
|
{
|
|
return number;
|
|
}
|
|
}
|
|
|
|
return reader.GetInt32();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
|
{
|
|
if (value is null)
|
|
{
|
|
writer.WriteNullValue();
|
|
}
|
|
else
|
|
{
|
|
writer.WriteNumberValue(value.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|