Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unreachable branches from JsonConverters #7018

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Jellyfin.Extensions.Json.Converters
/// Convert delimited string to array of type.
/// </summary>
/// <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;

Expand All @@ -31,9 +31,9 @@ protected JsonDelimitedArrayConverter()
{
if (reader.TokenType == JsonTokenType.String)
{
// GetString can't return null here because we already handled it above
var stringEntries = reader.GetString()?.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
if (stringEntries == null || stringEntries.Length == 0)
// null got handled higher up the call stack
var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
if (stringEntries.Length == 0)
{
return Array.Empty<T>();
}
Expand Down
18 changes: 11 additions & 7 deletions src/Jellyfin.Extensions/Json/Converters/JsonGuidConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ public class JsonGuidConverter : JsonConverter<Guid>
{
/// <inheritdoc />
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var guidStr = reader.GetString();
return guidStr == null ? Guid.Empty : new Guid(guidStr);
}
=> reader.TokenType == JsonTokenType.Null
? Guid.Empty
: 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 />
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
}
=> WriteInternal(writer, value);

internal static void WriteInternal(Utf8JsonWriter writer, Guid value)
=> writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -12,21 +11,19 @@ public class JsonNullableGuidConverter : JsonConverter<Guid?>
{
/// <inheritdoc />
public override Guid? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var guidStr = reader.GetString();
return guidStr == null ? null : new Guid(guidStr);
}
=> JsonGuidConverter.ReadInternal(ref reader);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, Guid? value, JsonSerializerOptions options)
{
if (value == null || value == Guid.Empty)
if (value == Guid.Empty)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.Value.ToString("N", CultureInfo.InvariantCulture));
// null got handled higher up the call stack
JsonGuidConverter.WriteInternal(writer, value!.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ public class JsonNullableStructConverter<TStruct> : JsonConverter<TStruct?>
/// <inheritdoc />
public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

// 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;
}
Expand All @@ -31,15 +28,6 @@ public class JsonNullableStructConverter<TStruct> : JsonConverter<TStruct?>

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options)
{
if (value.HasValue)
{
JsonSerializer.Serialize(writer, value.Value, options);
}
else
{
writer.WriteNullValue();
}
}
=> JsonSerializer.Serialize(writer, value!.Value, options); // null got handled higher up the call stack
}
}
13 changes: 2 additions & 11 deletions src/Jellyfin.Extensions/Json/Converters/JsonStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,11 @@ public class JsonStringConverter : JsonConverter<string?>
{
/// <inheritdoc />
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.Null => null,
JsonTokenType.String => reader.GetString(),
_ => GetRawValue(reader)
};
}
=> reader.TokenType == JsonTokenType.String ? reader.GetString() : GetRawValue(reader);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
=> writer.WriteStringValue(value);

private static string GetRawValue(Utf8JsonReader reader)
{
Expand Down