Only handle empty string or null case

pull/4033/head
crobibero 4 years ago
parent 9b2359a453
commit 1f2d73af8e

@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.Buffers.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
@ -15,29 +13,15 @@ namespace MediaBrowser.Common.Json.Converters
/// <inheritdoc />
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;
}
if (int.TryParse(stringValue, out number))
{
return number;
}
default:
// fallback to default handling
return reader.GetInt32();
}
return reader.GetInt32();
}
/// <inheritdoc />

@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.Buffers.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
@ -22,32 +20,15 @@ namespace MediaBrowser.Common.Json.Converters
/// <returns>Parsed value.</returns>
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;
}
// 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;
}
default:
// fallback to default handling
return reader.GetInt64();
}
// fallback to default handling
return reader.GetInt64();
}
/// <summary>

Loading…
Cancel
Save