parent
582c016d3b
commit
8b96881aa1
@ -1,40 +0,0 @@
|
||||
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 int32 object or value to/from JSON.
|
||||
/// </summary>
|
||||
public class JsonInt32Converter : 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;
|
||||
}
|
||||
|
||||
if (int.TryParse(reader.GetString(), out number))
|
||||
{
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
return reader.GetInt32();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(value);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Parse JSON string as long.
|
||||
/// Javascript does not support 64-bit integers.
|
||||
/// </summary>
|
||||
public class JsonInt64Converter : JsonConverter<long>
|
||||
{
|
||||
/// <summary>
|
||||
/// Read JSON string as int64.
|
||||
/// </summary>
|
||||
/// <param name="reader"><see cref="Utf8JsonReader"/>.</param>
|
||||
/// <param name="type">Type.</param>
|
||||
/// <param name="options">Options.</param>
|
||||
/// <returns>Parsed value.</returns>
|
||||
public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
// try to parse from a string if the above failed, this covers cases with other escaped/UTF characters
|
||||
if (long.TryParse(reader.GetString(), out number))
|
||||
{
|
||||
return number;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to default handling
|
||||
return reader.GetInt64();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write long to JSON long.
|
||||
/// </summary>
|
||||
/// <param name="writer"><see cref="Utf8JsonWriter"/>.</param>
|
||||
/// <param name="value">Value to write.</param>
|
||||
/// <param name="options">Options.</param>
|
||||
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(value);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
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.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Parse JSON string as nullable long.
|
||||
/// Javascript does not support 64-bit integers.
|
||||
/// </summary>
|
||||
public class JsonNullableInt64Converter : JsonConverter<long?>
|
||||
{
|
||||
/// <summary>
|
||||
/// Read JSON string as int64.
|
||||
/// </summary>
|
||||
/// <param name="reader"><see cref="Utf8JsonReader"/>.</param>
|
||||
/// <param name="type">Type.</param>
|
||||
/// <param name="options">Options.</param>
|
||||
/// <returns>Parsed value.</returns>
|
||||
public override long? Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to default handling
|
||||
return reader.GetInt64();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write long to JSON long.
|
||||
/// </summary>
|
||||
/// <param name="writer"><see cref="Utf8JsonWriter"/>.</param>
|
||||
/// <param name="value">Value to write.</param>
|
||||
/// <param name="options">Options.</param>
|
||||
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNumberValue(value.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue