aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Json/Converters
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common/Json/Converters')
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs56
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs40
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonInt64Converter.cs56
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverter.cs82
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs59
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs44
6 files changed, 44 insertions, 293 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs b/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs
deleted file mode 100644
index 56c0ecbe9..000000000
--- a/MediaBrowser.Common/Json/Converters/JsonDoubleConverter.cs
+++ /dev/null
@@ -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>
- /// Double to String JSON converter.
- /// Web client send quoted doubles.
- /// </summary>
- public class JsonDoubleConverter : JsonConverter<double>
- {
- /// <summary>
- /// Read JSON string as double.
- /// </summary>
- /// <param name="reader"><see cref="Utf8JsonReader"/>.</param>
- /// <param name="typeToConvert">Type.</param>
- /// <param name="options">Options.</param>
- /// <returns>Parsed value.</returns>
- public override double Read(ref Utf8JsonReader reader, Type typeToConvert, 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 double 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 (double.TryParse(reader.GetString(), out number))
- {
- return number;
- }
- }
-
- // fallback to default handling
- return reader.GetDouble();
- }
-
- /// <summary>
- /// Write double to JSON string.
- /// </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, double value, JsonSerializerOptions options)
- {
- writer.WriteNumberValue(value);
- }
- }
-}
diff --git a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs b/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
deleted file mode 100644
index 70c375b8c..000000000
--- a/MediaBrowser.Common/Json/Converters/JsonInt32Converter.cs
+++ /dev/null
@@ -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 GUID 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);
- }
- }
-}
diff --git a/MediaBrowser.Common/Json/Converters/JsonInt64Converter.cs b/MediaBrowser.Common/Json/Converters/JsonInt64Converter.cs
deleted file mode 100644
index d18fd95d5..000000000
--- a/MediaBrowser.Common/Json/Converters/JsonInt64Converter.cs
+++ /dev/null
@@ -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>
- /// Long to String JSON converter.
- /// 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 string.
- /// </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.WriteStringValue(value.ToString(NumberFormatInfo.InvariantInfo));
- }
- }
-}
diff --git a/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverter.cs b/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverter.cs
deleted file mode 100644
index 8053461f0..000000000
--- a/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverter.cs
+++ /dev/null
@@ -1,82 +0,0 @@
-#nullable enable
-
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Globalization;
-using System.Reflection;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-
-namespace MediaBrowser.Common.Json.Converters
-{
- /// <summary>
- /// Converter for Dictionaries without string key.
- /// TODO This can be removed when System.Text.Json supports Dictionaries with non-string keys.
- /// </summary>
- /// <typeparam name="TKey">Type of key.</typeparam>
- /// <typeparam name="TValue">Type of value.</typeparam>
- internal sealed class JsonNonStringKeyDictionaryConverter<TKey, TValue> : JsonConverter<IDictionary<TKey, TValue>>
- {
- /// <summary>
- /// Read JSON.
- /// </summary>
- /// <param name="reader">The Utf8JsonReader.</param>
- /// <param name="typeToConvert">The type to convert.</param>
- /// <param name="options">The json serializer options.</param>
- /// <returns>Typed dictionary.</returns>
- /// <exception cref="NotSupportedException">Dictionary key type not supported.</exception>
- public override IDictionary<TKey, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- {
- var convertedType = typeof(Dictionary<,>).MakeGenericType(typeof(string), typeToConvert.GenericTypeArguments[1]);
- var value = JsonSerializer.Deserialize(ref reader, convertedType, options);
- var instance = (Dictionary<TKey, TValue>)Activator.CreateInstance(
- typeToConvert,
- BindingFlags.Instance | BindingFlags.Public,
- null,
- null,
- CultureInfo.CurrentCulture);
- var enumerator = (IEnumerator)convertedType.GetMethod("GetEnumerator")!.Invoke(value, null);
- var parse = typeof(TKey).GetMethod(
- "Parse",
- 0,
- BindingFlags.Public | BindingFlags.Static,
- null,
- CallingConventions.Any,
- new[] { typeof(string) },
- null);
- if (parse == null)
- {
- throw new NotSupportedException($"{typeof(TKey)} as TKey in IDictionary<TKey, TValue> is not supported.");
- }
-
- while (enumerator.MoveNext())
- {
- var element = (KeyValuePair<string?, TValue>)enumerator.Current;
- instance.Add((TKey)parse.Invoke(null, new[] { (object?)element.Key }), element.Value);
- }
-
- return instance;
- }
-
- /// <summary>
- /// Write dictionary as Json.
- /// </summary>
- /// <param name="writer">The Utf8JsonWriter.</param>
- /// <param name="value">The dictionary value.</param>
- /// <param name="options">The Json serializer options.</param>
- public override void Write(Utf8JsonWriter writer, IDictionary<TKey, TValue> value, JsonSerializerOptions options)
- {
- var convertedDictionary = new Dictionary<string?, TValue>(value.Count);
- foreach (var (k, v) in value)
- {
- if (k != null)
- {
- convertedDictionary[k.ToString()] = v;
- }
- }
-
- JsonSerializer.Serialize(writer, convertedDictionary, options);
- }
- }
-}
diff --git a/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs b/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
deleted file mode 100644
index 52f360740..000000000
--- a/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-#nullable enable
-
-using System;
-using System.Collections;
-using System.Globalization;
-using System.Reflection;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-
-namespace MediaBrowser.Common.Json.Converters
-{
- /// <summary>
- /// https://github.com/dotnet/runtime/issues/30524#issuecomment-524619972.
- /// TODO This can be removed when System.Text.Json supports Dictionaries with non-string keys.
- /// </summary>
- internal sealed class JsonNonStringKeyDictionaryConverterFactory : JsonConverterFactory
- {
- /// <summary>
- /// Only convert objects that implement IDictionary and do not have string keys.
- /// </summary>
- /// <param name="typeToConvert">Type convert.</param>
- /// <returns>Conversion ability.</returns>
- public override bool CanConvert(Type typeToConvert)
- {
- if (!typeToConvert.IsGenericType)
- {
- return false;
- }
-
- // Let built in converter handle string keys
- if (typeToConvert.GenericTypeArguments[0] == typeof(string))
- {
- return false;
- }
-
- // Only support objects that implement IDictionary
- return typeToConvert.GetInterface(nameof(IDictionary)) != null;
- }
-
- /// <summary>
- /// Create converter for generic dictionary type.
- /// </summary>
- /// <param name="typeToConvert">Type to convert.</param>
- /// <param name="options">Json serializer options.</param>
- /// <returns>JsonConverter for given type.</returns>
- public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
- {
- var converterType = typeof(JsonNonStringKeyDictionaryConverter<,>)
- .MakeGenericType(typeToConvert.GenericTypeArguments[0], typeToConvert.GenericTypeArguments[1]);
- var converter = (JsonConverter)Activator.CreateInstance(
- converterType,
- BindingFlags.Instance | BindingFlags.Public,
- null,
- null,
- CultureInfo.CurrentCulture);
- return converter;
- }
- }
-}
diff --git a/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs
new file mode 100644
index 000000000..cffc41ba3
--- /dev/null
+++ b/MediaBrowser.Common/Json/Converters/JsonNullableStructConverter.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace MediaBrowser.Common.Json.Converters
+{
+ /// <summary>
+ /// Converts a nullable struct or value to/from JSON.
+ /// Required - some clients send an empty string.
+ /// </summary>
+ /// <typeparam name="T">The struct type.</typeparam>
+ public class JsonNullableStructConverter<T> : JsonConverter<T?>
+ where T : struct
+ {
+ private readonly JsonConverter<T?> _baseJsonConverter;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="JsonNullableStructConverter{T}"/> class.
+ /// </summary>
+ /// <param name="baseJsonConverter">The base json converter.</param>
+ public JsonNullableStructConverter(JsonConverter<T?> baseJsonConverter)
+ {
+ _baseJsonConverter = baseJsonConverter;
+ }
+
+ /// <inheritdoc />
+ public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ // Handle empty string.
+ 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, T? value, JsonSerializerOptions options)
+ {
+ _baseJsonConverter.Write(writer, value, options);
+ }
+ }
+}