-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathProgram.cs
183 lines (168 loc) · 5.19 KB
/
Program.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
Category appetizers = new("Appetizers");
appetizers.Items.Add(new Item("Dungeness Crab Cocktail", "Classic cocktail sauce", 27M));
appetizers.Items.Add(new Item("Almond Crusted Scallops", "Almonds, Parmesan, chive beurre blanc", 19M));
Category dinner = new("Dinner");
dinner.Items.Add(new Item("Grilled King Salmon", "Lemon chive buerre blanc", 49M));
Card card = new("The Restaurant");
card.Categories.Add(appetizers);
card.Categories.Add(dinner);
string json = SerializeJson(card);
DeserializeJson(json);
UseDom(json);
WriteDom();
UseReader(json);
UseWriter();
string SerializeJson(Card card)
{
Console.WriteLine(nameof(SerializeJson));
JsonSerializerOptions options = new()
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
// ReferenceHandler = ReferenceHandler.Preserve
};
string json = JsonSerializer.Serialize(card, options);
Console.WriteLine(json);
Console.WriteLine();
return json;
}
void DeserializeJson(string json)
{
Console.WriteLine(nameof(DeserializeJson));
JsonSerializerOptions options = new()
{
PropertyNameCaseInsensitive = true
};
Card? card = JsonSerializer.Deserialize<Card>(json, options);
if (card is null)
{
Console.WriteLine("no card deserialized");
return;
}
Console.WriteLine($"{card.Title}");
foreach (var category in card.Categories)
{
Console.WriteLine($"\t{category.Title}");
foreach (var item in category.Items)
{
Console.WriteLine($"\t\t{item.Title}");
}
}
Console.WriteLine();
}
void UseDom(string json)
{
Console.WriteLine(nameof(UseDom));
using JsonDocument document = JsonDocument.Parse(json);
JsonElement titleElement = document.RootElement.GetProperty("title");
Console.WriteLine(titleElement);
foreach (JsonElement category in document.RootElement.GetProperty("categories").EnumerateArray())
{
foreach (JsonElement item in category.GetProperty("items").EnumerateArray())
{
foreach (JsonProperty property in item.EnumerateObject())
{
Console.WriteLine($"{property.Name} {property.Value}");
}
Console.WriteLine($"{item.GetProperty("title")}");
}
}
Console.WriteLine();
}
void UseReader(string json)
{
Console.WriteLine(nameof(UseReader));
bool isNextPrice = false;
bool isNextTitle = false;
string? title = default;
byte[] data = Encoding.UTF8.GetBytes(json);
Utf8JsonReader reader = new(data);
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "title")
{
isNextTitle = true;
}
if (reader.TokenType == JsonTokenType.String && isNextTitle)
{
title = reader.GetString();
isNextTitle = false;
}
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "price")
{
isNextPrice = true;
}
if (reader.TokenType == JsonTokenType.Number && isNextPrice && reader.TryGetDecimal(out decimal price))
{
Console.WriteLine($"{title}, price: {price:C}");
isNextPrice = false;
}
}
Console.WriteLine();
}
// new with .NET 6
void WriteDom()
{
Console.WriteLine(nameof(WriteDom));
JsonObject json = new()
{
["Books"] = new JsonArray()
{
new JsonObject()
{
["Title"] = "Professional C# and .NET",
["Subtitle"] = "2021 Edition"
},
new JsonObject()
{
["Title"] = "Professional C# 7 and .NET Core 2",
["Subtitle"] = "2018 Edition"
}
}
};
Console.WriteLine(json);
Console.WriteLine();
}
void UseWriter()
{
Console.WriteLine(nameof(UseWriter));
using MemoryStream stream = new();
JsonWriterOptions options = new()
{
Indented = true
};
using (Utf8JsonWriter writer = new(stream, options))
{
writer.WriteStartArray();
writer.WriteStartObject();
writer.WriteStartObject("Book");
writer.WriteString("Title", "Professional C# and .NET");
writer.WriteString("Subtitle", "2021 Edition");
writer.WriteEndObject();
writer.WriteEndObject();
writer.WriteStartObject();
writer.WriteStartObject("Book");
writer.WriteString("Title", "Professional C# 7 and .NET Core 2");
writer.WriteString("Subtitle", "2018 Edition");
writer.WriteEndObject();
writer.WriteEndObject();
writer.WriteEndArray();
}
string json = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(json);
Console.WriteLine();
}
public record Item(string Title, string Text, decimal Price);
public record Category(string Title)
{
public IList<Item> Items { get; init; } = new List<Item>();
}
public record Card(string Title)
{
public IList<Category> Categories { get; init; } = new List<Category>();
}