| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers.Text; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents a specific JSON value within a <see cref="JsonDocument"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 17 | | public readonly partial struct JsonElement |
| | | 18 | | { |
| | | 19 | | private readonly JsonDocument _parent; |
| | | 20 | | private readonly int _idx; |
| | | 21 | | |
| | | 22 | | internal JsonElement(JsonDocument parent, int idx) |
| | 966 | 23 | | { |
| | | 24 | | // parent is usually not null, but the Current property |
| | | 25 | | // on the enumerators (when initialized as `default`) can |
| | | 26 | | // get here with a null. |
| | 966 | 27 | | Debug.Assert(idx >= 0); |
| | | 28 | | |
| | 966 | 29 | | _parent = parent; |
| | 966 | 30 | | _idx = idx; |
| | 966 | 31 | | } |
| | | 32 | | |
| | | 33 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 34 | | private JsonTokenType TokenType |
| | | 35 | | { |
| | | 36 | | get |
| | 145 | 37 | | { |
| | 145 | 38 | | return _parent?.GetJsonTokenType(_idx) ?? JsonTokenType.None; |
| | 145 | 39 | | } |
| | | 40 | | } |
| | | 41 | | /// <summary> |
| | | 42 | | /// The <see cref="JsonValueKind"/> that the value is. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <exception cref="ObjectDisposedException"> |
| | | 45 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 46 | | /// </exception> |
| | 142 | 47 | | public JsonValueKind ValueKind => TokenType.ToValueKind(); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Get the value at a specified index when the current value is a |
| | | 51 | | /// <see cref="JsonValueKind.Array"/>. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <exception cref="InvalidOperationException"> |
| | | 54 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>. |
| | | 55 | | /// </exception> |
| | | 56 | | /// <exception cref="IndexOutOfRangeException"> |
| | | 57 | | /// <paramref name="index"/> is not in the range [0, <see cref="GetArrayLength"/>()). |
| | | 58 | | /// </exception> |
| | | 59 | | /// <exception cref="ObjectDisposedException"> |
| | | 60 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 61 | | /// </exception> |
| | | 62 | | public JsonElement this[int index] |
| | | 63 | | { |
| | | 64 | | get |
| | 0 | 65 | | { |
| | 0 | 66 | | CheckValidInstance(); |
| | | 67 | | |
| | 0 | 68 | | return _parent.GetArrayIndexElement(_idx, index); |
| | 0 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Get the number of values contained within the current array value. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns>The number of values contained within the current array value.</returns> |
| | | 76 | | /// <exception cref="InvalidOperationException"> |
| | | 77 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>. |
| | | 78 | | /// </exception> |
| | | 79 | | /// <exception cref="ObjectDisposedException"> |
| | | 80 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 81 | | /// </exception> |
| | | 82 | | public int GetArrayLength() |
| | 0 | 83 | | { |
| | 0 | 84 | | CheckValidInstance(); |
| | | 85 | | |
| | 0 | 86 | | return _parent.GetArrayLength(_idx); |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Get the number of properties contained within the current object value. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <returns>The number of properties contained within the current object value.</returns> |
| | | 93 | | /// <exception cref="InvalidOperationException"> |
| | | 94 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 95 | | /// </exception> |
| | | 96 | | /// <exception cref="ObjectDisposedException"> |
| | | 97 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 98 | | /// </exception> |
| | | 99 | | public int GetPropertyCount() |
| | 0 | 100 | | { |
| | 0 | 101 | | CheckValidInstance(); |
| | | 102 | | |
| | 0 | 103 | | return _parent.GetPropertyCount(_idx); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets a <see cref="JsonElement"/> representing the value of a required property identified |
| | | 108 | | /// by <paramref name="propertyName"/>. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <remarks> |
| | | 111 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 112 | | /// |
| | | 113 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 114 | | /// what is matched. |
| | | 115 | | /// </remarks> |
| | | 116 | | /// <param name="propertyName">Name of the property whose value to return.</param> |
| | | 117 | | /// <returns> |
| | | 118 | | /// A <see cref="JsonElement"/> representing the value of the requested property. |
| | | 119 | | /// </returns> |
| | | 120 | | /// <seealso cref="EnumerateObject"/> |
| | | 121 | | /// <exception cref="InvalidOperationException"> |
| | | 122 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 123 | | /// </exception> |
| | | 124 | | /// <exception cref="KeyNotFoundException"> |
| | | 125 | | /// No property was found with the requested name. |
| | | 126 | | /// </exception> |
| | | 127 | | /// <exception cref="ArgumentNullException"> |
| | | 128 | | /// <paramref name="propertyName"/> is <see langword="null"/>. |
| | | 129 | | /// </exception> |
| | | 130 | | /// <exception cref="ObjectDisposedException"> |
| | | 131 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 132 | | /// </exception> |
| | | 133 | | public JsonElement GetProperty(string propertyName) |
| | 0 | 134 | | { |
| | 0 | 135 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | | 136 | | |
| | 0 | 137 | | if (TryGetProperty(propertyName, out JsonElement property)) |
| | 0 | 138 | | { |
| | 0 | 139 | | return property; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | throw new KeyNotFoundException(); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets a <see cref="JsonElement"/> representing the value of a required property identified |
| | | 147 | | /// by <paramref name="propertyName"/>. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <remarks> |
| | | 150 | | /// <para> |
| | | 151 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 152 | | /// </para> |
| | | 153 | | /// |
| | | 154 | | /// <para> |
| | | 155 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 156 | | /// what is matched. |
| | | 157 | | /// </para> |
| | | 158 | | /// </remarks> |
| | | 159 | | /// <param name="propertyName">Name of the property whose value to return.</param> |
| | | 160 | | /// <returns> |
| | | 161 | | /// A <see cref="JsonElement"/> representing the value of the requested property. |
| | | 162 | | /// </returns> |
| | | 163 | | /// <seealso cref="EnumerateObject"/> |
| | | 164 | | /// <exception cref="InvalidOperationException"> |
| | | 165 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 166 | | /// </exception> |
| | | 167 | | /// <exception cref="KeyNotFoundException"> |
| | | 168 | | /// No property was found with the requested name. |
| | | 169 | | /// </exception> |
| | | 170 | | /// <exception cref="ObjectDisposedException"> |
| | | 171 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 172 | | /// </exception> |
| | | 173 | | public JsonElement GetProperty(ReadOnlySpan<char> propertyName) |
| | 0 | 174 | | { |
| | 0 | 175 | | if (TryGetProperty(propertyName, out JsonElement property)) |
| | 0 | 176 | | { |
| | 0 | 177 | | return property; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | throw new KeyNotFoundException(); |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets a <see cref="JsonElement"/> representing the value of a required property identified |
| | | 185 | | /// by <paramref name="utf8PropertyName"/>. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <remarks> |
| | | 188 | | /// <para> |
| | | 189 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 190 | | /// </para> |
| | | 191 | | /// |
| | | 192 | | /// <para> |
| | | 193 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 194 | | /// what is matched. |
| | | 195 | | /// </para> |
| | | 196 | | /// </remarks> |
| | | 197 | | /// <param name="utf8PropertyName"> |
| | | 198 | | /// The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return. |
| | | 199 | | /// </param> |
| | | 200 | | /// <returns> |
| | | 201 | | /// A <see cref="JsonElement"/> representing the value of the requested property. |
| | | 202 | | /// </returns> |
| | | 203 | | /// <exception cref="InvalidOperationException"> |
| | | 204 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 205 | | /// </exception> |
| | | 206 | | /// <exception cref="KeyNotFoundException"> |
| | | 207 | | /// No property was found with the requested name. |
| | | 208 | | /// </exception> |
| | | 209 | | /// <exception cref="ObjectDisposedException"> |
| | | 210 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 211 | | /// </exception> |
| | | 212 | | /// <seealso cref="EnumerateObject"/> |
| | | 213 | | public JsonElement GetProperty(ReadOnlySpan<byte> utf8PropertyName) |
| | 0 | 214 | | { |
| | 0 | 215 | | if (TryGetProperty(utf8PropertyName, out JsonElement property)) |
| | 0 | 216 | | { |
| | 0 | 217 | | return property; |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | throw new KeyNotFoundException(); |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Looks for a property named <paramref name="propertyName"/> in the current object, returning |
| | | 225 | | /// whether or not such a property existed. When the property exists <paramref name="value"/> |
| | | 226 | | /// is assigned to the value of that property. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <remarks> |
| | | 229 | | /// <para> |
| | | 230 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 231 | | /// </para> |
| | | 232 | | /// |
| | | 233 | | /// <para> |
| | | 234 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 235 | | /// what is matched. |
| | | 236 | | /// </para> |
| | | 237 | | /// </remarks> |
| | | 238 | | /// <param name="propertyName">Name of the property to find.</param> |
| | | 239 | | /// <param name="value">Receives the value of the located property.</param> |
| | | 240 | | /// <returns> |
| | | 241 | | /// <see langword="true"/> if the property was found, <see langword="false"/> otherwise. |
| | | 242 | | /// </returns> |
| | | 243 | | /// <exception cref="InvalidOperationException"> |
| | | 244 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 245 | | /// </exception> |
| | | 246 | | /// <exception cref="ArgumentNullException"> |
| | | 247 | | /// <paramref name="propertyName"/> is <see langword="null"/>. |
| | | 248 | | /// </exception> |
| | | 249 | | /// <exception cref="ObjectDisposedException"> |
| | | 250 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 251 | | /// </exception> |
| | | 252 | | /// <seealso cref="EnumerateObject"/> |
| | | 253 | | public bool TryGetProperty(string propertyName, out JsonElement value) |
| | 0 | 254 | | { |
| | 0 | 255 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | | 256 | | |
| | 0 | 257 | | return TryGetProperty(propertyName.AsSpan(), out value); |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Looks for a property named <paramref name="propertyName"/> in the current object, returning |
| | | 262 | | /// whether or not such a property existed. When the property exists <paramref name="value"/> |
| | | 263 | | /// is assigned to the value of that property. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <remarks> |
| | | 266 | | /// <para> |
| | | 267 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 268 | | /// </para> |
| | | 269 | | /// |
| | | 270 | | /// <para> |
| | | 271 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 272 | | /// what is matched. |
| | | 273 | | /// </para> |
| | | 274 | | /// </remarks> |
| | | 275 | | /// <param name="propertyName">Name of the property to find.</param> |
| | | 276 | | /// <param name="value">Receives the value of the located property.</param> |
| | | 277 | | /// <returns> |
| | | 278 | | /// <see langword="true"/> if the property was found, <see langword="false"/> otherwise. |
| | | 279 | | /// </returns> |
| | | 280 | | /// <seealso cref="EnumerateObject"/> |
| | | 281 | | /// <exception cref="InvalidOperationException"> |
| | | 282 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 283 | | /// </exception> |
| | | 284 | | /// <exception cref="ObjectDisposedException"> |
| | | 285 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 286 | | /// </exception> |
| | | 287 | | public bool TryGetProperty(ReadOnlySpan<char> propertyName, out JsonElement value) |
| | 0 | 288 | | { |
| | 0 | 289 | | CheckValidInstance(); |
| | | 290 | | |
| | 0 | 291 | | return _parent.TryGetNamedPropertyValue(_idx, propertyName, out value); |
| | 0 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Looks for a property named <paramref name="utf8PropertyName"/> in the current object, returning |
| | | 296 | | /// whether or not such a property existed. When the property exists <paramref name="value"/> |
| | | 297 | | /// is assigned to the value of that property. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <remarks> |
| | | 300 | | /// <para> |
| | | 301 | | /// Property name matching is performed as an ordinal, case-sensitive, comparison. |
| | | 302 | | /// </para> |
| | | 303 | | /// |
| | | 304 | | /// <para> |
| | | 305 | | /// If a property is defined multiple times for the same object, the last such definition is |
| | | 306 | | /// what is matched. |
| | | 307 | | /// </para> |
| | | 308 | | /// </remarks> |
| | | 309 | | /// <param name="utf8PropertyName"> |
| | | 310 | | /// The UTF-8 (with no Byte-Order-Mark (BOM)) representation of the name of the property to return. |
| | | 311 | | /// </param> |
| | | 312 | | /// <param name="value">Receives the value of the located property.</param> |
| | | 313 | | /// <returns> |
| | | 314 | | /// <see langword="true"/> if the property was found, <see langword="false"/> otherwise. |
| | | 315 | | /// </returns> |
| | | 316 | | /// <seealso cref="EnumerateObject"/> |
| | | 317 | | /// <exception cref="InvalidOperationException"> |
| | | 318 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 319 | | /// </exception> |
| | | 320 | | /// <exception cref="ObjectDisposedException"> |
| | | 321 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 322 | | /// </exception> |
| | | 323 | | public bool TryGetProperty(ReadOnlySpan<byte> utf8PropertyName, out JsonElement value) |
| | 0 | 324 | | { |
| | 0 | 325 | | CheckValidInstance(); |
| | | 326 | | |
| | 0 | 327 | | return _parent.TryGetNamedPropertyValue(_idx, utf8PropertyName, out value); |
| | 0 | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Gets the value of the element as a <see cref="bool"/>. |
| | | 332 | | /// </summary> |
| | | 333 | | /// <remarks> |
| | | 334 | | /// This method does not parse the contents of a JSON string value. |
| | | 335 | | /// </remarks> |
| | | 336 | | /// <returns>The value of the element as a <see cref="bool"/>.</returns> |
| | | 337 | | /// <exception cref="InvalidOperationException"> |
| | | 338 | | /// This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.True"/> or |
| | | 339 | | /// <see cref="JsonValueKind.False"/>. |
| | | 340 | | /// </exception> |
| | | 341 | | /// <exception cref="ObjectDisposedException"> |
| | | 342 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 343 | | /// </exception> |
| | | 344 | | public bool GetBoolean() |
| | 0 | 345 | | { |
| | | 346 | | // CheckValidInstance is redundant. Asking for the type will |
| | | 347 | | // return None, which then throws the same exception in the return statement. |
| | | 348 | | |
| | 0 | 349 | | JsonTokenType type = TokenType; |
| | | 350 | | |
| | 0 | 351 | | return |
| | 0 | 352 | | type == JsonTokenType.True ? true : |
| | 0 | 353 | | type == JsonTokenType.False ? false : |
| | 0 | 354 | | ThrowJsonElementWrongTypeException(type); |
| | | 355 | | |
| | | 356 | | static bool ThrowJsonElementWrongTypeException(JsonTokenType actualType) |
| | 0 | 357 | | { |
| | 0 | 358 | | throw ThrowHelper.GetJsonElementWrongTypeException(nameof(Boolean), actualType.ToValueKind()); |
| | | 359 | | } |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Gets the value of the element as a <see cref="string"/>. |
| | | 364 | | /// </summary> |
| | | 365 | | /// <remarks> |
| | | 366 | | /// This method does not create a string representation of values other than JSON strings. |
| | | 367 | | /// </remarks> |
| | | 368 | | /// <returns>The value of the element as a <see cref="string"/>.</returns> |
| | | 369 | | /// <exception cref="InvalidOperationException"> |
| | | 370 | | /// This value's <see cref="ValueKind"/> is neither <see cref="JsonValueKind.String"/> nor <see cref="JsonValu |
| | | 371 | | /// </exception> |
| | | 372 | | /// <exception cref="ObjectDisposedException"> |
| | | 373 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 374 | | /// </exception> |
| | | 375 | | /// <seealso cref="ToString"/> |
| | | 376 | | public string? GetString() |
| | 0 | 377 | | { |
| | 0 | 378 | | CheckValidInstance(); |
| | | 379 | | |
| | 0 | 380 | | return _parent.GetString(_idx, JsonTokenType.String); |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// Attempts to represent the current JSON string as bytes assuming it is Base64 encoded. |
| | | 385 | | /// </summary> |
| | | 386 | | /// <param name="value">Receives the value.</param> |
| | | 387 | | /// <remarks> |
| | | 388 | | /// This method does not create a byte[] representation of values other than base 64 encoded JSON strings. |
| | | 389 | | /// </remarks> |
| | | 390 | | /// <returns> |
| | | 391 | | /// <see langword="true"/> if the entire token value is encoded as valid Base64 text and can be successfully d |
| | | 392 | | /// <see langword="false"/> otherwise. |
| | | 393 | | /// </returns> |
| | | 394 | | /// <exception cref="InvalidOperationException"> |
| | | 395 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 396 | | /// </exception> |
| | | 397 | | /// <exception cref="ObjectDisposedException"> |
| | | 398 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 399 | | /// </exception> |
| | | 400 | | public bool TryGetBytesFromBase64([NotNullWhen(true)] out byte[]? value) |
| | 0 | 401 | | { |
| | 0 | 402 | | CheckValidInstance(); |
| | | 403 | | |
| | 0 | 404 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 405 | | } |
| | | 406 | | |
| | | 407 | | /// <summary> |
| | | 408 | | /// Gets the value of the element as bytes. |
| | | 409 | | /// </summary> |
| | | 410 | | /// <remarks> |
| | | 411 | | /// This method does not create a byte[] representation of values other than Base64 encoded JSON strings. |
| | | 412 | | /// </remarks> |
| | | 413 | | /// <returns>The value decode to bytes.</returns> |
| | | 414 | | /// <exception cref="InvalidOperationException"> |
| | | 415 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 416 | | /// </exception> |
| | | 417 | | /// <exception cref="FormatException"> |
| | | 418 | | /// The value is not encoded as Base64 text and hence cannot be decoded to bytes. |
| | | 419 | | /// </exception> |
| | | 420 | | /// <exception cref="ObjectDisposedException"> |
| | | 421 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 422 | | /// </exception> |
| | | 423 | | /// <seealso cref="ToString"/> |
| | | 424 | | public byte[] GetBytesFromBase64() |
| | 0 | 425 | | { |
| | 0 | 426 | | if (!TryGetBytesFromBase64(out byte[]? value)) |
| | 0 | 427 | | { |
| | 0 | 428 | | ThrowHelper.ThrowFormatException(); |
| | | 429 | | } |
| | | 430 | | |
| | 0 | 431 | | return value; |
| | 0 | 432 | | } |
| | | 433 | | |
| | | 434 | | /// <summary> |
| | | 435 | | /// Attempts to represent the current JSON number as an <see cref="sbyte"/>. |
| | | 436 | | /// </summary> |
| | | 437 | | /// <param name="value">Receives the value.</param> |
| | | 438 | | /// <remarks> |
| | | 439 | | /// This method does not parse the contents of a JSON string value. |
| | | 440 | | /// </remarks> |
| | | 441 | | /// <returns> |
| | | 442 | | /// <see langword="true"/> if the number can be represented as an <see cref="sbyte"/>, |
| | | 443 | | /// <see langword="false"/> otherwise. |
| | | 444 | | /// </returns> |
| | | 445 | | /// <exception cref="InvalidOperationException"> |
| | | 446 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 447 | | /// </exception> |
| | | 448 | | /// <exception cref="ObjectDisposedException"> |
| | | 449 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 450 | | /// </exception> |
| | | 451 | | [CLSCompliant(false)] |
| | | 452 | | public bool TryGetSByte(out sbyte value) |
| | 0 | 453 | | { |
| | 0 | 454 | | CheckValidInstance(); |
| | | 455 | | |
| | 0 | 456 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 457 | | } |
| | | 458 | | |
| | | 459 | | /// <summary> |
| | | 460 | | /// Gets the current JSON number as an <see cref="sbyte"/>. |
| | | 461 | | /// </summary> |
| | | 462 | | /// <returns>The current JSON number as an <see cref="sbyte"/>.</returns> |
| | | 463 | | /// <exception cref="InvalidOperationException"> |
| | | 464 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 465 | | /// </exception> |
| | | 466 | | /// <exception cref="FormatException"> |
| | | 467 | | /// The value cannot be represented as an <see cref="sbyte"/>. |
| | | 468 | | /// </exception> |
| | | 469 | | /// <exception cref="ObjectDisposedException"> |
| | | 470 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 471 | | /// </exception> |
| | | 472 | | [CLSCompliant(false)] |
| | | 473 | | public sbyte GetSByte() |
| | 0 | 474 | | { |
| | 0 | 475 | | if (TryGetSByte(out sbyte value)) |
| | 0 | 476 | | { |
| | 0 | 477 | | return value; |
| | | 478 | | } |
| | | 479 | | |
| | 0 | 480 | | throw new FormatException(); |
| | 0 | 481 | | } |
| | | 482 | | |
| | | 483 | | /// <summary> |
| | | 484 | | /// Attempts to represent the current JSON number as a <see cref="byte"/>. |
| | | 485 | | /// </summary> |
| | | 486 | | /// <param name="value">Receives the value.</param> |
| | | 487 | | /// <remarks> |
| | | 488 | | /// This method does not parse the contents of a JSON string value. |
| | | 489 | | /// </remarks> |
| | | 490 | | /// <returns> |
| | | 491 | | /// <see langword="true"/> if the number can be represented as a <see cref="byte"/>, |
| | | 492 | | /// <see langword="false"/> otherwise. |
| | | 493 | | /// </returns> |
| | | 494 | | /// <exception cref="InvalidOperationException"> |
| | | 495 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 496 | | /// </exception> |
| | | 497 | | /// <exception cref="ObjectDisposedException"> |
| | | 498 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 499 | | /// </exception> |
| | | 500 | | public bool TryGetByte(out byte value) |
| | 0 | 501 | | { |
| | 0 | 502 | | CheckValidInstance(); |
| | | 503 | | |
| | 0 | 504 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 505 | | } |
| | | 506 | | |
| | | 507 | | /// <summary> |
| | | 508 | | /// Gets the current JSON number as a <see cref="byte"/>. |
| | | 509 | | /// </summary> |
| | | 510 | | /// <returns>The current JSON number as a <see cref="byte"/>.</returns> |
| | | 511 | | /// <remarks> |
| | | 512 | | /// This method does not parse the contents of a JSON string value. |
| | | 513 | | /// </remarks> |
| | | 514 | | /// <exception cref="InvalidOperationException"> |
| | | 515 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 516 | | /// </exception> |
| | | 517 | | /// <exception cref="FormatException"> |
| | | 518 | | /// The value cannot be represented as a <see cref="byte"/>. |
| | | 519 | | /// </exception> |
| | | 520 | | /// <exception cref="ObjectDisposedException"> |
| | | 521 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 522 | | /// </exception> |
| | | 523 | | public byte GetByte() |
| | 0 | 524 | | { |
| | 0 | 525 | | if (TryGetByte(out byte value)) |
| | 0 | 526 | | { |
| | 0 | 527 | | return value; |
| | | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | throw new FormatException(); |
| | 0 | 531 | | } |
| | | 532 | | |
| | | 533 | | /// <summary> |
| | | 534 | | /// Attempts to represent the current JSON number as an <see cref="short"/>. |
| | | 535 | | /// </summary> |
| | | 536 | | /// <param name="value">Receives the value.</param> |
| | | 537 | | /// <remarks> |
| | | 538 | | /// This method does not parse the contents of a JSON string value. |
| | | 539 | | /// </remarks> |
| | | 540 | | /// <returns> |
| | | 541 | | /// <see langword="true"/> if the number can be represented as an <see cref="short"/>, |
| | | 542 | | /// <see langword="false"/> otherwise. |
| | | 543 | | /// </returns> |
| | | 544 | | /// <exception cref="InvalidOperationException"> |
| | | 545 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 546 | | /// </exception> |
| | | 547 | | /// <exception cref="ObjectDisposedException"> |
| | | 548 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 549 | | /// </exception> |
| | | 550 | | public bool TryGetInt16(out short value) |
| | 0 | 551 | | { |
| | 0 | 552 | | CheckValidInstance(); |
| | | 553 | | |
| | 0 | 554 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// Gets the current JSON number as an <see cref="short"/>. |
| | | 559 | | /// </summary> |
| | | 560 | | /// <returns>The current JSON number as an <see cref="short"/>.</returns> |
| | | 561 | | /// <exception cref="InvalidOperationException"> |
| | | 562 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 563 | | /// </exception> |
| | | 564 | | /// <exception cref="FormatException"> |
| | | 565 | | /// The value cannot be represented as an <see cref="short"/>. |
| | | 566 | | /// </exception> |
| | | 567 | | /// <exception cref="ObjectDisposedException"> |
| | | 568 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 569 | | /// </exception> |
| | | 570 | | public short GetInt16() |
| | 0 | 571 | | { |
| | 0 | 572 | | if (TryGetInt16(out short value)) |
| | 0 | 573 | | { |
| | 0 | 574 | | return value; |
| | | 575 | | } |
| | | 576 | | |
| | 0 | 577 | | throw new FormatException(); |
| | 0 | 578 | | } |
| | | 579 | | |
| | | 580 | | /// <summary> |
| | | 581 | | /// Attempts to represent the current JSON number as a <see cref="ushort"/>. |
| | | 582 | | /// </summary> |
| | | 583 | | /// <param name="value">Receives the value.</param> |
| | | 584 | | /// <remarks> |
| | | 585 | | /// This method does not parse the contents of a JSON string value. |
| | | 586 | | /// </remarks> |
| | | 587 | | /// <returns> |
| | | 588 | | /// <see langword="true"/> if the number can be represented as a <see cref="ushort"/>, |
| | | 589 | | /// <see langword="false"/> otherwise. |
| | | 590 | | /// </returns> |
| | | 591 | | /// <exception cref="InvalidOperationException"> |
| | | 592 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 593 | | /// </exception> |
| | | 594 | | /// <exception cref="ObjectDisposedException"> |
| | | 595 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 596 | | /// </exception> |
| | | 597 | | [CLSCompliant(false)] |
| | | 598 | | public bool TryGetUInt16(out ushort value) |
| | 0 | 599 | | { |
| | 0 | 600 | | CheckValidInstance(); |
| | | 601 | | |
| | 0 | 602 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 603 | | } |
| | | 604 | | |
| | | 605 | | /// <summary> |
| | | 606 | | /// Gets the current JSON number as a <see cref="ushort"/>. |
| | | 607 | | /// </summary> |
| | | 608 | | /// <returns>The current JSON number as a <see cref="ushort"/>.</returns> |
| | | 609 | | /// <remarks> |
| | | 610 | | /// This method does not parse the contents of a JSON string value. |
| | | 611 | | /// </remarks> |
| | | 612 | | /// <exception cref="InvalidOperationException"> |
| | | 613 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 614 | | /// </exception> |
| | | 615 | | /// <exception cref="FormatException"> |
| | | 616 | | /// The value cannot be represented as a <see cref="ushort"/>. |
| | | 617 | | /// </exception> |
| | | 618 | | /// <exception cref="ObjectDisposedException"> |
| | | 619 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 620 | | /// </exception> |
| | | 621 | | [CLSCompliant(false)] |
| | | 622 | | public ushort GetUInt16() |
| | 0 | 623 | | { |
| | 0 | 624 | | if (TryGetUInt16(out ushort value)) |
| | 0 | 625 | | { |
| | 0 | 626 | | return value; |
| | | 627 | | } |
| | | 628 | | |
| | 0 | 629 | | throw new FormatException(); |
| | 0 | 630 | | } |
| | | 631 | | |
| | | 632 | | /// <summary> |
| | | 633 | | /// Attempts to represent the current JSON number as an <see cref="int"/>. |
| | | 634 | | /// </summary> |
| | | 635 | | /// <param name="value">Receives the value.</param> |
| | | 636 | | /// <remarks> |
| | | 637 | | /// This method does not parse the contents of a JSON string value. |
| | | 638 | | /// </remarks> |
| | | 639 | | /// <returns> |
| | | 640 | | /// <see langword="true"/> if the number can be represented as an <see cref="int"/>, |
| | | 641 | | /// <see langword="false"/> otherwise. |
| | | 642 | | /// </returns> |
| | | 643 | | /// <exception cref="InvalidOperationException"> |
| | | 644 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 645 | | /// </exception> |
| | | 646 | | /// <exception cref="ObjectDisposedException"> |
| | | 647 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 648 | | /// </exception> |
| | | 649 | | public bool TryGetInt32(out int value) |
| | 0 | 650 | | { |
| | 0 | 651 | | CheckValidInstance(); |
| | | 652 | | |
| | 0 | 653 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 654 | | } |
| | | 655 | | |
| | | 656 | | /// <summary> |
| | | 657 | | /// Gets the current JSON number as an <see cref="int"/>. |
| | | 658 | | /// </summary> |
| | | 659 | | /// <returns>The current JSON number as an <see cref="int"/>.</returns> |
| | | 660 | | /// <exception cref="InvalidOperationException"> |
| | | 661 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 662 | | /// </exception> |
| | | 663 | | /// <exception cref="FormatException"> |
| | | 664 | | /// The value cannot be represented as an <see cref="int"/>. |
| | | 665 | | /// </exception> |
| | | 666 | | /// <exception cref="ObjectDisposedException"> |
| | | 667 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 668 | | /// </exception> |
| | | 669 | | public int GetInt32() |
| | 0 | 670 | | { |
| | 0 | 671 | | if (!TryGetInt32(out int value)) |
| | 0 | 672 | | { |
| | 0 | 673 | | ThrowHelper.ThrowFormatException(); |
| | | 674 | | } |
| | | 675 | | |
| | 0 | 676 | | return value; |
| | 0 | 677 | | } |
| | | 678 | | |
| | | 679 | | /// <summary> |
| | | 680 | | /// Attempts to represent the current JSON number as a <see cref="uint"/>. |
| | | 681 | | /// </summary> |
| | | 682 | | /// <param name="value">Receives the value.</param> |
| | | 683 | | /// <remarks> |
| | | 684 | | /// This method does not parse the contents of a JSON string value. |
| | | 685 | | /// </remarks> |
| | | 686 | | /// <returns> |
| | | 687 | | /// <see langword="true"/> if the number can be represented as a <see cref="uint"/>, |
| | | 688 | | /// <see langword="false"/> otherwise. |
| | | 689 | | /// </returns> |
| | | 690 | | /// <exception cref="InvalidOperationException"> |
| | | 691 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 692 | | /// </exception> |
| | | 693 | | /// <exception cref="ObjectDisposedException"> |
| | | 694 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 695 | | /// </exception> |
| | | 696 | | [CLSCompliant(false)] |
| | | 697 | | public bool TryGetUInt32(out uint value) |
| | 0 | 698 | | { |
| | 0 | 699 | | CheckValidInstance(); |
| | | 700 | | |
| | 0 | 701 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 702 | | } |
| | | 703 | | |
| | | 704 | | /// <summary> |
| | | 705 | | /// Gets the current JSON number as a <see cref="uint"/>. |
| | | 706 | | /// </summary> |
| | | 707 | | /// <returns>The current JSON number as a <see cref="uint"/>.</returns> |
| | | 708 | | /// <remarks> |
| | | 709 | | /// This method does not parse the contents of a JSON string value. |
| | | 710 | | /// </remarks> |
| | | 711 | | /// <exception cref="InvalidOperationException"> |
| | | 712 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 713 | | /// </exception> |
| | | 714 | | /// <exception cref="FormatException"> |
| | | 715 | | /// The value cannot be represented as a <see cref="uint"/>. |
| | | 716 | | /// </exception> |
| | | 717 | | /// <exception cref="ObjectDisposedException"> |
| | | 718 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 719 | | /// </exception> |
| | | 720 | | [CLSCompliant(false)] |
| | | 721 | | public uint GetUInt32() |
| | 0 | 722 | | { |
| | 0 | 723 | | if (!TryGetUInt32(out uint value)) |
| | 0 | 724 | | { |
| | 0 | 725 | | ThrowHelper.ThrowFormatException(); |
| | | 726 | | } |
| | | 727 | | |
| | 0 | 728 | | return value; |
| | 0 | 729 | | } |
| | | 730 | | |
| | | 731 | | /// <summary> |
| | | 732 | | /// Attempts to represent the current JSON number as a <see cref="long"/>. |
| | | 733 | | /// </summary> |
| | | 734 | | /// <param name="value">Receives the value.</param> |
| | | 735 | | /// <remarks> |
| | | 736 | | /// This method does not parse the contents of a JSON string value. |
| | | 737 | | /// </remarks> |
| | | 738 | | /// <returns> |
| | | 739 | | /// <see langword="true"/> if the number can be represented as a <see cref="long"/>, |
| | | 740 | | /// <see langword="false"/> otherwise. |
| | | 741 | | /// </returns> |
| | | 742 | | /// <exception cref="InvalidOperationException"> |
| | | 743 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 744 | | /// </exception> |
| | | 745 | | /// <exception cref="ObjectDisposedException"> |
| | | 746 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 747 | | /// </exception> |
| | | 748 | | public bool TryGetInt64(out long value) |
| | 0 | 749 | | { |
| | 0 | 750 | | CheckValidInstance(); |
| | | 751 | | |
| | 0 | 752 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 753 | | } |
| | | 754 | | |
| | | 755 | | /// <summary> |
| | | 756 | | /// Gets the current JSON number as a <see cref="long"/>. |
| | | 757 | | /// </summary> |
| | | 758 | | /// <returns>The current JSON number as a <see cref="long"/>.</returns> |
| | | 759 | | /// <remarks> |
| | | 760 | | /// This method does not parse the contents of a JSON string value. |
| | | 761 | | /// </remarks> |
| | | 762 | | /// <exception cref="InvalidOperationException"> |
| | | 763 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 764 | | /// </exception> |
| | | 765 | | /// <exception cref="FormatException"> |
| | | 766 | | /// The value cannot be represented as a <see cref="long"/>. |
| | | 767 | | /// </exception> |
| | | 768 | | /// <exception cref="ObjectDisposedException"> |
| | | 769 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 770 | | /// </exception> |
| | | 771 | | public long GetInt64() |
| | 0 | 772 | | { |
| | 0 | 773 | | if (!TryGetInt64(out long value)) |
| | 0 | 774 | | { |
| | 0 | 775 | | ThrowHelper.ThrowFormatException(); |
| | | 776 | | } |
| | | 777 | | |
| | 0 | 778 | | return value; |
| | 0 | 779 | | } |
| | | 780 | | |
| | | 781 | | /// <summary> |
| | | 782 | | /// Attempts to represent the current JSON number as a <see cref="ulong"/>. |
| | | 783 | | /// </summary> |
| | | 784 | | /// <param name="value">Receives the value.</param> |
| | | 785 | | /// <remarks> |
| | | 786 | | /// This method does not parse the contents of a JSON string value. |
| | | 787 | | /// </remarks> |
| | | 788 | | /// <returns> |
| | | 789 | | /// <see langword="true"/> if the number can be represented as a <see cref="ulong"/>, |
| | | 790 | | /// <see langword="false"/> otherwise. |
| | | 791 | | /// </returns> |
| | | 792 | | /// <exception cref="InvalidOperationException"> |
| | | 793 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 794 | | /// </exception> |
| | | 795 | | /// <exception cref="ObjectDisposedException"> |
| | | 796 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 797 | | /// </exception> |
| | | 798 | | [CLSCompliant(false)] |
| | | 799 | | public bool TryGetUInt64(out ulong value) |
| | 0 | 800 | | { |
| | 0 | 801 | | CheckValidInstance(); |
| | | 802 | | |
| | 0 | 803 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 804 | | } |
| | | 805 | | |
| | | 806 | | /// <summary> |
| | | 807 | | /// Gets the current JSON number as a <see cref="ulong"/>. |
| | | 808 | | /// </summary> |
| | | 809 | | /// <returns>The current JSON number as a <see cref="ulong"/>.</returns> |
| | | 810 | | /// <remarks> |
| | | 811 | | /// This method does not parse the contents of a JSON string value. |
| | | 812 | | /// </remarks> |
| | | 813 | | /// <exception cref="InvalidOperationException"> |
| | | 814 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 815 | | /// </exception> |
| | | 816 | | /// <exception cref="FormatException"> |
| | | 817 | | /// The value cannot be represented as a <see cref="ulong"/>. |
| | | 818 | | /// </exception> |
| | | 819 | | /// <exception cref="ObjectDisposedException"> |
| | | 820 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 821 | | /// </exception> |
| | | 822 | | [CLSCompliant(false)] |
| | | 823 | | public ulong GetUInt64() |
| | 0 | 824 | | { |
| | 0 | 825 | | if (!TryGetUInt64(out ulong value)) |
| | 0 | 826 | | { |
| | 0 | 827 | | ThrowHelper.ThrowFormatException(); |
| | | 828 | | } |
| | | 829 | | |
| | 0 | 830 | | return value; |
| | 0 | 831 | | } |
| | | 832 | | |
| | | 833 | | /// <summary> |
| | | 834 | | /// Attempts to represent the current JSON number as a <see cref="double"/>. |
| | | 835 | | /// </summary> |
| | | 836 | | /// <param name="value">Receives the value.</param> |
| | | 837 | | /// <remarks> |
| | | 838 | | /// <para> |
| | | 839 | | /// This method does not parse the contents of a JSON string value. |
| | | 840 | | /// </para> |
| | | 841 | | /// |
| | | 842 | | /// <para> |
| | | 843 | | /// On .NET Core this method does not return <see langword="false"/> for values larger than |
| | | 844 | | /// <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>), |
| | | 845 | | /// instead <see langword="true"/> is returned and <see cref="double.PositiveInfinity"/> (or |
| | | 846 | | /// <see cref="double.NegativeInfinity"/>) is emitted. |
| | | 847 | | /// </para> |
| | | 848 | | /// </remarks> |
| | | 849 | | /// <returns> |
| | | 850 | | /// <see langword="true"/> if the number can be represented as a <see cref="double"/>, |
| | | 851 | | /// <see langword="false"/> otherwise. |
| | | 852 | | /// </returns> |
| | | 853 | | /// <exception cref="InvalidOperationException"> |
| | | 854 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 855 | | /// </exception> |
| | | 856 | | /// <exception cref="ObjectDisposedException"> |
| | | 857 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 858 | | /// </exception> |
| | | 859 | | public bool TryGetDouble(out double value) |
| | 0 | 860 | | { |
| | 0 | 861 | | CheckValidInstance(); |
| | | 862 | | |
| | 0 | 863 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 864 | | } |
| | | 865 | | |
| | | 866 | | /// <summary> |
| | | 867 | | /// Gets the current JSON number as a <see cref="double"/>. |
| | | 868 | | /// </summary> |
| | | 869 | | /// <returns>The current JSON number as a <see cref="double"/>.</returns> |
| | | 870 | | /// <remarks> |
| | | 871 | | /// <para> |
| | | 872 | | /// This method does not parse the contents of a JSON string value. |
| | | 873 | | /// </para> |
| | | 874 | | /// |
| | | 875 | | /// <para> |
| | | 876 | | /// On .NET Core this method returns <see cref="double.PositiveInfinity"/> (or |
| | | 877 | | /// <see cref="double.NegativeInfinity"/>) for values larger than |
| | | 878 | | /// <see cref="double.MaxValue"/> (or smaller than <see cref="double.MinValue"/>). |
| | | 879 | | /// </para> |
| | | 880 | | /// </remarks> |
| | | 881 | | /// <exception cref="InvalidOperationException"> |
| | | 882 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 883 | | /// </exception> |
| | | 884 | | /// <exception cref="FormatException"> |
| | | 885 | | /// The value cannot be represented as a <see cref="double"/>. |
| | | 886 | | /// </exception> |
| | | 887 | | /// <exception cref="ObjectDisposedException"> |
| | | 888 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 889 | | /// </exception> |
| | | 890 | | public double GetDouble() |
| | 0 | 891 | | { |
| | 0 | 892 | | if (!TryGetDouble(out double value)) |
| | 0 | 893 | | { |
| | 0 | 894 | | ThrowHelper.ThrowFormatException(); |
| | | 895 | | } |
| | | 896 | | |
| | 0 | 897 | | return value; |
| | 0 | 898 | | } |
| | | 899 | | |
| | | 900 | | /// <summary> |
| | | 901 | | /// Attempts to represent the current JSON number as a <see cref="float"/>. |
| | | 902 | | /// </summary> |
| | | 903 | | /// <param name="value">Receives the value.</param> |
| | | 904 | | /// <remarks> |
| | | 905 | | /// <para> |
| | | 906 | | /// This method does not parse the contents of a JSON string value. |
| | | 907 | | /// </para> |
| | | 908 | | /// |
| | | 909 | | /// <para> |
| | | 910 | | /// On .NET Core this method does not return <see langword="false"/> for values larger than |
| | | 911 | | /// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>), |
| | | 912 | | /// instead <see langword="true"/> is returned and <see cref="float.PositiveInfinity"/> (or |
| | | 913 | | /// <see cref="float.NegativeInfinity"/>) is emitted. |
| | | 914 | | /// </para> |
| | | 915 | | /// </remarks> |
| | | 916 | | /// <returns> |
| | | 917 | | /// <see langword="true"/> if the number can be represented as a <see cref="float"/>, |
| | | 918 | | /// <see langword="false"/> otherwise. |
| | | 919 | | /// </returns> |
| | | 920 | | /// <exception cref="InvalidOperationException"> |
| | | 921 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 922 | | /// </exception> |
| | | 923 | | /// <exception cref="ObjectDisposedException"> |
| | | 924 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 925 | | /// </exception> |
| | | 926 | | public bool TryGetSingle(out float value) |
| | 0 | 927 | | { |
| | 0 | 928 | | CheckValidInstance(); |
| | | 929 | | |
| | 0 | 930 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 931 | | } |
| | | 932 | | |
| | | 933 | | /// <summary> |
| | | 934 | | /// Gets the current JSON number as a <see cref="float"/>. |
| | | 935 | | /// </summary> |
| | | 936 | | /// <returns>The current JSON number as a <see cref="float"/>.</returns> |
| | | 937 | | /// <remarks> |
| | | 938 | | /// <para> |
| | | 939 | | /// This method does not parse the contents of a JSON string value. |
| | | 940 | | /// </para> |
| | | 941 | | /// |
| | | 942 | | /// <para> |
| | | 943 | | /// On .NET Core this method returns <see cref="float.PositiveInfinity"/> (or |
| | | 944 | | /// <see cref="float.NegativeInfinity"/>) for values larger than |
| | | 945 | | /// <see cref="float.MaxValue"/> (or smaller than <see cref="float.MinValue"/>). |
| | | 946 | | /// </para> |
| | | 947 | | /// </remarks> |
| | | 948 | | /// <exception cref="InvalidOperationException"> |
| | | 949 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 950 | | /// </exception> |
| | | 951 | | /// <exception cref="FormatException"> |
| | | 952 | | /// The value cannot be represented as a <see cref="float"/>. |
| | | 953 | | /// </exception> |
| | | 954 | | /// <exception cref="ObjectDisposedException"> |
| | | 955 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 956 | | /// </exception> |
| | | 957 | | public float GetSingle() |
| | 0 | 958 | | { |
| | 0 | 959 | | if (!TryGetSingle(out float value)) |
| | 0 | 960 | | { |
| | 0 | 961 | | ThrowHelper.ThrowFormatException(); |
| | | 962 | | } |
| | | 963 | | |
| | 0 | 964 | | return value; |
| | 0 | 965 | | } |
| | | 966 | | |
| | | 967 | | /// <summary> |
| | | 968 | | /// Attempts to represent the current JSON number as a <see cref="decimal"/>. |
| | | 969 | | /// </summary> |
| | | 970 | | /// <param name="value">Receives the value.</param> |
| | | 971 | | /// <remarks> |
| | | 972 | | /// This method does not parse the contents of a JSON string value. |
| | | 973 | | /// </remarks> |
| | | 974 | | /// <returns> |
| | | 975 | | /// <see langword="true"/> if the number can be represented as a <see cref="decimal"/>, |
| | | 976 | | /// <see langword="false"/> otherwise. |
| | | 977 | | /// </returns> |
| | | 978 | | /// <exception cref="InvalidOperationException"> |
| | | 979 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 980 | | /// </exception> |
| | | 981 | | /// <exception cref="ObjectDisposedException"> |
| | | 982 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 983 | | /// </exception> |
| | | 984 | | /// <seealso cref="GetRawText"/> |
| | | 985 | | public bool TryGetDecimal(out decimal value) |
| | 0 | 986 | | { |
| | 0 | 987 | | CheckValidInstance(); |
| | | 988 | | |
| | 0 | 989 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 990 | | } |
| | | 991 | | |
| | | 992 | | /// <summary> |
| | | 993 | | /// Gets the current JSON number as a <see cref="decimal"/>. |
| | | 994 | | /// </summary> |
| | | 995 | | /// <returns>The current JSON number as a <see cref="decimal"/>.</returns> |
| | | 996 | | /// <remarks> |
| | | 997 | | /// This method does not parse the contents of a JSON string value. |
| | | 998 | | /// </remarks> |
| | | 999 | | /// <exception cref="InvalidOperationException"> |
| | | 1000 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Number"/>. |
| | | 1001 | | /// </exception> |
| | | 1002 | | /// <exception cref="FormatException"> |
| | | 1003 | | /// The value cannot be represented as a <see cref="decimal"/>. |
| | | 1004 | | /// </exception> |
| | | 1005 | | /// <exception cref="ObjectDisposedException"> |
| | | 1006 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1007 | | /// </exception> |
| | | 1008 | | /// <seealso cref="GetRawText"/> |
| | | 1009 | | public decimal GetDecimal() |
| | 0 | 1010 | | { |
| | 0 | 1011 | | if (!TryGetDecimal(out decimal value)) |
| | 0 | 1012 | | { |
| | 0 | 1013 | | ThrowHelper.ThrowFormatException(); |
| | | 1014 | | } |
| | | 1015 | | |
| | 0 | 1016 | | return value; |
| | 0 | 1017 | | } |
| | | 1018 | | |
| | | 1019 | | /// <summary> |
| | | 1020 | | /// Attempts to represent the current JSON string as a <see cref="DateTime"/>. |
| | | 1021 | | /// </summary> |
| | | 1022 | | /// <param name="value">Receives the value.</param> |
| | | 1023 | | /// <remarks> |
| | | 1024 | | /// This method does not create a DateTime representation of values other than JSON strings. |
| | | 1025 | | /// </remarks> |
| | | 1026 | | /// <returns> |
| | | 1027 | | /// <see langword="true"/> if the string can be represented as a <see cref="DateTime"/>, |
| | | 1028 | | /// <see langword="false"/> otherwise. |
| | | 1029 | | /// </returns> |
| | | 1030 | | /// <exception cref="InvalidOperationException"> |
| | | 1031 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1032 | | /// </exception> |
| | | 1033 | | /// <exception cref="ObjectDisposedException"> |
| | | 1034 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1035 | | /// </exception> |
| | | 1036 | | public bool TryGetDateTime(out DateTime value) |
| | 0 | 1037 | | { |
| | 0 | 1038 | | CheckValidInstance(); |
| | | 1039 | | |
| | 0 | 1040 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 1041 | | } |
| | | 1042 | | |
| | | 1043 | | /// <summary> |
| | | 1044 | | /// Gets the value of the element as a <see cref="DateTime"/>. |
| | | 1045 | | /// </summary> |
| | | 1046 | | /// <remarks> |
| | | 1047 | | /// This method does not create a DateTime representation of values other than JSON strings. |
| | | 1048 | | /// </remarks> |
| | | 1049 | | /// <returns>The value of the element as a <see cref="DateTime"/>.</returns> |
| | | 1050 | | /// <exception cref="InvalidOperationException"> |
| | | 1051 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1052 | | /// </exception> |
| | | 1053 | | /// <exception cref="FormatException"> |
| | | 1054 | | /// The value cannot be represented as a <see cref="DateTime"/>. |
| | | 1055 | | /// </exception> |
| | | 1056 | | /// <exception cref="ObjectDisposedException"> |
| | | 1057 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1058 | | /// </exception> |
| | | 1059 | | /// <seealso cref="ToString"/> |
| | | 1060 | | public DateTime GetDateTime() |
| | 0 | 1061 | | { |
| | 0 | 1062 | | if (!TryGetDateTime(out DateTime value)) |
| | 0 | 1063 | | { |
| | 0 | 1064 | | ThrowHelper.ThrowFormatException(); |
| | | 1065 | | } |
| | | 1066 | | |
| | 0 | 1067 | | return value; |
| | 0 | 1068 | | } |
| | | 1069 | | |
| | | 1070 | | /// <summary> |
| | | 1071 | | /// Attempts to represent the current JSON string as a <see cref="DateTimeOffset"/>. |
| | | 1072 | | /// </summary> |
| | | 1073 | | /// <param name="value">Receives the value.</param> |
| | | 1074 | | /// <remarks> |
| | | 1075 | | /// This method does not create a DateTimeOffset representation of values other than JSON strings. |
| | | 1076 | | /// </remarks> |
| | | 1077 | | /// <returns> |
| | | 1078 | | /// <see langword="true"/> if the string can be represented as a <see cref="DateTimeOffset"/>, |
| | | 1079 | | /// <see langword="false"/> otherwise. |
| | | 1080 | | /// </returns> |
| | | 1081 | | /// <exception cref="InvalidOperationException"> |
| | | 1082 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1083 | | /// </exception> |
| | | 1084 | | /// <exception cref="ObjectDisposedException"> |
| | | 1085 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1086 | | /// </exception> |
| | | 1087 | | public bool TryGetDateTimeOffset(out DateTimeOffset value) |
| | 0 | 1088 | | { |
| | 0 | 1089 | | CheckValidInstance(); |
| | | 1090 | | |
| | 0 | 1091 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 1092 | | } |
| | | 1093 | | |
| | | 1094 | | /// <summary> |
| | | 1095 | | /// Gets the value of the element as a <see cref="DateTimeOffset"/>. |
| | | 1096 | | /// </summary> |
| | | 1097 | | /// <remarks> |
| | | 1098 | | /// This method does not create a DateTimeOffset representation of values other than JSON strings. |
| | | 1099 | | /// </remarks> |
| | | 1100 | | /// <returns>The value of the element as a <see cref="DateTimeOffset"/>.</returns> |
| | | 1101 | | /// <exception cref="InvalidOperationException"> |
| | | 1102 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1103 | | /// </exception> |
| | | 1104 | | /// <exception cref="FormatException"> |
| | | 1105 | | /// The value cannot be represented as a <see cref="DateTimeOffset"/>. |
| | | 1106 | | /// </exception> |
| | | 1107 | | /// <exception cref="ObjectDisposedException"> |
| | | 1108 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1109 | | /// </exception> |
| | | 1110 | | /// <seealso cref="ToString"/> |
| | | 1111 | | public DateTimeOffset GetDateTimeOffset() |
| | 0 | 1112 | | { |
| | 0 | 1113 | | if (!TryGetDateTimeOffset(out DateTimeOffset value)) |
| | 0 | 1114 | | { |
| | 0 | 1115 | | ThrowHelper.ThrowFormatException(); |
| | | 1116 | | } |
| | | 1117 | | |
| | 0 | 1118 | | return value; |
| | 0 | 1119 | | } |
| | | 1120 | | |
| | | 1121 | | /// <summary> |
| | | 1122 | | /// Attempts to represent the current JSON string as a <see cref="Guid"/>. |
| | | 1123 | | /// </summary> |
| | | 1124 | | /// <param name="value">Receives the value.</param> |
| | | 1125 | | /// <remarks> |
| | | 1126 | | /// This method does not create a Guid representation of values other than JSON strings. |
| | | 1127 | | /// </remarks> |
| | | 1128 | | /// <returns> |
| | | 1129 | | /// <see langword="true"/> if the string can be represented as a <see cref="Guid"/>, |
| | | 1130 | | /// <see langword="false"/> otherwise. |
| | | 1131 | | /// </returns> |
| | | 1132 | | /// <exception cref="InvalidOperationException"> |
| | | 1133 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1134 | | /// </exception> |
| | | 1135 | | /// <exception cref="ObjectDisposedException"> |
| | | 1136 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1137 | | /// </exception> |
| | | 1138 | | public bool TryGetGuid(out Guid value) |
| | 0 | 1139 | | { |
| | 0 | 1140 | | CheckValidInstance(); |
| | | 1141 | | |
| | 0 | 1142 | | return _parent.TryGetValue(_idx, out value); |
| | 0 | 1143 | | } |
| | | 1144 | | |
| | | 1145 | | /// <summary> |
| | | 1146 | | /// Gets the value of the element as a <see cref="Guid"/>. |
| | | 1147 | | /// </summary> |
| | | 1148 | | /// <remarks> |
| | | 1149 | | /// This method does not create a Guid representation of values other than JSON strings. |
| | | 1150 | | /// </remarks> |
| | | 1151 | | /// <returns>The value of the element as a <see cref="Guid"/>.</returns> |
| | | 1152 | | /// <exception cref="InvalidOperationException"> |
| | | 1153 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1154 | | /// </exception> |
| | | 1155 | | /// <exception cref="FormatException"> |
| | | 1156 | | /// The value cannot be represented as a <see cref="Guid"/>. |
| | | 1157 | | /// </exception> |
| | | 1158 | | /// <exception cref="ObjectDisposedException"> |
| | | 1159 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1160 | | /// </exception> |
| | | 1161 | | /// <seealso cref="ToString"/> |
| | | 1162 | | public Guid GetGuid() |
| | 0 | 1163 | | { |
| | 0 | 1164 | | if (!TryGetGuid(out Guid value)) |
| | 0 | 1165 | | { |
| | 0 | 1166 | | ThrowHelper.ThrowFormatException(); |
| | | 1167 | | } |
| | | 1168 | | |
| | 0 | 1169 | | return value; |
| | 0 | 1170 | | } |
| | | 1171 | | |
| | | 1172 | | internal string GetPropertyName() |
| | 0 | 1173 | | { |
| | 0 | 1174 | | CheckValidInstance(); |
| | | 1175 | | |
| | 0 | 1176 | | return _parent.GetNameOfPropertyValue(_idx); |
| | 0 | 1177 | | } |
| | | 1178 | | |
| | | 1179 | | internal ReadOnlySpan<byte> GetPropertyNameRaw() |
| | 0 | 1180 | | { |
| | 0 | 1181 | | CheckValidInstance(); |
| | | 1182 | | |
| | 0 | 1183 | | return _parent.GetPropertyNameRaw(_idx); |
| | 0 | 1184 | | } |
| | | 1185 | | |
| | | 1186 | | /// <summary> |
| | | 1187 | | /// Gets the original input data backing this value, returning it as a <see cref="string"/>. |
| | | 1188 | | /// </summary> |
| | | 1189 | | /// <returns> |
| | | 1190 | | /// The original input data backing this value, returning it as a <see cref="string"/>. |
| | | 1191 | | /// </returns> |
| | | 1192 | | /// <exception cref="ObjectDisposedException"> |
| | | 1193 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1194 | | /// </exception> |
| | | 1195 | | public string GetRawText() |
| | 0 | 1196 | | { |
| | 0 | 1197 | | CheckValidInstance(); |
| | | 1198 | | |
| | 0 | 1199 | | return _parent.GetRawValueAsString(_idx); |
| | 0 | 1200 | | } |
| | | 1201 | | |
| | | 1202 | | internal ReadOnlyMemory<byte> GetRawValue() |
| | 70 | 1203 | | { |
| | 70 | 1204 | | CheckValidInstance(); |
| | | 1205 | | |
| | 70 | 1206 | | return _parent.GetRawValue(_idx, includeQuotes: true); |
| | 70 | 1207 | | } |
| | | 1208 | | |
| | | 1209 | | internal string GetPropertyRawText() |
| | 0 | 1210 | | { |
| | 0 | 1211 | | CheckValidInstance(); |
| | | 1212 | | |
| | 0 | 1213 | | return _parent.GetPropertyRawValueAsString(_idx); |
| | 0 | 1214 | | } |
| | | 1215 | | |
| | | 1216 | | internal bool ValueIsEscaped |
| | | 1217 | | { |
| | | 1218 | | get |
| | 3 | 1219 | | { |
| | 3 | 1220 | | CheckValidInstance(); |
| | | 1221 | | |
| | 3 | 1222 | | return _parent.ValueIsEscaped(_idx, isPropertyName: false); |
| | 3 | 1223 | | } |
| | | 1224 | | } |
| | | 1225 | | |
| | | 1226 | | internal ReadOnlySpan<byte> ValueSpan |
| | | 1227 | | { |
| | | 1228 | | get |
| | 3 | 1229 | | { |
| | 3 | 1230 | | CheckValidInstance(); |
| | | 1231 | | |
| | 3 | 1232 | | return _parent.GetRawValue(_idx, includeQuotes: false).Span; |
| | 3 | 1233 | | } |
| | | 1234 | | } |
| | | 1235 | | |
| | | 1236 | | /// <summary> |
| | | 1237 | | /// Compares the values of two <see cref="JsonElement"/> values for equality, including the values of all descen |
| | | 1238 | | /// </summary> |
| | | 1239 | | /// <param name="element1">The first <see cref="JsonElement"/> to compare.</param> |
| | | 1240 | | /// <param name="element2">The second <see cref="JsonElement"/> to compare.</param> |
| | | 1241 | | /// <returns><see langword="true"/> if the two values are equal; otherwise, <see langword="false"/>.</returns> |
| | | 1242 | | /// <remarks> |
| | | 1243 | | /// Deep equality of two JSON values is defined as follows: |
| | | 1244 | | /// <list type="bullet"> |
| | | 1245 | | /// <item>JSON values of different kinds are not equal.</item> |
| | | 1246 | | /// <item>JSON constants <see langword="null"/>, <see langword="false"/>, and <see langword="true"/> only equal |
| | | 1247 | | /// <item>JSON numbers are equal if and only if they have they have equivalent decimal representations, with no |
| | | 1248 | | /// <item>JSON strings are equal if and only if they are equal using ordinal string comparison.</item> |
| | | 1249 | | /// <item>JSON arrays are equal if and only if they are of equal length and each of their elements are pairwise |
| | | 1250 | | /// <item> |
| | | 1251 | | /// JSON objects are equal if and only if they have the same number of properties and each property in the f |
| | | 1252 | | /// has a corresponding property in the second object with the same name and equal value. The order of prope |
| | | 1253 | | /// significant, with the exception of repeated properties that must be specified in the same order (with in |
| | | 1254 | | /// </item> |
| | | 1255 | | /// </list> |
| | | 1256 | | /// </remarks> |
| | | 1257 | | public static bool DeepEquals(JsonElement element1, JsonElement element2) |
| | 38 | 1258 | | { |
| | 38 | 1259 | | if (!StackHelper.TryEnsureSufficientExecutionStack()) |
| | 0 | 1260 | | { |
| | 0 | 1261 | | ThrowHelper.ThrowInsufficientExecutionStackException_JsonElementDeepEqualsInsufficientExecutionStack(); |
| | | 1262 | | } |
| | | 1263 | | |
| | 38 | 1264 | | element1.CheckValidInstance(); |
| | 38 | 1265 | | element2.CheckValidInstance(); |
| | | 1266 | | |
| | 38 | 1267 | | JsonValueKind kind = element1.ValueKind; |
| | 38 | 1268 | | if (kind != element2.ValueKind) |
| | 0 | 1269 | | { |
| | 0 | 1270 | | return false; |
| | | 1271 | | } |
| | | 1272 | | |
| | 38 | 1273 | | switch (kind) |
| | | 1274 | | { |
| | | 1275 | | case JsonValueKind.Null or JsonValueKind.False or JsonValueKind.True: |
| | 0 | 1276 | | return true; |
| | | 1277 | | |
| | | 1278 | | case JsonValueKind.Number: |
| | 35 | 1279 | | return JsonHelpers.AreEqualJsonNumbers(element1.GetRawValue().Span, element2.GetRawValue().Span); |
| | | 1280 | | |
| | | 1281 | | case JsonValueKind.String: |
| | 3 | 1282 | | if (element2.ValueIsEscaped) |
| | 0 | 1283 | | { |
| | 0 | 1284 | | if (element1.ValueIsEscaped) |
| | 0 | 1285 | | { |
| | | 1286 | | // Need to unescape and compare both inputs. |
| | 0 | 1287 | | return JsonReaderHelper.UnescapeAndCompareBothInputs(element1.ValueSpan, element2.ValueSpan) |
| | | 1288 | | } |
| | | 1289 | | |
| | | 1290 | | // Swap values so that unescaping is handled by the LHS. |
| | 0 | 1291 | | (element1, element2) = (element2, element1); |
| | 0 | 1292 | | } |
| | | 1293 | | |
| | 3 | 1294 | | return element1.ValueEquals(element2.ValueSpan); |
| | | 1295 | | |
| | | 1296 | | case JsonValueKind.Array: |
| | 0 | 1297 | | if (element1.GetArrayLength() != element2.GetArrayLength()) |
| | 0 | 1298 | | { |
| | 0 | 1299 | | return false; |
| | | 1300 | | } |
| | | 1301 | | |
| | 0 | 1302 | | ArrayEnumerator arrayEnumerator2 = element2.EnumerateArray(); |
| | 0 | 1303 | | foreach (JsonElement e1 in element1.EnumerateArray()) |
| | 0 | 1304 | | { |
| | 0 | 1305 | | bool success = arrayEnumerator2.MoveNext(); |
| | 0 | 1306 | | Debug.Assert(success, "enumerators must have matching length"); |
| | | 1307 | | |
| | 0 | 1308 | | if (!DeepEquals(e1, arrayEnumerator2.Current)) |
| | 0 | 1309 | | { |
| | 0 | 1310 | | return false; |
| | | 1311 | | } |
| | 0 | 1312 | | } |
| | | 1313 | | |
| | 0 | 1314 | | Debug.Assert(!arrayEnumerator2.MoveNext()); |
| | 0 | 1315 | | return true; |
| | | 1316 | | |
| | | 1317 | | default: |
| | 0 | 1318 | | Debug.Assert(kind is JsonValueKind.Object); |
| | | 1319 | | |
| | 0 | 1320 | | int count = element1.GetPropertyCount(); |
| | 0 | 1321 | | if (count != element2.GetPropertyCount()) |
| | 0 | 1322 | | { |
| | 0 | 1323 | | return false; |
| | | 1324 | | } |
| | | 1325 | | |
| | 0 | 1326 | | ObjectEnumerator objectEnumerator1 = element1.EnumerateObject(); |
| | 0 | 1327 | | ObjectEnumerator objectEnumerator2 = element2.EnumerateObject(); |
| | | 1328 | | |
| | | 1329 | | // Two JSON objects are considered equal if they define the same set of properties. |
| | | 1330 | | // Start optimistically with pairwise comparison, but fall back to unordered |
| | | 1331 | | // comparison as soon as a mismatch is encountered. |
| | | 1332 | | |
| | 0 | 1333 | | while (objectEnumerator1.MoveNext()) |
| | 0 | 1334 | | { |
| | 0 | 1335 | | bool success = objectEnumerator2.MoveNext(); |
| | 0 | 1336 | | Debug.Assert(success, "enumerators should have matching lengths"); |
| | | 1337 | | |
| | 0 | 1338 | | JsonProperty prop1 = objectEnumerator1.Current; |
| | 0 | 1339 | | JsonProperty prop2 = objectEnumerator2.Current; |
| | | 1340 | | |
| | 0 | 1341 | | if (!NameEquals(prop1, prop2)) |
| | 0 | 1342 | | { |
| | | 1343 | | // We have our first mismatch, fall back to unordered comparison. |
| | 0 | 1344 | | return UnorderedObjectDeepEquals(objectEnumerator1, objectEnumerator2, remainingProps: count |
| | | 1345 | | } |
| | | 1346 | | |
| | 0 | 1347 | | if (!DeepEquals(prop1.Value, prop2.Value)) |
| | 0 | 1348 | | { |
| | 0 | 1349 | | return false; |
| | | 1350 | | } |
| | | 1351 | | |
| | 0 | 1352 | | count--; |
| | 0 | 1353 | | } |
| | | 1354 | | |
| | 0 | 1355 | | Debug.Assert(!objectEnumerator2.MoveNext()); |
| | 0 | 1356 | | return true; |
| | | 1357 | | |
| | | 1358 | | static bool UnorderedObjectDeepEquals(ObjectEnumerator objectEnumerator1, ObjectEnumerator objectEnu |
| | 0 | 1359 | | { |
| | | 1360 | | // JsonElement objects allow duplicate property names, which is optional per the JSON RFC. |
| | | 1361 | | // Even though this implementation of equality does not take property ordering into account, |
| | | 1362 | | // repeated property names must be specified in the same order (although they may be interleaved |
| | | 1363 | | // This is to preserve a degree of coherence with JSON serialization, where either the first |
| | | 1364 | | // or last occurrence of a repeated property name is used. It also simplifies the implementation |
| | | 1365 | | // and keeps it at O(n + m) complexity. |
| | | 1366 | | |
| | 0 | 1367 | | Dictionary<string, ValueQueue<JsonElement>> properties2 = new(capacity: remainingProps, StringCo |
| | | 1368 | | do |
| | 0 | 1369 | | { |
| | 0 | 1370 | | JsonProperty prop2 = objectEnumerator2.Current; |
| | | 1371 | | #if NET |
| | 0 | 1372 | | ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti |
| | | 1373 | | #else |
| | | 1374 | | properties2.TryGetValue(prop2.Name, out ValueQueue<JsonElement> values); |
| | | 1375 | | #endif |
| | 0 | 1376 | | values.Enqueue(prop2.Value); |
| | | 1377 | | #if !NET |
| | | 1378 | | properties2[prop2.Name] = values; |
| | | 1379 | | #endif |
| | 0 | 1380 | | } |
| | 0 | 1381 | | while (objectEnumerator2.MoveNext()); |
| | | 1382 | | |
| | | 1383 | | do |
| | 0 | 1384 | | { |
| | 0 | 1385 | | JsonProperty prop = objectEnumerator1.Current; |
| | | 1386 | | #if NET |
| | 0 | 1387 | | ref ValueQueue<JsonElement> values = ref CollectionsMarshal.GetValueRefOrAddDefault(properti |
| | | 1388 | | #else |
| | | 1389 | | bool exists = properties2.TryGetValue(prop.Name, out ValueQueue<JsonElement> values); |
| | | 1390 | | #endif |
| | 0 | 1391 | | if (!exists || !values.TryDequeue(out JsonElement value) || !DeepEquals(prop.Value, value)) |
| | 0 | 1392 | | { |
| | 0 | 1393 | | return false; |
| | | 1394 | | } |
| | | 1395 | | #if !NET |
| | | 1396 | | properties2[prop.Name] = values; |
| | | 1397 | | #endif |
| | 0 | 1398 | | } |
| | 0 | 1399 | | while (objectEnumerator1.MoveNext()); |
| | | 1400 | | |
| | 0 | 1401 | | return true; |
| | 0 | 1402 | | } |
| | | 1403 | | |
| | | 1404 | | static bool NameEquals(JsonProperty left, JsonProperty right) |
| | 0 | 1405 | | { |
| | 0 | 1406 | | if (right.NameIsEscaped) |
| | 0 | 1407 | | { |
| | 0 | 1408 | | if (left.NameIsEscaped) |
| | 0 | 1409 | | { |
| | | 1410 | | // Need to unescape and compare both inputs. |
| | 0 | 1411 | | return JsonReaderHelper.UnescapeAndCompareBothInputs(left.NameSpan, right.NameSpan); |
| | | 1412 | | } |
| | | 1413 | | |
| | | 1414 | | // Swap values so that unescaping is handled by the LHS |
| | 0 | 1415 | | (left, right) = (right, left); |
| | 0 | 1416 | | } |
| | | 1417 | | |
| | 0 | 1418 | | return left.NameEquals(right.NameSpan); |
| | 0 | 1419 | | } |
| | | 1420 | | } |
| | 38 | 1421 | | } |
| | | 1422 | | |
| | | 1423 | | /// <summary> |
| | | 1424 | | /// Compares <paramref name="text" /> to the string value of this element. |
| | | 1425 | | /// </summary> |
| | | 1426 | | /// <param name="text">The text to compare against.</param> |
| | | 1427 | | /// <returns> |
| | | 1428 | | /// <see langword="true" /> if the string value of this element matches <paramref name="text"/>, |
| | | 1429 | | /// <see langword="false" /> otherwise. |
| | | 1430 | | /// </returns> |
| | | 1431 | | /// <exception cref="InvalidOperationException"> |
| | | 1432 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1433 | | /// </exception> |
| | | 1434 | | /// <remarks> |
| | | 1435 | | /// This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and |
| | | 1436 | | /// the result of calling <see cref="GetString" />, but avoids creating the string instance. |
| | | 1437 | | /// </remarks> |
| | | 1438 | | public bool ValueEquals(string? text) |
| | 0 | 1439 | | { |
| | | 1440 | | // CheckValidInstance is done in the helper |
| | | 1441 | | |
| | 0 | 1442 | | if (TokenType == JsonTokenType.Null) |
| | 0 | 1443 | | { |
| | 0 | 1444 | | return text == null; |
| | | 1445 | | } |
| | | 1446 | | |
| | 0 | 1447 | | return TextEqualsHelper(text.AsSpan(), isPropertyName: false); |
| | 0 | 1448 | | } |
| | | 1449 | | |
| | | 1450 | | /// <summary> |
| | | 1451 | | /// Compares the text represented by <paramref name="utf8Text" /> to the string value of this element. |
| | | 1452 | | /// </summary> |
| | | 1453 | | /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param> |
| | | 1454 | | /// <returns> |
| | | 1455 | | /// <see langword="true" /> if the string value of this element has the same UTF-8 encoding as |
| | | 1456 | | /// <paramref name="utf8Text" />, <see langword="false" /> otherwise. |
| | | 1457 | | /// </returns> |
| | | 1458 | | /// <exception cref="InvalidOperationException"> |
| | | 1459 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1460 | | /// </exception> |
| | | 1461 | | /// <remarks> |
| | | 1462 | | /// This method is functionally equal to doing an ordinal comparison of the string produced by UTF-8 decoding |
| | | 1463 | | /// <paramref name="utf8Text" /> with the result of calling <see cref="GetString" />, but avoids creating the |
| | | 1464 | | /// string instances. |
| | | 1465 | | /// </remarks> |
| | | 1466 | | public bool ValueEquals(ReadOnlySpan<byte> utf8Text) |
| | 3 | 1467 | | { |
| | | 1468 | | // CheckValidInstance is done in the helper |
| | | 1469 | | |
| | 3 | 1470 | | if (TokenType == JsonTokenType.Null) |
| | 0 | 1471 | | { |
| | | 1472 | | // This is different than Length == 0, in that it tests true for null, but false for "" |
| | | 1473 | | #pragma warning disable CA2265 |
| | 0 | 1474 | | return utf8Text.Slice(0, 0) == default; |
| | | 1475 | | #pragma warning restore CA2265 |
| | | 1476 | | } |
| | | 1477 | | |
| | 3 | 1478 | | return TextEqualsHelper(utf8Text, isPropertyName: false, shouldUnescape: true); |
| | 3 | 1479 | | } |
| | | 1480 | | |
| | | 1481 | | /// <summary> |
| | | 1482 | | /// Compares <paramref name="text" /> to the string value of this element. |
| | | 1483 | | /// </summary> |
| | | 1484 | | /// <param name="text">The text to compare against.</param> |
| | | 1485 | | /// <returns> |
| | | 1486 | | /// <see langword="true" /> if the string value of this element matches <paramref name="text"/>, |
| | | 1487 | | /// <see langword="false" /> otherwise. |
| | | 1488 | | /// </returns> |
| | | 1489 | | /// <exception cref="InvalidOperationException"> |
| | | 1490 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.String"/>. |
| | | 1491 | | /// </exception> |
| | | 1492 | | /// <remarks> |
| | | 1493 | | /// This method is functionally equal to doing an ordinal comparison of <paramref name="text" /> and |
| | | 1494 | | /// the result of calling <see cref="GetString" />, but avoids creating the string instance. |
| | | 1495 | | /// </remarks> |
| | | 1496 | | public bool ValueEquals(ReadOnlySpan<char> text) |
| | 0 | 1497 | | { |
| | | 1498 | | // CheckValidInstance is done in the helper |
| | | 1499 | | |
| | 0 | 1500 | | if (TokenType == JsonTokenType.Null) |
| | 0 | 1501 | | { |
| | | 1502 | | // This is different than Length == 0, in that it tests true for null, but false for "" |
| | | 1503 | | #pragma warning disable CA2265 |
| | 0 | 1504 | | return text.Slice(0, 0) == default; |
| | | 1505 | | #pragma warning restore CA2265 |
| | | 1506 | | } |
| | | 1507 | | |
| | 0 | 1508 | | return TextEqualsHelper(text, isPropertyName: false); |
| | 0 | 1509 | | } |
| | | 1510 | | |
| | | 1511 | | internal bool TextEqualsHelper(ReadOnlySpan<byte> utf8Text, bool isPropertyName, bool shouldUnescape) |
| | 3 | 1512 | | { |
| | 3 | 1513 | | CheckValidInstance(); |
| | | 1514 | | |
| | 3 | 1515 | | return _parent.TextEquals(_idx, utf8Text, isPropertyName, shouldUnescape); |
| | 3 | 1516 | | } |
| | | 1517 | | |
| | | 1518 | | internal bool TextEqualsHelper(ReadOnlySpan<char> text, bool isPropertyName) |
| | 0 | 1519 | | { |
| | 0 | 1520 | | CheckValidInstance(); |
| | | 1521 | | |
| | 0 | 1522 | | return _parent.TextEquals(_idx, text, isPropertyName); |
| | 0 | 1523 | | } |
| | | 1524 | | |
| | | 1525 | | internal bool ValueIsEscapedHelper(bool isPropertyName) |
| | 0 | 1526 | | { |
| | 0 | 1527 | | CheckValidInstance(); |
| | | 1528 | | |
| | 0 | 1529 | | return _parent.ValueIsEscaped(_idx, isPropertyName); |
| | 0 | 1530 | | } |
| | | 1531 | | |
| | | 1532 | | /// <summary> |
| | | 1533 | | /// Write the element into the provided writer as a JSON value. |
| | | 1534 | | /// </summary> |
| | | 1535 | | /// <param name="writer">The writer.</param> |
| | | 1536 | | /// <exception cref="ArgumentNullException"> |
| | | 1537 | | /// The <paramref name="writer"/> parameter is <see langword="null"/>. |
| | | 1538 | | /// </exception> |
| | | 1539 | | /// <exception cref="InvalidOperationException"> |
| | | 1540 | | /// This value's <see cref="ValueKind"/> is <see cref="JsonValueKind.Undefined"/>. |
| | | 1541 | | /// </exception> |
| | | 1542 | | /// <exception cref="ObjectDisposedException"> |
| | | 1543 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1544 | | /// </exception> |
| | | 1545 | | public void WriteTo(Utf8JsonWriter writer) |
| | 0 | 1546 | | { |
| | 0 | 1547 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 1548 | | |
| | 0 | 1549 | | CheckValidInstance(); |
| | | 1550 | | |
| | 0 | 1551 | | _parent.WriteElementTo(_idx, writer); |
| | 0 | 1552 | | } |
| | | 1553 | | |
| | | 1554 | | internal void WritePropertyNameTo(Utf8JsonWriter writer) |
| | 0 | 1555 | | { |
| | 0 | 1556 | | CheckValidInstance(); |
| | | 1557 | | |
| | 0 | 1558 | | _parent.WritePropertyName(_idx, writer); |
| | 0 | 1559 | | } |
| | | 1560 | | |
| | | 1561 | | /// <summary> |
| | | 1562 | | /// Get an enumerator to enumerate the values in the JSON array represented by this JsonElement. |
| | | 1563 | | /// </summary> |
| | | 1564 | | /// <returns> |
| | | 1565 | | /// An enumerator to enumerate the values in the JSON array represented by this JsonElement. |
| | | 1566 | | /// </returns> |
| | | 1567 | | /// <exception cref="InvalidOperationException"> |
| | | 1568 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Array"/>. |
| | | 1569 | | /// </exception> |
| | | 1570 | | /// <exception cref="ObjectDisposedException"> |
| | | 1571 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1572 | | /// </exception> |
| | | 1573 | | public ArrayEnumerator EnumerateArray() |
| | 0 | 1574 | | { |
| | 0 | 1575 | | CheckValidInstance(); |
| | | 1576 | | |
| | 0 | 1577 | | JsonTokenType tokenType = TokenType; |
| | | 1578 | | |
| | 0 | 1579 | | if (tokenType != JsonTokenType.StartArray) |
| | 0 | 1580 | | { |
| | 0 | 1581 | | ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartArray, tokenType); |
| | | 1582 | | } |
| | | 1583 | | |
| | 0 | 1584 | | return new ArrayEnumerator(this); |
| | 0 | 1585 | | } |
| | | 1586 | | |
| | | 1587 | | /// <summary> |
| | | 1588 | | /// Get an enumerator to enumerate the properties in the JSON object represented by this JsonElement. |
| | | 1589 | | /// </summary> |
| | | 1590 | | /// <returns> |
| | | 1591 | | /// An enumerator to enumerate the properties in the JSON object represented by this JsonElement. |
| | | 1592 | | /// </returns> |
| | | 1593 | | /// <exception cref="InvalidOperationException"> |
| | | 1594 | | /// This value's <see cref="ValueKind"/> is not <see cref="JsonValueKind.Object"/>. |
| | | 1595 | | /// </exception> |
| | | 1596 | | /// <exception cref="ObjectDisposedException"> |
| | | 1597 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1598 | | /// </exception> |
| | | 1599 | | public ObjectEnumerator EnumerateObject() |
| | 0 | 1600 | | { |
| | 0 | 1601 | | CheckValidInstance(); |
| | | 1602 | | |
| | 0 | 1603 | | JsonTokenType tokenType = TokenType; |
| | | 1604 | | |
| | 0 | 1605 | | if (tokenType != JsonTokenType.StartObject) |
| | 0 | 1606 | | { |
| | 0 | 1607 | | ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType.StartObject, tokenType); |
| | | 1608 | | } |
| | | 1609 | | |
| | 0 | 1610 | | return new ObjectEnumerator(this); |
| | 0 | 1611 | | } |
| | | 1612 | | |
| | | 1613 | | /// <summary> |
| | | 1614 | | /// Gets a string representation for the current value appropriate to the value type. |
| | | 1615 | | /// </summary> |
| | | 1616 | | /// <remarks> |
| | | 1617 | | /// <para> |
| | | 1618 | | /// For JsonElement built from <see cref="JsonDocument"/>: |
| | | 1619 | | /// </para> |
| | | 1620 | | /// |
| | | 1621 | | /// <para> |
| | | 1622 | | /// For <see cref="JsonValueKind.Null"/>, <see cref="string.Empty"/> is returned. |
| | | 1623 | | /// </para> |
| | | 1624 | | /// |
| | | 1625 | | /// <para> |
| | | 1626 | | /// For <see cref="JsonValueKind.True"/>, <see cref="bool.TrueString"/> is returned. |
| | | 1627 | | /// </para> |
| | | 1628 | | /// |
| | | 1629 | | /// <para> |
| | | 1630 | | /// For <see cref="JsonValueKind.False"/>, <see cref="bool.FalseString"/> is returned. |
| | | 1631 | | /// </para> |
| | | 1632 | | /// |
| | | 1633 | | /// <para> |
| | | 1634 | | /// For <see cref="JsonValueKind.String"/>, the value of <see cref="GetString"/>() is returned. |
| | | 1635 | | /// </para> |
| | | 1636 | | /// |
| | | 1637 | | /// <para> |
| | | 1638 | | /// For other types, the value of <see cref="GetRawText"/>() is returned. |
| | | 1639 | | /// </para> |
| | | 1640 | | /// </remarks> |
| | | 1641 | | /// <returns> |
| | | 1642 | | /// A string representation for the current value appropriate to the value type. |
| | | 1643 | | /// </returns> |
| | | 1644 | | /// <exception cref="ObjectDisposedException"> |
| | | 1645 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 1646 | | /// </exception> |
| | | 1647 | | public override string ToString() |
| | 0 | 1648 | | { |
| | 0 | 1649 | | switch (TokenType) |
| | | 1650 | | { |
| | | 1651 | | case JsonTokenType.None: |
| | | 1652 | | case JsonTokenType.Null: |
| | 0 | 1653 | | return string.Empty; |
| | | 1654 | | case JsonTokenType.True: |
| | 0 | 1655 | | return bool.TrueString; |
| | | 1656 | | case JsonTokenType.False: |
| | 0 | 1657 | | return bool.FalseString; |
| | | 1658 | | case JsonTokenType.Number: |
| | | 1659 | | case JsonTokenType.StartArray: |
| | | 1660 | | case JsonTokenType.StartObject: |
| | 0 | 1661 | | { |
| | | 1662 | | // null parent should have hit the None case |
| | 0 | 1663 | | Debug.Assert(_parent != null); |
| | 0 | 1664 | | return _parent.GetRawValueAsString(_idx); |
| | | 1665 | | } |
| | | 1666 | | case JsonTokenType.String: |
| | 0 | 1667 | | return GetString()!; |
| | | 1668 | | case JsonTokenType.Comment: |
| | | 1669 | | case JsonTokenType.EndArray: |
| | | 1670 | | case JsonTokenType.EndObject: |
| | | 1671 | | default: |
| | 0 | 1672 | | Debug.Fail($"No handler for {nameof(JsonTokenType)}.{TokenType}"); |
| | | 1673 | | return string.Empty; |
| | | 1674 | | } |
| | 0 | 1675 | | } |
| | | 1676 | | |
| | | 1677 | | /// <summary> |
| | | 1678 | | /// Get a JsonElement which can be safely stored beyond the lifetime of the |
| | | 1679 | | /// original <see cref="JsonDocument"/>. |
| | | 1680 | | /// </summary> |
| | | 1681 | | /// <returns> |
| | | 1682 | | /// A JsonElement which can be safely stored beyond the lifetime of the |
| | | 1683 | | /// original <see cref="JsonDocument"/>. |
| | | 1684 | | /// </returns> |
| | | 1685 | | /// <remarks> |
| | | 1686 | | /// <para> |
| | | 1687 | | /// If this JsonElement is itself the output of a previous call to Clone, or |
| | | 1688 | | /// a value contained within another JsonElement which was the output of a previous |
| | | 1689 | | /// call to Clone, this method results in no additional memory allocation. |
| | | 1690 | | /// </para> |
| | | 1691 | | /// </remarks> |
| | | 1692 | | public JsonElement Clone() |
| | 0 | 1693 | | { |
| | 0 | 1694 | | CheckValidInstance(); |
| | | 1695 | | |
| | 0 | 1696 | | if (!_parent.IsDisposable) |
| | 0 | 1697 | | { |
| | 0 | 1698 | | return this; |
| | | 1699 | | } |
| | | 1700 | | |
| | 0 | 1701 | | return _parent.CloneElement(_idx); |
| | 0 | 1702 | | } |
| | | 1703 | | |
| | | 1704 | | private void CheckValidInstance() |
| | 155 | 1705 | | { |
| | 155 | 1706 | | if (_parent == null) |
| | 0 | 1707 | | { |
| | 0 | 1708 | | throw new InvalidOperationException(); |
| | | 1709 | | } |
| | 155 | 1710 | | } |
| | | 1711 | | |
| | 0 | 1712 | | internal readonly int MetadataDbIndex => _idx; |
| | | 1713 | | |
| | | 1714 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 1715 | | private string DebuggerDisplay => $"ValueKind = {ValueKind} : \"{ToString()}\""; |
| | | 1716 | | } |
| | | 1717 | | } |