aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
diff options
context:
space:
mode:
authorcrobibero <cody@robibe.ro>2020-08-18 20:20:32 -0600
committercrobibero <cody@robibe.ro>2020-08-18 20:20:32 -0600
commitb43a8fb9d35846f458212d0ad17f3d52c896ef6e (patch)
tree339a40a3e2f2ee73a0ed73cef66fd4bc7c553fb9 /MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
parent2b5d515de79f2309219459c7223b8a56269737f8 (diff)
parent93fe595e5e9863874c1973e4f2d7f3c85549d3f9 (diff)
Merge remote-tracking branch 'upstream/master' into package-install-repo
Diffstat (limited to 'MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs')
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs37
1 files changed, 12 insertions, 25 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
index fe5dd6cd4..70c375b8c 100644
--- a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
+++ b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
@@ -14,40 +14,27 @@ namespace MediaBrowser.Common.Json.Converters
/// <inheritdoc />
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- static void ThrowFormatException() => throw new FormatException("Invalid format for an integer.");
- ReadOnlySpan<byte> span = stackalloc byte[0];
-
- if (reader.HasValueSequence)
- {
- long sequenceLength = reader.ValueSequence.Length;
- Span<byte> stackSpan = stackalloc byte[(int)sequenceLength];
- reader.ValueSequence.CopyTo(stackSpan);
- span = stackSpan;
- }
- else
+ if (reader.TokenType == JsonTokenType.String)
{
- span = reader.ValueSpan;
- }
+ 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;
+ }
- if (!Utf8Parser.TryParse(span, out int number, out _))
- {
- ThrowFormatException();
+ if (int.TryParse(reader.GetString(), out number))
+ {
+ return number;
+ }
}
- return number;
+ return reader.GetInt32();
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
- static void ThrowInvalidOperationException() => throw new InvalidOperationException();
- Span<byte> span = stackalloc byte[16];
- if (Utf8Formatter.TryFormat(value, span, out int bytesWritten))
- {
- writer.WriteStringValue(span.Slice(0, bytesWritten));
- }
-
- ThrowInvalidOperationException();
+ writer.WriteNumberValue(value);
}
}
}