| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | | 3 | | // license information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Reflection; |
| | | 9 | | using Microsoft.Azure.Search.Common; |
| | | 10 | | using Newtonsoft.Json; |
| | | 11 | | using Newtonsoft.Json.Linq; |
| | | 12 | | |
| | | 13 | | namespace Microsoft.Azure.Search.Serialization |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Defines extension methods for various JSON.NET types that make it easier to implement a custom JsonConverter. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class JsonExtensions |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts |
| | | 22 | | /// that the value of the token matches a given expected value. If any of the assertions fail, this method |
| | | 23 | | /// throws a JsonSerializationException. Otherwise, this method attempts to advance the JSON reader to the |
| | | 24 | | /// next position. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="reader">The JSON reader.</param> |
| | | 27 | | /// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param> |
| | | 28 | | /// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param> |
| | | 29 | | public static void ExpectAndAdvance( |
| | | 30 | | this JsonReader reader, |
| | | 31 | | JsonToken expectedToken, |
| | 3814 | 32 | | params object[] expectedValues) => ExpectAndAdvance<object>(reader, expectedToken, expectedValues); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the |
| | | 36 | | /// value of the token, if any. Optionally asserts that the value of the token matches a given expected |
| | | 37 | | /// value. If any of the assertions fail, this method throws a JsonSerializationException. Otherwise, this |
| | | 38 | | /// method attempts to advance the JSON reader to the next position. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <typeparam name="TValue">The expected type of the value of the current JSON token.</typeparam> |
| | | 41 | | /// <param name="reader">The JSON reader.</param> |
| | | 42 | | /// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param> |
| | | 43 | | /// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param> |
| | | 44 | | /// <returns> |
| | | 45 | | /// The value of the JSON token before advancing the reader, or default(TValue) if the token has no value. |
| | | 46 | | /// </returns> |
| | | 47 | | public static TValue ExpectAndAdvance<TValue>( |
| | | 48 | | this JsonReader reader, |
| | | 49 | | JsonToken expectedToken, |
| | | 50 | | params object[] expectedValues) |
| | | 51 | | { |
| | 7726 | 52 | | TValue result = Expect<TValue>(reader, expectedToken, expectedValues); |
| | 7716 | 53 | | Advance(reader); |
| | 7716 | 54 | | return result; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Asserts that the given JSON reader is positioned on a token with the expected type. Optionally asserts |
| | | 59 | | /// that the value of the token matches a given expected value. If any of the assertions fail, this method |
| | | 60 | | /// throws a JsonSerializationException. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="reader">The JSON reader.</param> |
| | | 63 | | /// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param> |
| | | 64 | | /// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param> |
| | | 65 | | public static void Expect(this JsonReader reader, JsonToken expectedToken, params object[] expectedValues) => |
| | 0 | 66 | | Expect<object>(reader, expectedToken, expectedValues); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Asserts that the given JSON reader is positioned on a token with the expected type and retrieves the |
| | | 70 | | /// value of the token, if any. Optionally asserts that the value of the token matches a given expected |
| | | 71 | | /// value. If any of the assertions fail, this method throws a JsonSerializationException. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <typeparam name="TValue">The expected type of the value of the current JSON token.</typeparam> |
| | | 74 | | /// <param name="reader">The JSON reader.</param> |
| | | 75 | | /// <param name="expectedToken">The JSON token on which the reader is expected to be positioned.</param> |
| | | 76 | | /// <param name="expectedValues">Optional; The expected possible values of the current JSON token.</param> |
| | | 77 | | /// <returns> |
| | | 78 | | /// The value of the current JSON token, or default(TValue) if the current token has no value. |
| | | 79 | | /// </returns> |
| | | 80 | | public static TValue Expect<TValue>( |
| | | 81 | | this JsonReader reader, |
| | | 82 | | JsonToken expectedToken, |
| | | 83 | | params object[] expectedValues) |
| | | 84 | | { |
| | 24642 | 85 | | Throw.IfArgumentNull(reader, nameof(reader)); |
| | | 86 | | |
| | 24642 | 87 | | if (reader.TokenType != expectedToken) |
| | | 88 | | { |
| | 2 | 89 | | throw new JsonSerializationException( |
| | 2 | 90 | | string.Format("Deserialization failed. Expected token: '{0}'", expectedToken)); |
| | | 91 | | } |
| | | 92 | | |
| | 24640 | 93 | | if (expectedValues != null && expectedValues.Length > 0 && |
| | 30722 | 94 | | (reader.Value == null || !Array.Exists(expectedValues, v => reader.Value.Equals(v)))) |
| | | 95 | | { |
| | 8 | 96 | | string message = |
| | 8 | 97 | | string.Format( |
| | 8 | 98 | | "Deserialization failed. Expected value(s): '{0}'. Actual: '{1}'", |
| | 8 | 99 | | string.Join(", ", expectedValues), |
| | 8 | 100 | | reader.Value); |
| | | 101 | | |
| | 8 | 102 | | throw new JsonSerializationException(message); |
| | | 103 | | } |
| | | 104 | | |
| | 24632 | 105 | | var result = default(TValue); |
| | | 106 | | |
| | 24632 | 107 | | if (reader.Value != null) |
| | | 108 | | { |
| | 22222 | 109 | | if (!typeof(TValue).GetTypeInfo().IsAssignableFrom(reader.ValueType.GetTypeInfo())) |
| | | 110 | | { |
| | 0 | 111 | | string message = |
| | 0 | 112 | | string.Format( |
| | 0 | 113 | | "Deserialization failed. Value '{0}' is not of expected type '{1}'.", |
| | 0 | 114 | | reader.Value, |
| | 0 | 115 | | typeof(TValue)); |
| | | 116 | | |
| | 0 | 117 | | throw new JsonSerializationException(message); |
| | | 118 | | } |
| | | 119 | | |
| | 22222 | 120 | | result = (TValue)reader.Value; |
| | | 121 | | } |
| | | 122 | | |
| | 24632 | 123 | | return result; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Advances the given JSON reader, or throws a JsonSerializationException if it cannot be advanced. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="reader">The JSON reader to advance.</param> |
| | | 130 | | public static void Advance(this JsonReader reader) |
| | | 131 | | { |
| | 8766 | 132 | | Throw.IfArgumentNull(reader, nameof(reader)); |
| | | 133 | | |
| | 8766 | 134 | | if (!reader.Read()) |
| | | 135 | | { |
| | 0 | 136 | | throw new JsonSerializationException("Deserialization failed. Unexpected end of input."); |
| | | 137 | | } |
| | 8766 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order o |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="reader">The JSON reader to use to read an object.</param> |
| | | 144 | | /// <param name="requiredProperties"> |
| | | 145 | | /// The names of all JSON properties that are expected to be present in the parsed object. |
| | | 146 | | /// </param> |
| | | 147 | | /// <param name="readProperty"> |
| | | 148 | | /// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c |
| | | 149 | | /// advance the reader to the name of the next property, or the end of the object if there are no more propertie |
| | | 150 | | /// </param> |
| | | 151 | | /// <remarks> |
| | | 152 | | /// This method will leave the reader positioned on the end of the object. |
| | | 153 | | /// </remarks> |
| | | 154 | | public static void ReadObject( |
| | | 155 | | this JsonReader reader, |
| | | 156 | | IEnumerable<string> requiredProperties, |
| | | 157 | | Action<JsonReader, string> readProperty) => |
| | 920 | 158 | | reader.ReadObject(requiredProperties, Enumerable.Empty<string>(), readProperty); |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order o |
| | | 162 | | /// and then advances the given reader to the next token after the end of the object. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="reader">The JSON reader to use to read an object.</param> |
| | | 165 | | /// <param name="requiredProperties"> |
| | | 166 | | /// The names of all JSON properties that are expected to be present in the parsed object. |
| | | 167 | | /// </param> |
| | | 168 | | /// <param name="readProperty"> |
| | | 169 | | /// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c |
| | | 170 | | /// advance the reader to the name of the next property, or the end of the object if there are no more propertie |
| | | 171 | | /// </param> |
| | | 172 | | /// <remarks> |
| | | 173 | | /// This method will advance the reader to the next position after the end of the object. |
| | | 174 | | /// </remarks> |
| | | 175 | | public static void ReadObjectAndAdvance( |
| | | 176 | | this JsonReader reader, |
| | | 177 | | IEnumerable<string> requiredProperties, |
| | | 178 | | Action<JsonReader, string> readProperty) |
| | | 179 | | { |
| | 920 | 180 | | reader.ReadObject(requiredProperties, readProperty); |
| | 914 | 181 | | reader.Advance(); |
| | 914 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Reads the properties of JSON objects, enforcing the presence of required properties and ignoring the order o |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="reader">The JSON reader to use to read an object.</param> |
| | | 188 | | /// <param name="requiredProperties"> |
| | | 189 | | /// The names of all JSON properties that are expected to be present in the parsed object. |
| | | 190 | | /// </param> |
| | | 191 | | /// <param name="optionalProperties"> |
| | | 192 | | /// The names of JSON properties besides the required properties that may be present in the parsed object. |
| | | 193 | | /// </param> |
| | | 194 | | /// <param name="readProperty"> |
| | | 195 | | /// A callback that reads a property value with the given name from the given <c cref="JsonReader">JsonReader</c |
| | | 196 | | /// advance the reader to the name of the next property, or the end of the object if there are no more propertie |
| | | 197 | | /// </param> |
| | | 198 | | /// <remarks> |
| | | 199 | | /// This method will leave the reader positioned on the end of the object. |
| | | 200 | | /// </remarks> |
| | | 201 | | public static void ReadObject( |
| | | 202 | | this JsonReader reader, |
| | | 203 | | IEnumerable<string> requiredProperties, |
| | | 204 | | IEnumerable<string> optionalProperties, |
| | | 205 | | Action<JsonReader, string> readProperty) |
| | | 206 | | { |
| | 1406 | 207 | | Throw.IfArgumentNull(requiredProperties, nameof(requiredProperties)); |
| | 1406 | 208 | | Throw.IfArgumentNull(optionalProperties, nameof(optionalProperties)); |
| | 1406 | 209 | | Throw.IfArgumentNull(readProperty, nameof(readProperty)); |
| | | 210 | | |
| | | 211 | | // ExpectAndAdvance validates that reader is not null. |
| | 1406 | 212 | | reader.ExpectAndAdvance(JsonToken.StartObject); |
| | | 213 | | |
| | 1406 | 214 | | string[] allPropertyNames = requiredProperties.Concat(optionalProperties).ToArray(); |
| | 1406 | 215 | | var processedProperties = new HashSet<string>(); |
| | | 216 | | |
| | 4202 | 217 | | while (reader.TokenType != JsonToken.EndObject) |
| | | 218 | | { |
| | 2812 | 219 | | string propertyName = reader.ExpectAndAdvance<string>(JsonToken.PropertyName, allPropertyNames); |
| | 2806 | 220 | | readProperty(reader, propertyName); |
| | 2796 | 221 | | processedProperties.Add(propertyName); |
| | | 222 | | } |
| | | 223 | | |
| | 7418 | 224 | | foreach (var propertyName in requiredProperties) |
| | | 225 | | { |
| | 2320 | 226 | | if (!processedProperties.Contains(propertyName)) |
| | | 227 | | { |
| | 2 | 228 | | throw new JsonSerializationException( |
| | 2 | 229 | | string.Format("Deserialization failed. Could not find required '{0}' property.", propertyName)); |
| | | 230 | | } |
| | | 231 | | } |
| | 1388 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Indicates whether or not the given JSON token matches the expected string. |
| | | 236 | | /// </summary> |
| | | 237 | | /// <param name="token">The token to check.</param> |
| | | 238 | | /// <param name="expectedValue">The expected string value.</param> |
| | | 239 | | /// <returns><c>true</c> if the given JSON token matches the expected string, <c>false</c> otherwise.</returns> |
| | | 240 | | public static bool IsString(this JToken token, string expectedValue) => |
| | 362 | 241 | | token?.Type == JTokenType.String && token?.Value<string>() == expectedValue; |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Indicates whether or not the given JSON token is a numeric literal. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="token">The token to check.</param> |
| | | 247 | | /// <returns><c>true</c> if the given JSON token represents a number, <c>false</c> otherwise.</returns> |
| | 268 | 248 | | public static bool IsNumber(this JToken token) => token?.Type == JTokenType.Float || token?.Type == JTokenType.I |
| | | 249 | | |
| | | 250 | | /// <summary> |
| | | 251 | | /// Validates the properties of the given JSON object, enforcing the presence of required properties and ignorin |
| | | 252 | | /// the order of properties. |
| | | 253 | | /// </summary> |
| | | 254 | | /// <param name="obj">The JSON object to validate.</param> |
| | | 255 | | /// <param name="requiredProperties"> |
| | | 256 | | /// The names of all JSON properties that are expected to be present in the given object. |
| | | 257 | | /// </param> |
| | | 258 | | /// <param name="isPropertyValid"> |
| | | 259 | | /// A predicate that determines whether the name and value of given <c cref="JProperty">JProperty</c> are valid. |
| | | 260 | | /// </param> |
| | | 261 | | /// <returns> |
| | | 262 | | /// <c>true</c> if all properties of the given JSON object pass the given validation function and all required p |
| | | 263 | | /// <c>false</c> otherwise. |
| | | 264 | | /// </returns> |
| | | 265 | | public static bool IsValid(this JObject obj, IEnumerable<string> requiredProperties, Func<JProperty, bool> isPro |
| | | 266 | | { |
| | 538 | 267 | | Throw.IfArgumentNull(obj, nameof(obj)); |
| | 538 | 268 | | Throw.IfArgumentNull(requiredProperties, nameof(requiredProperties)); |
| | 538 | 269 | | Throw.IfArgumentNull(isPropertyValid, nameof(isPropertyValid)); |
| | | 270 | | |
| | 538 | 271 | | var processedProperties = new HashSet<string>(); |
| | | 272 | | |
| | 2686 | 273 | | foreach (JProperty property in obj.Properties()) |
| | | 274 | | { |
| | 900 | 275 | | if (isPropertyValid(property)) |
| | | 276 | | { |
| | 710 | 277 | | processedProperties.Add(property.Name); |
| | | 278 | | } |
| | | 279 | | else |
| | | 280 | | { |
| | 190 | 281 | | return false; |
| | | 282 | | } |
| | | 283 | | } |
| | | 284 | | |
| | 932 | 285 | | return requiredProperties.All(p => processedProperties.Contains(p)); |
| | 190 | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |