Updated code to correct build errors for Jellyfin.Extensions
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a number to a boolean.
|
||||
/// This is needed for HDHomerun.
|
||||
@@ -28,6 +28,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
writer.WriteBooleanValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,36 +2,40 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a string to a boolean.
|
||||
/// This is needed for FFprobe.
|
||||
/// </summary>
|
||||
public class JsonBoolStringConverter : JsonConverter<bool>
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a string to a boolean.
|
||||
/// This is needed for FFprobe.
|
||||
/// </summary>
|
||||
public class JsonBoolStringConverter : JsonConverter<bool>
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
/// <inheritdoc />
|
||||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
ReadOnlySpan<byte> utf8Span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
||||
if (Utf8Parser.TryParse(utf8Span, out bool val, out _, 'l'))
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
return val;
|
||||
ReadOnlySpan<byte> utf8Span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
|
||||
if (Utf8Parser.TryParse(utf8Span, out bool val, out _, 'l'))
|
||||
{
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
return reader.GetBoolean();
|
||||
}
|
||||
|
||||
return reader.GetBoolean();
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
writer.WriteBooleanValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
|
||||
=> writer.WriteBooleanValue(value);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonCommaDelimitedCollectionConverter{T}"/> class.
|
||||
/// </summary>
|
||||
public JsonCommaDelimitedCollectionConverter() : base()
|
||||
public JsonCommaDelimitedCollectionConverter()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -2,13 +2,13 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Json comma delimited collection converter factory.
|
||||
/// </summary>
|
||||
@@ -20,6 +20,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
return typeToConvert.IsArray
|
||||
|| (typeToConvert.IsGenericType
|
||||
&& (typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyCollection<>)) || typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyList<>))));
|
||||
@@ -28,6 +29,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
|
||||
return (JsonConverter?)Activator.CreateInstance(typeof(JsonCommaDelimitedCollectionConverter<>).MakeGenericType(structType));
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Legacy DateTime converter.
|
||||
/// Milliseconds aren't output if zero by default.
|
||||
@@ -24,6 +24,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
if (value.Millisecond == 0)
|
||||
{
|
||||
// Remaining ticks value will be 0, manually format.
|
||||
|
||||
@@ -2,52 +2,56 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable SA1309 // Variables should not begin with underscore
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Json unknown enum converter.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
public class JsonDefaultStringEnumConverter<T> : JsonConverter<T>
|
||||
where T : struct, Enum
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
private readonly JsonConverter<T> _baseConverter;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonDefaultStringEnumConverter{T}"/> class.
|
||||
/// Json unknown enum converter.
|
||||
/// </summary>
|
||||
/// <param name="baseConverter">The base json converter.</param>
|
||||
public JsonDefaultStringEnumConverter(JsonConverter<T> baseConverter)
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
public class JsonDefaultStringEnumConverter<T> : JsonConverter<T>
|
||||
where T : struct, Enum
|
||||
{
|
||||
_baseConverter = baseConverter;
|
||||
}
|
||||
private readonly JsonConverter<T> _baseConverter;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNull() || reader.IsEmptyString())
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonDefaultStringEnumConverter{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="baseConverter">The base json converter.</param>
|
||||
public JsonDefaultStringEnumConverter(JsonConverter<T> baseConverter)
|
||||
{
|
||||
var customValueAttribute = typeToConvert.GetCustomAttribute<DefaultValueAttribute>();
|
||||
if (customValueAttribute?.Value is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Default value not set for '{typeToConvert.Name}'");
|
||||
}
|
||||
|
||||
return (T)customValueAttribute.Value;
|
||||
this._baseConverter = baseConverter;
|
||||
}
|
||||
|
||||
return _baseConverter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
if (reader.IsNull() || reader.IsEmptyString())
|
||||
{
|
||||
var customValueAttribute = typeToConvert.GetCustomAttribute<DefaultValueAttribute>();
|
||||
if (customValueAttribute?.Value is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Default value not set for '{typeToConvert.Name}'");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
_baseConverter.Write(writer, value, options);
|
||||
return (T)customValueAttribute.Value;
|
||||
}
|
||||
|
||||
return this._baseConverter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
this._baseConverter.Write(writer, value, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,34 +2,37 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable SA1309 // Variables should not begin with underscore
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Utilizes the JsonStringEnumConverter and sets a default value if not provided.
|
||||
/// </summary>
|
||||
public class JsonDefaultStringEnumConverterFactory : JsonConverterFactory
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
private static readonly JsonStringEnumConverter _baseConverterFactory = new();
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
/// <summary>
|
||||
/// Utilizes the JsonStringEnumConverter and sets a default value if not provided.
|
||||
/// </summary>
|
||||
public class JsonDefaultStringEnumConverterFactory : JsonConverterFactory
|
||||
{
|
||||
return _baseConverterFactory.CanConvert(typeToConvert)
|
||||
&& typeToConvert.IsDefined(typeof(DefaultValueAttribute));
|
||||
}
|
||||
private static readonly JsonStringEnumConverter _baseConverterFactory = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var baseConverter = _baseConverterFactory.CreateConverter(typeToConvert, options);
|
||||
var converterType = typeof(JsonDefaultStringEnumConverter<>).MakeGenericType(typeToConvert);
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return _baseConverterFactory.CanConvert(typeToConvert)
|
||||
&& typeToConvert.IsDefined(typeof(DefaultValueAttribute));
|
||||
}
|
||||
|
||||
return (JsonConverter?)Activator.CreateInstance(converterType, baseConverter);
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var baseConverter = _baseConverterFactory.CreateConverter(typeToConvert, options);
|
||||
var converterType = typeof(JsonDefaultStringEnumConverter<>).MakeGenericType(typeToConvert);
|
||||
|
||||
return (JsonConverter?)Activator.CreateInstance(converterType, baseConverter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable SA1309 // Variables should not begin with underscore
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Convert delimited string to array of type.
|
||||
/// </summary>
|
||||
@@ -23,7 +25,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// </summary>
|
||||
protected JsonDelimitedCollectionConverter()
|
||||
{
|
||||
_typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
||||
this._typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,7 +39,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
// null got handled higher up the call stack
|
||||
var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
|
||||
var stringEntries = reader.GetString()!.Split(this.Delimiter, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (stringEntries.Length == 0)
|
||||
{
|
||||
return [];
|
||||
@@ -48,7 +50,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
try
|
||||
{
|
||||
var parsedValue = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim());
|
||||
var parsedValue = this._typeConverter.ConvertFromInvariantString(stringEntries[i].Trim());
|
||||
if (parsedValue is not null)
|
||||
{
|
||||
typedValues.Add((T)parsedValue);
|
||||
@@ -60,6 +62,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
}
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
if (typeToConvert.IsArray)
|
||||
{
|
||||
return typedValues.ToArray();
|
||||
|
||||
@@ -2,39 +2,43 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable SA1309 // Variables should not begin with underscore
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Enum flag to json array converter.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
public class JsonFlagEnumConverter<T> : JsonConverter<T>
|
||||
where T : struct, Enum
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
private static readonly T[] _enumValues = Enum.GetValues<T>();
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
/// <summary>
|
||||
/// Enum flag to json array converter.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
public class JsonFlagEnumConverter<T> : JsonConverter<T>
|
||||
where T : struct, Enum
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
private static readonly T[] _enumValues = Enum.GetValues<T>();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
foreach (var enumValue in _enumValues)
|
||||
/// <inheritdoc />
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.HasFlag(enumValue))
|
||||
{
|
||||
writer.WriteStringValue(enumValue.ToString());
|
||||
}
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
writer.WriteStartArray();
|
||||
foreach (var enumValue in _enumValues)
|
||||
{
|
||||
if (value.HasFlag(enumValue))
|
||||
{
|
||||
writer.WriteStringValue(enumValue.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,29 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Json flag enum converter factory.
|
||||
/// </summary>
|
||||
public class JsonFlagEnumConverterFactory : JsonConverterFactory
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return typeToConvert.IsEnum && typeToConvert.IsDefined(typeof(FlagsAttribute));
|
||||
}
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
/// <summary>
|
||||
/// Json flag enum converter factory.
|
||||
/// </summary>
|
||||
public class JsonFlagEnumConverterFactory : JsonConverterFactory
|
||||
{
|
||||
return (JsonConverter?)Activator.CreateInstance(typeof(JsonFlagEnumConverter<>).MakeGenericType(typeToConvert));
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
return typeToConvert.IsEnum && typeToConvert.IsDefined(typeof(FlagsAttribute));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return (JsonConverter?)Activator.CreateInstance(typeof(JsonFlagEnumConverter<>).MakeGenericType(typeToConvert));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
#pragma warning disable SA1600 // Elements should be documented
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a GUID object or value to/from JSON.
|
||||
/// </summary>
|
||||
@@ -20,14 +22,17 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
? Guid.Empty
|
||||
: ReadInternal(ref reader);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
WriteInternal(writer, value);
|
||||
}
|
||||
|
||||
// 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)
|
||||
=> WriteInternal(writer, value);
|
||||
|
||||
internal static void WriteInternal(Utf8JsonWriter writer, Guid value)
|
||||
=> writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a GUID object or value to/from JSON.
|
||||
/// </summary>
|
||||
@@ -22,6 +22,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
// null got handled higher up the call stack
|
||||
var val = value!.Value;
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
if (val.IsEmpty())
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a nullable struct or value to/from JSON.
|
||||
/// Required - some clients send an empty string.
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Json nullable struct converter factory.
|
||||
/// </summary>
|
||||
@@ -16,6 +16,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
return typeToConvert.IsGenericType
|
||||
&& typeToConvert.GetGenericTypeDefinition() == typeof(Nullable<>)
|
||||
&& typeToConvert.GenericTypeArguments[0].IsValueType;
|
||||
@@ -24,6 +25,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
var structType = typeToConvert.GenericTypeArguments[0];
|
||||
return (JsonConverter?)Activator.CreateInstance(typeof(JsonNullableStructConverter<>).MakeGenericType(structType));
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonPipeDelimitedCollectionConverter{T}"/> class.
|
||||
/// </summary>
|
||||
public JsonPipeDelimitedCollectionConverter() : base()
|
||||
public JsonPipeDelimitedCollectionConverter()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -2,13 +2,13 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Json Pipe delimited collection converter factory.
|
||||
/// </summary>
|
||||
@@ -20,6 +20,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
return typeToConvert.IsArray
|
||||
|| (typeToConvert.IsGenericType
|
||||
&& (typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyCollection<>)) || typeToConvert.GetGenericTypeDefinition().IsAssignableFrom(typeof(IReadOnlyList<>))));
|
||||
@@ -28,6 +29,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(typeToConvert);
|
||||
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
|
||||
return (JsonConverter?)Activator.CreateInstance(typeof(JsonPipeDelimitedCollectionConverter<>).MakeGenericType(structType));
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converter to allow the serializer to read strings.
|
||||
/// </summary>
|
||||
@@ -21,7 +21,10 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value);
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
|
||||
private static string GetRawValue(Utf8JsonReader reader)
|
||||
{
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Version object or value to/from JSON.
|
||||
/// </summary>
|
||||
@@ -22,6 +22,11 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString());
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(writer);
|
||||
ArgumentNullException.ThrowIfNull(value);
|
||||
|
||||
writer.WriteStringValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using Jellyfin.Extensions.Json.Converters;
|
||||
#pragma warning disable SA1309 // Variables should not begin with underscore
|
||||
|
||||
namespace Jellyfin.Extensions.Json
|
||||
{
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Helper class for having compatible JSON throughout the codebase.
|
||||
/// </summary>
|
||||
@@ -46,19 +48,19 @@ namespace Jellyfin.Extensions.Json
|
||||
new JsonStringEnumConverter(),
|
||||
new JsonNullableStructConverterFactory(),
|
||||
new JsonDateTimeConverter(),
|
||||
new JsonStringConverter()
|
||||
new JsonStringConverter(),
|
||||
},
|
||||
TypeInfoResolver = new DefaultJsonTypeInfoResolver()
|
||||
TypeInfoResolver = new DefaultJsonTypeInfoResolver(),
|
||||
};
|
||||
|
||||
private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new(_jsonSerializerOptions)
|
||||
{
|
||||
PropertyNamingPolicy = null
|
||||
PropertyNamingPolicy = null,
|
||||
};
|
||||
|
||||
private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new(_jsonSerializerOptions)
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,30 +2,31 @@
|
||||
// Copyright (c) PlaceholderCompany. All rights reserved.
|
||||
// </copyright>
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Jellyfin.Extensions.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for Utf8JsonReader and Utf8JsonWriter.
|
||||
/// </summary>
|
||||
public static class Utf8JsonExtensions
|
||||
namespace Jellyfin.Extensions.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if the reader contains an empty string.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains an empty string.</returns>
|
||||
public static bool IsEmptyString(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.String
|
||||
&& ((reader.HasValueSequence && reader.ValueSequence.IsEmpty)
|
||||
|| (!reader.HasValueSequence && reader.ValueSpan.IsEmpty));
|
||||
using System.Text.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the reader contains a null value.
|
||||
/// Extensions for Utf8JsonReader and Utf8JsonWriter.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains null.</returns>
|
||||
public static bool IsNull(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.Null;
|
||||
public static class Utf8JsonExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if the reader contains an empty string.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains an empty string.</returns>
|
||||
public static bool IsEmptyString(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.String
|
||||
&& ((reader.HasValueSequence && reader.ValueSequence.IsEmpty)
|
||||
|| (!reader.HasValueSequence && reader.ValueSpan.IsEmpty));
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the reader contains a null value.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains null.</returns>
|
||||
public static bool IsNull(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.Null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user