-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCaseInsensitiveDictionaryConverter.cs
More file actions
33 lines (29 loc) · 1.03 KB
/
CaseInsensitiveDictionaryConverter.cs
File metadata and controls
33 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace RunScript.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
internal class CaseInsensitiveDictionaryConverter<TValue> : JsonConverter<Dictionary<string, TValue>>
{
public override Dictionary<string, TValue> Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
var dict = (Dictionary<string, TValue>)JsonSerializer
.Deserialize(ref reader, typeToConvert, options)!;
#pragma warning disable CA1308 // Normalize strings to uppercase
return dict.ToDictionary(
i => i.Key.ToLowerInvariant(),
i => i.Value,
StringComparer.OrdinalIgnoreCase);
#pragma warning restore CA1308 // Normalize strings to uppercase
}
public override void Write(
Utf8JsonWriter writer,
Dictionary<string, TValue> value,
JsonSerializerOptions options)
=> JsonSerializer.Serialize(
writer,
value,
value.GetType(),
options);
}