Remove unreachable branches from JsonConverters

* If the type is a reference type we don't have to handle null ourselves
* reader.ValueSpan is only valid if reader.HasValueSequence is false
This commit is contained in:
Bond_009 2021-12-19 02:17:32 +01:00
parent 923720c988
commit ea9fc9f9cc
5 changed files with 25 additions and 45 deletions

View File

@ -9,7 +9,7 @@ namespace Jellyfin.Extensions.Json.Converters
/// Convert delimited string to array of type. /// Convert delimited string to array of type.
/// </summary> /// </summary>
/// <typeparam name="T">Type to convert to.</typeparam> /// <typeparam name="T">Type to convert to.</typeparam>
public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]?> public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]>
{ {
private readonly TypeConverter _typeConverter; private readonly TypeConverter _typeConverter;
@ -31,9 +31,9 @@ namespace Jellyfin.Extensions.Json.Converters
{ {
if (reader.TokenType == JsonTokenType.String) if (reader.TokenType == JsonTokenType.String)
{ {
// GetString can't return null here because we already handled it above // null got handled higher up the call stack
var stringEntries = reader.GetString()?.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries); var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
if (stringEntries == null || stringEntries.Length == 0) if (stringEntries.Length == 0)
{ {
return Array.Empty<T>(); return Array.Empty<T>();
} }

View File

@ -12,15 +12,19 @@ namespace Jellyfin.Extensions.Json.Converters
{ {
/// <inheritdoc /> /// <inheritdoc />
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ => reader.TokenType == JsonTokenType.Null
var guidStr = reader.GetString(); ? Guid.Empty
return guidStr == null ? Guid.Empty : new Guid(guidStr); : ReadInternal(ref reader);
}
// TODO: optimize by parsing the UTF8 bytes instead of converting to string first
internal static Guid ReadInternal(ref Utf8JsonReader reader)
=> Guid.Parse(reader.GetString()!); // null got handled higher up the call stack
/// <inheritdoc /> /// <inheritdoc />
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
{ => WriteInternal(writer, value);
writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
} internal static void WriteInternal(Utf8JsonWriter writer, Guid value)
=> writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
} }
} }

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Globalization;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -12,21 +11,19 @@ namespace Jellyfin.Extensions.Json.Converters
{ {
/// <inheritdoc /> /// <inheritdoc />
public override Guid? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override Guid? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ => JsonGuidConverter.ReadInternal(ref reader);
var guidStr = reader.GetString();
return guidStr == null ? null : new Guid(guidStr);
}
/// <inheritdoc /> /// <inheritdoc />
public override void Write(Utf8JsonWriter writer, Guid? value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, Guid? value, JsonSerializerOptions options)
{ {
if (value == null || value == Guid.Empty) if (value == Guid.Empty)
{ {
writer.WriteNullValue(); writer.WriteNullValue();
} }
else else
{ {
writer.WriteStringValue(value.Value.ToString("N", CultureInfo.InvariantCulture)); // null got handled higher up the call stack
JsonGuidConverter.WriteInternal(writer, value!.Value);
} }
} }
} }

View File

@ -15,13 +15,10 @@ namespace Jellyfin.Extensions.Json.Converters
/// <inheritdoc /> /// <inheritdoc />
public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ {
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
// Token is empty string. // Token is empty string.
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty)) if (reader.TokenType == JsonTokenType.String
&& ((reader.HasValueSequence && reader.ValueSequence.IsEmpty)
|| (!reader.HasValueSequence && reader.ValueSpan.IsEmpty)))
{ {
return null; return null;
} }
@ -31,15 +28,6 @@ namespace Jellyfin.Extensions.Json.Converters
/// <inheritdoc /> /// <inheritdoc />
public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options)
{ => JsonSerializer.Serialize(writer, value!.Value, options); // null got handled higher up the call stack
if (value.HasValue)
{
JsonSerializer.Serialize(writer, value.Value, options);
}
else
{
writer.WriteNullValue();
}
}
} }
} }

View File

@ -13,20 +13,11 @@ namespace Jellyfin.Extensions.Json.Converters
{ {
/// <inheritdoc /> /// <inheritdoc />
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{ => reader.TokenType == JsonTokenType.String ? reader.GetString() : GetRawValue(reader);
return reader.TokenType switch
{
JsonTokenType.Null => null,
JsonTokenType.String => reader.GetString(),
_ => GetRawValue(reader)
};
}
/// <inheritdoc /> /// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{ => writer.WriteStringValue(value);
writer.WriteStringValue(value);
}
private static string GetRawValue(Utf8JsonReader reader) private static string GetRawValue(Utf8JsonReader reader)
{ {