-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJsonNodeSummary.cs
136 lines (111 loc) · 3.14 KB
/
JsonNodeSummary.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// .Net6 Console Template
string jsonString =
@"
{
""Date"": ""2019-08-01T00:00:00"",
""Temperature"": 25,
""Summary"": ""Hot"",
""DatesAvailable"": [
""2019-08-01T00:00:00"",
""2019-08-02T00:00:00""
],
""TemperatureRanges"": {
""Cold"": {
""High"": 20,
""Low"": -10
},
""Hot"": {
""High"": 60,
""Low"": 20
}
}
}
";
var options = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
};
JsonNode? jsonNode =
await Task.Run<JsonNode?>( () => JsonNode.Parse(jsonString));
// Console.WriteLine(jsonNode?.ToJsonString(options));
// JsonNode is mutable!
jsonNode!["TemperatureRanges"] = new JsonObject()
{
["Chill"] = new JsonObject()
{
["High"] = 22,
["Low"] = 5
}
};
JsonNode? temperature = jsonNode?["TemperatureRanges"];
// Console.WriteLine(temperature?.ToJsonString());
JsonNode? datesAvalilable = jsonNode?["DatesAvailable"];
JsonNode? firstElement =
datesAvalilable!.GetType() == typeof(JsonArray) ? datesAvalilable?[0] : default(JsonNode) ;
// Console.WriteLine(firstElement?.GetValue<DateTime>());
var jsonNodeOptions = new JsonNodeOptions()
{
PropertyNameCaseInsensitive = true
};
var jObject = new JsonObject(jsonNodeOptions)
{
["Date"] = new DateTime(2022, 1, 1),
["RoomTemperature"] = 21,
["Summary"] = "Moist",
["DatesAvailable"] =
new JsonArray(new DateTime(2019, 5, 5), new DateTime(2020, 4, 4)),
["TemperatureRanges"] = new JsonObject
{
["Cold"] = new JsonObject
{
["High"] = 20,
["Low"] = -10
},
["Warm"] = new JsonObject
{
["High"] = 40,
["Low"] = 20
}
},
["Location"] = new JsonObject
{
["HighAltitude"] = new JsonArray(435,22,11),
["LowAltitude"] = new JsonArray(322,32,11)
},
["KeyWords"] = new JsonArray("Cool", "Windy", "Moist")
};
// add a new value to TemperatureRanges
jObject["TemperatureRanges"]!["HeatWave"] = new JsonObject
{
["High"] = 100,
["Low"] = 40
};
jObject.Remove("Date");
//Console.WriteLine(jObject.ToJsonString(options));
await using (var stream = new MemoryStream())
{
await using (var jsonWriter = new Utf8JsonWriter(stream))
{
JsonObject? tempRangesObject = jObject["TemperatureRanges"]?.AsObject();
// write a small portion of jObject to stream
tempRangesObject?.WriteTo(jsonWriter);
await jsonWriter.FlushAsync();
stream.Seek(0, SeekOrigin.Begin);
TemperatureRanges temperatureRanges =
(await JsonSerializer.
DeserializeAsync<TemperatureRanges>(stream))!;
Console.WriteLine($"Hot.High = {temperatureRanges["Warm"].High}");
JsonArray? jArray = jObject["DatesAvailable"]?.AsArray();
Console.WriteLine($"First Date = {jArray?[0]?.GetValue<DateTime>()}");
}
}
public class TemperatureRanges : Dictionary<string, HighLowTemps>
{
}
public class HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
}