-
Notifications
You must be signed in to change notification settings - Fork 18
/
SystemTextJson.cs
121 lines (107 loc) · 3.67 KB
/
SystemTextJson.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
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace Serialization;
public class SystemTextJson
{
public static void Use()
{
var model = new SimpleData
{
Id = 42,
Names = new[] { "Bell", "Stacey", "her", "Jane" },
Location = new NestedData
{
LocationName = "London",
Latitude = 51.503209,
Longitude = -0.119145
},
Map = new Dictionary<string, int>
{
{ "Answer", 42 },
{ "FirstPrime", 2 }
}
};
string json = JsonSerializer.Serialize(
model,
new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
var deserialized = JsonSerializer.Deserialize<SimpleData>(json);
if (deserialized is not null)
{
Console.WriteLine(deserialized?.Id);
}
using (JsonDocument document = JsonDocument.Parse(json))
{
foreach (JsonProperty property in document.RootElement.EnumerateObject())
{
Console.WriteLine($"Property: {property.Name} ({property.Value.ValueKind})");
}
}
using (JsonDocument document = JsonDocument.Parse(json))
{
JsonElement namesElement = document.RootElement.GetProperty("names");
foreach (JsonElement name in namesElement.EnumerateArray())
{
Console.WriteLine($"Name: {name.GetString()}");
}
JsonElement root = document.RootElement;
if (root.TryGetProperty("location", out JsonElement locationElement))
{
JsonElement nameElement = locationElement.GetProperty("locationName");
JsonElement latitudeElement = locationElement.GetProperty("latitude");
JsonElement longitudeElement = locationElement.GetProperty("longitude");
string locationName = nameElement.GetString()!;
double latitude = latitudeElement.GetDouble();
double longitude = longitudeElement.GetDouble();
Console.WriteLine($"Location: {locationName}: {latitude},{longitude}");
}
}
JsonNode rootNode = JsonNode.Parse(json)!;
JsonNode mapNode = rootNode["map"]!;
mapNode["iceCream"] = 99;
}
public static string AutoCamelCase(SimpleData model)
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
WriteIndented = true
};
string json = JsonSerializer.Serialize(
model,
options);
return json;
}
public static string SerializeWithCircularReferences()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
WriteIndented = true,
ReferenceHandler = ReferenceHandler.Preserve
};
var circle = new SelfRef
{
Name = "Top",
Next = new SelfRef
{
Name = "Bottom",
}
};
circle.Next.Next = circle;
string json = JsonSerializer.Serialize(circle, options);
return json;
}
public class SimpleData
{
public int Id { get; set; }
public IList<string>? Names { get; set; }
public NestedData? Location { get; set; }
public IDictionary<string, int>? Map { get; set; }
}
public class NestedData
{
public string? LocationName { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
}