You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.4 KiB
41 lines
1.4 KiB
5 years ago
|
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 GUID object or value to/from JSON.
|
||
|
/// </summary>
|
||
|
public class JsonInt32Converter : JsonConverter<int>
|
||
|
{
|
||
|
/// <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 = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
||
|
if (!Utf8Parser.TryParse(span, out int number, out _))
|
||
|
{
|
||
|
ThrowFormatException();
|
||
|
}
|
||
|
|
||
|
return number;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
|
||
|
{
|
||
|
static void ThrowInvalidOperationException() => throw new InvalidOperationException();
|
||
|
Span<byte> span = new byte[16];
|
||
|
if (Utf8Formatter.TryFormat(value, span, out int bytesWritten))
|
||
|
{
|
||
|
writer.WriteStringValue(span.Slice(0, bytesWritten));
|
||
|
}
|
||
|
|
||
|
ThrowInvalidOperationException();
|
||
|
}
|
||
|
}
|
||
|
}
|