-
Notifications
You must be signed in to change notification settings - Fork 122
/
NameToList.cs
52 lines (47 loc) · 1.88 KB
/
NameToList.cs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace IconGenerator.Converters
{
public class DictinaryListConverter<T> : JsonConverter<IEnumerable<T>>
{
public override IEnumerable<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var result = new List<T>();
_ = JsonDocument.TryParseValue(ref reader, out JsonDocument doc);
foreach (JsonProperty property in doc.RootElement.EnumerateObject())
{
var name = property.Name;
var newItem = JsonSerializer.Deserialize<T>(property.Value.GetRawText(), options);
result.Add(newItem);
}
return result;
}
public override void Write(Utf8JsonWriter writer, IEnumerable<T> value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class StringDictinaryConverter<T> : JsonConverter<Dictionary<string, T>>
{
public override Dictionary<string, T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var dictionary = new Dictionary<string, T>();
_ = JsonDocument.TryParseValue(ref reader, out JsonDocument doc);
foreach (JsonProperty property in doc.RootElement.EnumerateObject())
{
var name = property.Name;
dictionary.Add(name, JsonSerializer.Deserialize<T>(property.Value.GetRawText(), options));
}
return dictionary;
}
public override void Write(Utf8JsonWriter writer, Dictionary<string, T> value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}