mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Fallback to base jsonconverter
This commit is contained in:
parent
1f2d73af8e
commit
eb400f7292
@ -10,31 +10,32 @@ namespace MediaBrowser.Common.Json.Converters
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class JsonNullableInt32Converter : JsonConverter<int?>
|
public class JsonNullableInt32Converter : JsonConverter<int?>
|
||||||
{
|
{
|
||||||
|
private readonly JsonConverter<int?> _baseJsonConverter;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="JsonNullableInt32Converter"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="baseJsonConverter">The base json converter.</param>
|
||||||
|
public JsonNullableInt32Converter(JsonConverter<int?> baseJsonConverter)
|
||||||
|
{
|
||||||
|
_baseJsonConverter = baseJsonConverter;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
switch (reader.TokenType)
|
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
|
||||||
{
|
{
|
||||||
case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty:
|
return null;
|
||||||
case JsonTokenType.Null:
|
|
||||||
return null;
|
|
||||||
default:
|
|
||||||
// fallback to default handling
|
|
||||||
return reader.GetInt32();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
if (value is null)
|
_baseJsonConverter.Write(writer, value, options);
|
||||||
{
|
|
||||||
writer.WriteNullValue();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
writer.WriteNumberValue(value.Value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,52 +1,42 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace MediaBrowser.Common.Json.Converters
|
namespace MediaBrowser.Common.Json.Converters
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parse JSON string as nullable long.
|
/// Converts a nullable int64 object or value to/from JSON.
|
||||||
/// Javascript does not support 64-bit integers.
|
|
||||||
/// Required - some clients send an empty string.
|
/// Required - some clients send an empty string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class JsonNullableInt64Converter : JsonConverter<long?>
|
public class JsonNullableInt64Converter : JsonConverter<long?>
|
||||||
{
|
{
|
||||||
/// <summary>
|
private readonly JsonConverter<long?> _baseJsonConverter;
|
||||||
/// 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)
|
|
||||||
{
|
|
||||||
switch (reader.TokenType)
|
|
||||||
{
|
|
||||||
case JsonTokenType.String when (reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty:
|
|
||||||
case JsonTokenType.Null:
|
|
||||||
return null;
|
|
||||||
default:
|
|
||||||
// fallback to default handling
|
|
||||||
return reader.GetInt64();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write long to JSON long.
|
/// Initializes a new instance of the <see cref="JsonNullableInt64Converter"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="writer"><see cref="Utf8JsonWriter"/>.</param>
|
/// <param name="baseJsonConverter">The base json converter.</param>
|
||||||
/// <param name="value">Value to write.</param>
|
public JsonNullableInt64Converter(JsonConverter<long?> baseJsonConverter)
|
||||||
/// <param name="options">Options.</param>
|
{
|
||||||
|
_baseJsonConverter = baseJsonConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
if (value is null)
|
_baseJsonConverter.Write(writer, value, options);
|
||||||
{
|
|
||||||
writer.WriteNullValue();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
writer.WriteNumberValue(value.Value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,14 @@ namespace MediaBrowser.Common.Json
|
|||||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get built-in converters for fallback converting.
|
||||||
|
var baseNullableInt32Converter = (JsonConverter<int?>)options.GetConverter(typeof(int?));
|
||||||
|
var baseNullableInt64Converter = (JsonConverter<long?>)options.GetConverter(typeof(long?));
|
||||||
|
|
||||||
options.Converters.Add(new JsonGuidConverter());
|
options.Converters.Add(new JsonGuidConverter());
|
||||||
options.Converters.Add(new JsonStringEnumConverter());
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
options.Converters.Add(new JsonNullableInt32Converter());
|
options.Converters.Add(new JsonNullableInt32Converter(baseNullableInt32Converter));
|
||||||
options.Converters.Add(new JsonNullableInt64Converter());
|
options.Converters.Add(new JsonNullableInt64Converter(baseNullableInt64Converter));
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user