-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
JsonExtensions.cs
288 lines (260 loc) · 14.1 KB
/
JsonExtensions.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Azure.Search.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.Search.Serialization
{
/// <summary>
/// Defines extension methods for various JSON.NET types that make it easier to implement a custom JsonConverter.
/// </summary>
public static class JsonExtensions
{
/// <summary>
/// Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts
/// that the value of the token matches a given expected value. If any of the assertions fail, this method
/// throws a JsonSerializationException. Otherwise, this method attempts to advance the JSON reader to the
/// next position.
/// </summary>
/// <param name="reader">The JSON reader.</param>
/// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param>
/// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param>
public static void ExpectAndAdvance(
this JsonReader reader,
JsonToken expectedToken,
params object[] expectedValues) => ExpectAndAdvance<object>(reader, expectedToken, expectedValues);
/// <summary>
/// Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the
/// value of the token, if any. Optionally asserts that the value of the token matches a given expected
/// value. If any of the assertions fail, this method throws a JsonSerializationException. Otherwise, this
/// method attempts to advance the JSON reader to the next position.
/// </summary>
/// <typeparam name="TValue">The expected type of the value of the current JSON token.</typeparam>
/// <param name="reader">The JSON reader.</param>
/// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param>
/// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param>
/// <returns>
/// The value of the JSON token before advancing the reader, or default(TValue) if the token has no value.
/// </returns>
public static TValue ExpectAndAdvance<TValue>(
this JsonReader reader,
JsonToken expectedToken,
params object[] expectedValues)
{
TValue result = Expect<TValue>(reader, expectedToken, expectedValues);
Advance(reader);
return result;
}
/// <summary>
/// Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts
/// that the value of the token matches a given expected value. If any of the assertions fail, this method
/// throws a JsonSerializationException.
/// </summary>
/// <param name="reader">The JSON reader.</param>
/// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param>
/// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param>
public static void Expect(this JsonReader reader, JsonToken expectedToken, params object[] expectedValues) =>
Expect<object>(reader, expectedToken, expectedValues);
/// <summary>
/// Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the
/// value of the token, if any. Optionally asserts that the value of the token matches a given expected
/// value. If any of the assertions fail, this method throws a JsonSerializationException.
/// </summary>
/// <typeparam name="TValue">The expected type of the value of the current JSON token.</typeparam>
/// <param name="reader">The JSON reader.</param>
/// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param>
/// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param>
/// <returns>
/// The value of the current JSON token, or default(TValue) if the current token has no value.
/// </returns>
public static TValue Expect<TValue>(
this JsonReader reader,
JsonToken expectedToken,
params object[] expectedValues)
{
Throw.IfArgumentNull(reader, nameof(reader));
if (reader.TokenType != expectedToken)
{
throw new JsonSerializationException(
string.Format("Deserialization failed. Expected token: '{0}'", expectedToken));
}
if (expectedValues != null && expectedValues.Length > 0 &&
(reader.Value == null || !Array.Exists(expectedValues, v => reader.Value.Equals(v))))
{
string message =
string.Format(
"Deserialization failed. Expected value(s): '{0}'. Actual: '{1}'",
string.Join(", ", expectedValues),
reader.Value);
throw new JsonSerializationException(message);
}
var result = default(TValue);
if (reader.Value != null)
{
if (!typeof(TValue).GetTypeInfo().IsAssignableFrom(reader.ValueType.GetTypeInfo()))
{
string message =
string.Format(
"Deserialization failed. Value '{0}' is not of expected type '{1}'.",
reader.Value,
typeof(TValue));
throw new JsonSerializationException(message);
}
result = (TValue)reader.Value;
}
return result;
}
/// <summary>
/// Advances the given JSON reader, or throws a JsonSerializationException if it cannot be advanced.
/// </summary>
/// <param name="reader">The JSON reader to advance.</param>
public static void Advance(this JsonReader reader)
{
Throw.IfArgumentNull(reader, nameof(reader));
if (!reader.Read())
{
throw new JsonSerializationException("Deserialization failed. Unexpected end of input.");
}
}
/// <summary>
/// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties.
/// </summary>
/// <param name="reader">The JSON reader to use to read an object.</param>
/// <param name="requiredProperties">
/// The names of all JSON properties that are expected to be present in the parsed object.
/// </param>
/// <param name="readProperty">
/// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c>. It must
/// advance the reader to the name of the next property, or the end of the object if there are no more properties to read.
/// </param>
/// <remarks>
/// This method will leave the reader positioned on the end of the object.
/// </remarks>
public static void ReadObject(
this JsonReader reader,
IEnumerable<string> requiredProperties,
Action<JsonReader, string> readProperty) =>
reader.ReadObject(requiredProperties, Enumerable.Empty<string>(), readProperty);
/// <summary>
/// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties,
/// and then advances the given reader to the next token after the end of the object.
/// </summary>
/// <param name="reader">The JSON reader to use to read an object.</param>
/// <param name="requiredProperties">
/// The names of all JSON properties that are expected to be present in the parsed object.
/// </param>
/// <param name="readProperty">
/// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c>. It must
/// advance the reader to the name of the next property, or the end of the object if there are no more properties to read.
/// </param>
/// <remarks>
/// This method will advance the reader to the next position after the end of the object.
/// </remarks>
public static void ReadObjectAndAdvance(
this JsonReader reader,
IEnumerable<string> requiredProperties,
Action<JsonReader, string> readProperty)
{
reader.ReadObject(requiredProperties, readProperty);
reader.Advance();
}
/// <summary>
/// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order of properties.
/// </summary>
/// <param name="reader">The JSON reader to use to read an object.</param>
/// <param name="requiredProperties">
/// The names of all JSON properties that are expected to be present in the parsed object.
/// </param>
/// <param name="optionalProperties">
/// The names of JSON properties besides the required properties that may be present in the parsed object.
/// </param>
/// <param name="readProperty">
/// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c>. It must
/// advance the reader to the name of the next property, or the end of the object if there are no more properties to read.
/// </param>
/// <remarks>
/// This method will leave the reader positioned on the end of the object.
/// </remarks>
public static void ReadObject(
this JsonReader reader,
IEnumerable<string> requiredProperties,
IEnumerable<string> optionalProperties,
Action<JsonReader, string> readProperty)
{
Throw.IfArgumentNull(requiredProperties, nameof(requiredProperties));
Throw.IfArgumentNull(optionalProperties, nameof(optionalProperties));
Throw.IfArgumentNull(readProperty, nameof(readProperty));
// ExpectAndAdvance validates that reader is not null.
reader.ExpectAndAdvance(JsonToken.StartObject);
string[] allPropertyNames = requiredProperties.Concat(optionalProperties).ToArray();
var processedProperties = new HashSet<string>();
while (reader.TokenType != JsonToken.EndObject)
{
string propertyName = reader.ExpectAndAdvance<string>(JsonToken.PropertyName, allPropertyNames);
readProperty(reader, propertyName);
processedProperties.Add(propertyName);
}
foreach (var propertyName in requiredProperties)
{
if (!processedProperties.Contains(propertyName))
{
throw new JsonSerializationException(
string.Format("Deserialization failed. Could not find required '{0}' property.", propertyName));
}
}
}
/// <summary>
/// Indicates whether or not the given JSON token matches the expected string.
/// </summary>
/// <param name="token">The token to check.</param>
/// <param name="expectedValue">The expected string value.</param>
/// <returns><c>true</c> if the given JSON token matches the expected string, <c>false</c> otherwise.</returns>
public static bool IsString(this JToken token, string expectedValue) =>
token?.Type == JTokenType.String && token?.Value<string>() == expectedValue;
/// <summary>
/// Indicates whether or not the given JSON token is a numeric literal.
/// </summary>
/// <param name="token">The token to check.</param>
/// <returns><c>true</c> if the given JSON token represents a number, <c>false</c> otherwise.</returns>
public static bool IsNumber(this JToken token) => token?.Type == JTokenType.Float || token?.Type == JTokenType.Integer;
/// <summary>
/// Validates the properties of the given JSON object, enforcing the presence of required properties and ignoring
/// the order of properties.
/// </summary>
/// <param name="obj">The JSON object to validate.</param>
/// <param name="requiredProperties">
/// The names of all JSON properties that are expected to be present in the given object.
/// </param>
/// <param name="isPropertyValid">
/// A predicate that determines whether the name and value of given <c cref="JProperty">JProperty</c> are valid.
/// </param>
/// <returns>
/// <c>true</c> if all properties of the given JSON object pass the given validation function and all required properties exist,
/// <c>false</c> otherwise.
/// </returns>
public static bool IsValid(this JObject obj, IEnumerable<string> requiredProperties, Func<JProperty, bool> isPropertyValid)
{
Throw.IfArgumentNull(obj, nameof(obj));
Throw.IfArgumentNull(requiredProperties, nameof(requiredProperties));
Throw.IfArgumentNull(isPropertyValid, nameof(isPropertyValid));
var processedProperties = new HashSet<string>();
foreach (JProperty property in obj.Properties())
{
if (isPropertyValid(property))
{
processedProperties.Add(property.Name);
}
else
{
return false;
}
}
return requiredProperties.All(p => processedProperties.Contains(p));
}
}
}