using System.Text.Json; namespace Umbraco.Cms.Infrastructure.Serialization; /// /// Converts a dictionary with a string key to or from JSON, using the comparer and interning the string key when reading. /// /// The type of the dictionary value. public sealed class JsonDictionaryStringInternIgnoreCaseConverter : ReadOnlyJsonConverter> { /// public override Dictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException(); } var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { return dictionary; } // Get key if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException(); } string propertyName = reader.GetString() ?? throw new JsonException(); // Get value reader.Read(); TValue? value = JsonSerializer.Deserialize(ref reader, options); if (value is not null) { dictionary[string.Intern(propertyName)] = value; } } throw new JsonException(); } }