aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
diff options
context:
space:
mode:
authorcrobibero <cody@robibe.ro>2020-06-04 15:16:04 -0600
committercrobibero <cody@robibe.ro>2020-06-04 15:16:04 -0600
commita787efc66027b678cb09e83002ce3f9ba00e206d (patch)
treecc460c9a764461158c548eb72332d82d3c1d7280 /MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
parent341b947cdecdfc791c1bc3e72da1e68cd3754c3a (diff)
parent601e4a88c94ea0ccaab9f3c148d4bbdca2e532ee (diff)
Merge remote-tracking branch 'upstream/api-migration' into api-scheduled-tasks
Diffstat (limited to 'MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs')
-rw-r--r--MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs60
1 files changed, 60 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs b/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
new file mode 100644
index 000000000..d9795a189
--- /dev/null
+++ b/MediaBrowser.Common/Json/Converters/JsonNonStringKeyDictionaryConverterFactory.cs
@@ -0,0 +1,60 @@
+#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;
+ }
+ }
+}