Files
Umbraco-CMS/src/Umbraco.Infrastructure/Serialization/CaseInsensitiveDictionaryConverter.cs
Mole 2dcdff5392 V14: Migrate nucache to use System.Text.Json (#15685)
* Create system text serializer

* Assign property names with system text

* Use the new serializer

* Impement AutoInterningStringConverter with System.Text.Json

* Implement TextAutoInterningStringKeyCaseInsensitiveDictionaryConverter

* Make CaseInsensitiveDictionaryConverter

* Force datetimes to be read as UTC

* Remove usages of Newtonsoft.Json

* Remove text prefixes

* Remove unused Newtonsoft converter

* Remove more newtonsoft

* Remove duplicate implementation

* Rmove usage of missing class in tests

* Ignore null values

* Fix tests

* Remove Newtonstoft reference from NuCache

---------

Co-authored-by: Elitsa <elm@umbraco.dk>
2024-02-14 12:10:45 +01:00

25 lines
1.2 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace Umbraco.Cms.Infrastructure.Serialization;
public class CaseInsensitiveDictionaryConverter<TValue> : JsonConverter<IDictionary<string, TValue>>
{
// This is a hacky workaround to creating a "read only converter", since System.Text.Json doesn't support it.
// Taken from https://github.com/dotnet/runtime/issues/46372#issuecomment-1660515178
private readonly JsonConverter<IDictionary<string, TValue>> _fallbackConverter = (JsonConverter<IDictionary<string, TValue>>)JsonSerializerOptions.Default.GetConverter(typeof(IDictionary<string, TValue>));
public override IDictionary<string, TValue>? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
IDictionary<string, TValue>? defaultDictionary = JsonSerializer.Deserialize<IDictionary<string, TValue>>(ref reader, options);
return defaultDictionary is null
? null
: new Dictionary<string, TValue>(defaultDictionary, StringComparer.OrdinalIgnoreCase);
}
public override void Write(Utf8JsonWriter writer, IDictionary<string, TValue> value, JsonSerializerOptions options) => _fallbackConverter.Write(writer, value, options);
}