-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
JsonTests.cs
209 lines (187 loc) · 5.29 KB
/
JsonTests.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
public class JsonTests
{
[Fact]
public Task JObjectOrdering1()
{
var obj = new JObject(
new JProperty("@xmlns", 2),
new JProperty("#text", 1)
);
return Verify(obj);
}
[Fact]
public Task JTokenIgnore()
{
var jToken = JToken.Parse(
"""
{
Include: 1,
Ignore: 2,
"Memory Info": {
fragmentedBytes: 208,
heapSizeBytes: 2479536,
highMemoryLoadThresholdBytes: 30821986713,
memoryLoadBytes: 14041127280,
totalAvailableMemoryBytes: 34246651904
}
}
""");
return Verify(jToken)
.IgnoreMembers("Ignore", "Memory Info");
}
[Fact]
public Task JTokenScrub()
{
var jToken = JToken.Parse(
"""
{
Include: 1,
Scrub: 2,
"Memory Info": {
fragmentedBytes: 208,
heapSizeBytes: 2479536,
highMemoryLoadThresholdBytes: 30821986713,
memoryLoadBytes: 14041127280,
totalAvailableMemoryBytes: 34246651904
}
}
""");
return Verify(jToken)
.ScrubMembers("Scrub", "Memory Info");
}
[Fact]
public Task JObjectIgnore()
{
var obj = new JObject(
new JProperty("Include", 2),
new JProperty("Ignore", 1)
);
return Verify(obj)
.IgnoreMember("Ignore");
}
[Fact]
public Task JObjectOrdering2()
{
var obj = new JObject(
new JProperty("#text", 1),
new JProperty("@xmlns", 2)
);
return Verify(obj);
}
[Fact]
public Task IgnoreJTokenByName()
{
var json = """
{
'short': {
'key': {
'code': 0,
'msg': 'No action taken'
},
'Ignore1': {
'code': 2,
'msg': 'ignore this'
}
}
}
""";
var target = JToken.Parse(json);
return Verify(target)
.IgnoreMember("Ignore1");
}
[Fact]
public Task ScrubJTokenByName()
{
var json = """
{
'short': {
'key': {
'code': 0,
'msg': 'No action taken'
},
'Scrub': {
'code': 2,
'msg': 'ignore this'
}
}
}
""";
var target = JToken.Parse(json);
return Verify(target)
.ScrubMember("Scrub");
}
[Fact]
public Task VerifyJsonGuid() =>
VerifyJson("{'key': 'c572ff75-e1a2-49bd-99b9-4550697946c3'}");
[Fact]
public Task VerifyJsonEmpty() =>
VerifyJson("{}");
[Fact]
public Task VerifyJsonRefRespectSerializerSettings() =>
VerifyJson("{'$ref': '#/no/ref'}")
.AddExtraSettings(s => s.MetadataPropertyHandling = MetadataPropertyHandling.Ignore);
[Fact]
public Task VerifyJsonTypeRespectSerializerSettings() =>
VerifyJson("{ '$type': 'MyNamespace.User, MyAssembly'}")
.AddExtraSettings(s => s.MetadataPropertyHandling = MetadataPropertyHandling.Ignore);
[Fact]
public Task VerifyJsonOnlyIgnoredMember() =>
VerifyJson("{'key': 'value'}")
.IgnoreMember("key");
[Fact]
public Task VerifyJsonDateTime()
{
var json = $"{{'key': '{DateTime.Now:yyyy-MM-ddTHH:mm:ss}'}}";
return VerifyJson(json);
}
[Fact]
public Task VerifyJsonWithArray()
{
var json = """
{
commitments: [
{
id: '9585dadf-551a-43eb-960c-18b935993cc3',
title: 'Commitment1'
}
]
}
""";
return VerifyJson(json);
}
[Fact]
public Task VerifyJsonWithArrayAtRoot()
{
var json = """
[
{
id: '9585dadf-551a-43eb-960c-18b935993cc3',
title: 'Commitment1'
}
]
""";
return VerifyJson(json);
}
#region VerifyJson
[Fact]
public Task VerifyJsonString()
{
var json = "{'key': {'msg': 'No action taken'}}";
return VerifyJson(json);
}
[Fact]
public Task VerifyJsonStream()
{
var json = "{'key': {'msg': 'No action taken'}}";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
return VerifyJson(stream);
}
[Fact]
public Task VerifyJsonJToken()
{
var json = "{'key': {'msg': 'No action taken'}}";
var target = JToken.Parse(json);
return Verify(target);
}
#endregion
}