mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Only handle empty string or null case
This commit is contained in:
parent
9b2359a453
commit
1f2d73af8e
@ -1,6 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Buffers;
|
|
||||||
using System.Buffers.Text;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
@ -15,30 +13,16 @@ namespace MediaBrowser.Common.Json.Converters
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonTokenType.String)
|
switch (reader.TokenType)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
|
case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty:
|
||||||
|
case JsonTokenType.Null:
|
||||||
return null;
|
return null;
|
||||||
}
|
default:
|
||||||
|
// fallback to default handling
|
||||||
if (int.TryParse(stringValue, out number))
|
|
||||||
{
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return reader.GetInt32();
|
return reader.GetInt32();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Buffers;
|
|
||||||
using System.Buffers.Text;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
@ -22,33 +20,16 @@ namespace MediaBrowser.Common.Json.Converters
|
|||||||
/// <returns>Parsed value.</returns>
|
/// <returns>Parsed value.</returns>
|
||||||
public override long? Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
|
public override long? Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
if (reader.TokenType == JsonTokenType.String)
|
switch (reader.TokenType)
|
||||||
{
|
|
||||||
// try to parse number directly from bytes
|
|
||||||
var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
|
||||||
if (Utf8Parser.TryParse(span, out long number, out var bytesConsumed) && span.Length == bytesConsumed)
|
|
||||||
{
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
|
|
||||||
var stringValue = reader.GetString().AsSpan();
|
|
||||||
|
|
||||||
// value is null or empty, just return null.
|
|
||||||
if (stringValue.IsEmpty)
|
|
||||||
{
|
{
|
||||||
|
case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty:
|
||||||
|
case JsonTokenType.Null:
|
||||||
return null;
|
return null;
|
||||||
}
|
default:
|
||||||
|
|
||||||
// try to parse from a string if the above failed, this covers cases with other escaped/UTF characters
|
|
||||||
if (long.TryParse(stringValue, out number))
|
|
||||||
{
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fallback to default handling
|
// fallback to default handling
|
||||||
return reader.GetInt64();
|
return reader.GetInt64();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write long to JSON long.
|
/// Write long to JSON long.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user