| | | 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; |
| | | 5 | | using System.Buffers.Text; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | namespace System.Text.Json |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Represents the structure of a JSON value in a lightweight, read-only form. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// This class utilizes resources from pooled memory to minimize the garbage collector (GC) |
| | | 18 | | /// impact in high-usage scenarios. Failure to properly Dispose this object will result in |
| | | 19 | | /// the memory not being returned to the pool, which will cause an increase in GC impact across |
| | | 20 | | /// various parts of the framework. |
| | | 21 | | /// </remarks> |
| | | 22 | | public sealed partial class JsonDocument : IDisposable |
| | | 23 | | { |
| | | 24 | | private ReadOnlyMemory<byte> _utf8Json; |
| | | 25 | | private MetadataDb _parsedData; |
| | | 26 | | |
| | | 27 | | private byte[]? _extraRentedArrayPoolBytes; |
| | | 28 | | private PooledByteBufferWriter? _extraPooledByteBufferWriter; |
| | | 29 | | |
| | 28 | 30 | | internal bool IsDisposable { get; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The <see cref="JsonElement"/> representing the value of the document. |
| | | 34 | | /// </summary> |
| | 966 | 35 | | public JsonElement RootElement => new JsonElement(this, 0); |
| | | 36 | | |
| | 1400 | 37 | | private JsonDocument( |
| | 1400 | 38 | | ReadOnlyMemory<byte> utf8Json, |
| | 1400 | 39 | | MetadataDb parsedData, |
| | 1400 | 40 | | byte[]? extraRentedArrayPoolBytes = null, |
| | 1400 | 41 | | PooledByteBufferWriter? extraPooledByteBufferWriter = null, |
| | 1400 | 42 | | bool isDisposable = true) |
| | 1400 | 43 | | { |
| | 1400 | 44 | | Debug.Assert(!utf8Json.IsEmpty); |
| | | 45 | | |
| | | 46 | | // Both rented values better be null if we're not disposable. |
| | 1400 | 47 | | Debug.Assert(isDisposable || |
| | 1400 | 48 | | (extraRentedArrayPoolBytes == null && extraPooledByteBufferWriter == null)); |
| | | 49 | | |
| | | 50 | | // Both rented values can't be specified. |
| | 1400 | 51 | | Debug.Assert(extraRentedArrayPoolBytes == null || extraPooledByteBufferWriter == null); |
| | | 52 | | |
| | 1400 | 53 | | _utf8Json = utf8Json; |
| | 1400 | 54 | | _parsedData = parsedData; |
| | 1400 | 55 | | _extraRentedArrayPoolBytes = extraRentedArrayPoolBytes; |
| | 1400 | 56 | | _extraPooledByteBufferWriter = extraPooledByteBufferWriter; |
| | 1400 | 57 | | IsDisposable = isDisposable; |
| | 1400 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public void Dispose() |
| | 28 | 62 | | { |
| | 28 | 63 | | int length = _utf8Json.Length; |
| | 28 | 64 | | if (length == 0 || !IsDisposable) |
| | 0 | 65 | | { |
| | 0 | 66 | | return; |
| | | 67 | | } |
| | | 68 | | |
| | 28 | 69 | | _parsedData.Dispose(); |
| | 28 | 70 | | _utf8Json = ReadOnlyMemory<byte>.Empty; |
| | | 71 | | |
| | 28 | 72 | | if (_extraRentedArrayPoolBytes != null) |
| | 28 | 73 | | { |
| | 28 | 74 | | byte[]? extraRentedBytes = Interlocked.Exchange<byte[]?>(ref _extraRentedArrayPoolBytes, null); |
| | | 75 | | |
| | 28 | 76 | | if (extraRentedBytes != null) |
| | 28 | 77 | | { |
| | | 78 | | // When "extra rented bytes exist" it contains the document, |
| | | 79 | | // and thus needs to be cleared before being returned. |
| | 28 | 80 | | extraRentedBytes.AsSpan(0, length).Clear(); |
| | 28 | 81 | | ArrayPool<byte>.Shared.Return(extraRentedBytes); |
| | 28 | 82 | | } |
| | 28 | 83 | | } |
| | 0 | 84 | | else if (_extraPooledByteBufferWriter != null) |
| | 0 | 85 | | { |
| | 0 | 86 | | PooledByteBufferWriter? extraBufferWriter = Interlocked.Exchange<PooledByteBufferWriter?>(ref _extraPool |
| | 0 | 87 | | extraBufferWriter?.Dispose(); |
| | 0 | 88 | | } |
| | 28 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Write the document into the provided writer as a JSON value. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="writer"></param> |
| | | 95 | | /// <exception cref="ArgumentNullException"> |
| | | 96 | | /// The <paramref name="writer"/> parameter is <see langword="null"/>. |
| | | 97 | | /// </exception> |
| | | 98 | | /// <exception cref="InvalidOperationException"> |
| | | 99 | | /// This <see cref="RootElement"/>'s <see cref="JsonElement.ValueKind"/> would result in an invalid JSON. |
| | | 100 | | /// </exception> |
| | | 101 | | /// <exception cref="ObjectDisposedException"> |
| | | 102 | | /// The parent <see cref="JsonDocument"/> has been disposed. |
| | | 103 | | /// </exception> |
| | | 104 | | public void WriteTo(Utf8JsonWriter writer) |
| | 0 | 105 | | { |
| | 0 | 106 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 107 | | |
| | 0 | 108 | | RootElement.WriteTo(writer); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal JsonTokenType GetJsonTokenType(int index) |
| | 145 | 112 | | { |
| | 145 | 113 | | CheckNotDisposed(); |
| | | 114 | | |
| | 145 | 115 | | return _parsedData.GetJsonTokenType(index); |
| | 145 | 116 | | } |
| | | 117 | | |
| | | 118 | | internal bool ValueIsEscaped(int index, bool isPropertyName) |
| | 3 | 119 | | { |
| | 3 | 120 | | CheckNotDisposed(); |
| | | 121 | | |
| | 3 | 122 | | int matchIndex = isPropertyName ? index - DbRow.Size : index; |
| | 3 | 123 | | DbRow row = _parsedData.Get(matchIndex); |
| | 3 | 124 | | Debug.Assert(!isPropertyName || row.TokenType is JsonTokenType.PropertyName); |
| | | 125 | | |
| | 3 | 126 | | return row.HasComplexChildren; |
| | 3 | 127 | | } |
| | | 128 | | |
| | | 129 | | internal int GetArrayLength(int index) |
| | 0 | 130 | | { |
| | 0 | 131 | | CheckNotDisposed(); |
| | | 132 | | |
| | 0 | 133 | | DbRow row = _parsedData.Get(index); |
| | | 134 | | |
| | 0 | 135 | | CheckExpectedType(JsonTokenType.StartArray, row.TokenType); |
| | | 136 | | |
| | 0 | 137 | | return row.SizeOrLength; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | internal int GetPropertyCount(int index) |
| | 0 | 141 | | { |
| | 0 | 142 | | CheckNotDisposed(); |
| | | 143 | | |
| | 0 | 144 | | DbRow row = _parsedData.Get(index); |
| | | 145 | | |
| | 0 | 146 | | CheckExpectedType(JsonTokenType.StartObject, row.TokenType); |
| | | 147 | | |
| | 0 | 148 | | return row.SizeOrLength; |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | internal JsonElement GetArrayIndexElement(int currentIndex, int arrayIndex) |
| | 0 | 152 | | { |
| | 0 | 153 | | CheckNotDisposed(); |
| | | 154 | | |
| | 0 | 155 | | DbRow row = _parsedData.Get(currentIndex); |
| | | 156 | | |
| | 0 | 157 | | CheckExpectedType(JsonTokenType.StartArray, row.TokenType); |
| | | 158 | | |
| | 0 | 159 | | int arrayLength = row.SizeOrLength; |
| | | 160 | | |
| | 0 | 161 | | if ((uint)arrayIndex >= (uint)arrayLength) |
| | 0 | 162 | | { |
| | 0 | 163 | | throw new IndexOutOfRangeException(); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (!row.HasComplexChildren) |
| | 0 | 167 | | { |
| | | 168 | | // Since we wouldn't be here without having completed the document parse, and we |
| | | 169 | | // already vetted the index against the length, this new index will always be |
| | | 170 | | // within the table. |
| | 0 | 171 | | return new JsonElement(this, currentIndex + ((arrayIndex + 1) * DbRow.Size)); |
| | | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | int elementCount = 0; |
| | 0 | 175 | | int objectOffset = currentIndex + DbRow.Size; |
| | | 176 | | |
| | 0 | 177 | | for (; objectOffset < _parsedData.Length; objectOffset += DbRow.Size) |
| | 0 | 178 | | { |
| | 0 | 179 | | if (arrayIndex == elementCount) |
| | 0 | 180 | | { |
| | 0 | 181 | | return new JsonElement(this, objectOffset); |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | row = _parsedData.Get(objectOffset); |
| | | 185 | | |
| | 0 | 186 | | if (!row.IsSimpleValue) |
| | 0 | 187 | | { |
| | 0 | 188 | | objectOffset += DbRow.Size * row.NumberOfRows; |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | elementCount++; |
| | 0 | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | Debug.Fail( |
| | 0 | 195 | | $"Ran out of database searching for array index {arrayIndex} from {currentIndex} when length was {arrayL |
| | | 196 | | throw new IndexOutOfRangeException(); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | internal int GetEndIndex(int index, bool includeEndElement) |
| | 0 | 200 | | { |
| | 0 | 201 | | CheckNotDisposed(); |
| | | 202 | | |
| | 0 | 203 | | DbRow row = _parsedData.Get(index); |
| | | 204 | | |
| | 0 | 205 | | if (row.IsSimpleValue) |
| | 0 | 206 | | { |
| | 0 | 207 | | return index + DbRow.Size; |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | int endIndex = index + DbRow.Size * row.NumberOfRows; |
| | | 211 | | |
| | 0 | 212 | | if (includeEndElement) |
| | 0 | 213 | | { |
| | 0 | 214 | | endIndex += DbRow.Size; |
| | 0 | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | return endIndex; |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | internal ReadOnlyMemory<byte> GetRootRawValue() |
| | 0 | 221 | | { |
| | 0 | 222 | | return GetRawValue(0, includeQuotes: true); |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | internal ReadOnlyMemory<byte> GetRawValue(int index, bool includeQuotes) |
| | 73 | 226 | | { |
| | 73 | 227 | | CheckNotDisposed(); |
| | | 228 | | |
| | 73 | 229 | | DbRow row = _parsedData.Get(index); |
| | | 230 | | |
| | 73 | 231 | | if (row.IsSimpleValue) |
| | 73 | 232 | | { |
| | 73 | 233 | | if (includeQuotes && row.TokenType == JsonTokenType.String) |
| | 0 | 234 | | { |
| | | 235 | | // Start one character earlier than the value (the open quote) |
| | | 236 | | // End one character after the value (the close quote) |
| | 0 | 237 | | return _utf8Json.Slice(row.Location - 1, row.SizeOrLength + 2); |
| | | 238 | | } |
| | | 239 | | |
| | 73 | 240 | | return _utf8Json.Slice(row.Location, row.SizeOrLength); |
| | | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | int endElementIdx = GetEndIndex(index, includeEndElement: false); |
| | 0 | 244 | | int start = row.Location; |
| | 0 | 245 | | row = _parsedData.Get(endElementIdx); |
| | 0 | 246 | | return _utf8Json.Slice(start, row.Location - start + row.SizeOrLength); |
| | 73 | 247 | | } |
| | | 248 | | |
| | | 249 | | private ReadOnlyMemory<byte> GetPropertyRawValue(int valueIndex) |
| | 0 | 250 | | { |
| | 0 | 251 | | CheckNotDisposed(); |
| | | 252 | | |
| | | 253 | | // The property name is stored one row before the value |
| | 0 | 254 | | DbRow row = _parsedData.Get(valueIndex - DbRow.Size); |
| | 0 | 255 | | Debug.Assert(row.TokenType == JsonTokenType.PropertyName); |
| | | 256 | | |
| | | 257 | | // Subtract one for the open quote. |
| | 0 | 258 | | int start = row.Location - 1; |
| | | 259 | | int end; |
| | | 260 | | |
| | 0 | 261 | | row = _parsedData.Get(valueIndex); |
| | | 262 | | |
| | 0 | 263 | | if (row.IsSimpleValue) |
| | 0 | 264 | | { |
| | 0 | 265 | | end = row.Location + row.SizeOrLength; |
| | | 266 | | |
| | | 267 | | // If the value was a string, pick up the terminating quote. |
| | 0 | 268 | | if (row.TokenType == JsonTokenType.String) |
| | 0 | 269 | | { |
| | 0 | 270 | | end++; |
| | 0 | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | return _utf8Json.Slice(start, end - start); |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | int endElementIdx = GetEndIndex(valueIndex, includeEndElement: false); |
| | 0 | 277 | | row = _parsedData.Get(endElementIdx); |
| | 0 | 278 | | end = row.Location + row.SizeOrLength; |
| | 0 | 279 | | return _utf8Json.Slice(start, end - start); |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | internal string? GetString(int index, JsonTokenType expectedType) |
| | 0 | 283 | | { |
| | 0 | 284 | | CheckNotDisposed(); |
| | | 285 | | |
| | 0 | 286 | | DbRow row = _parsedData.Get(index); |
| | | 287 | | |
| | 0 | 288 | | JsonTokenType tokenType = row.TokenType; |
| | | 289 | | |
| | 0 | 290 | | if (tokenType == JsonTokenType.Null) |
| | 0 | 291 | | { |
| | 0 | 292 | | return null; |
| | | 293 | | } |
| | | 294 | | |
| | 0 | 295 | | CheckExpectedType(expectedType, tokenType); |
| | | 296 | | |
| | 0 | 297 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 298 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 299 | | |
| | 0 | 300 | | return row.HasComplexChildren |
| | 0 | 301 | | ? JsonReaderHelper.GetUnescapedString(segment) |
| | 0 | 302 | | : JsonReaderHelper.TranscodeHelper(segment); |
| | 0 | 303 | | } |
| | | 304 | | |
| | | 305 | | internal bool TextEquals(int index, ReadOnlySpan<char> otherText, bool isPropertyName) |
| | 0 | 306 | | { |
| | 0 | 307 | | CheckNotDisposed(); |
| | | 308 | | |
| | 0 | 309 | | byte[]? otherUtf8TextArray = null; |
| | | 310 | | |
| | 0 | 311 | | int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); |
| | 0 | 312 | | Span<byte> otherUtf8Text = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 313 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 314 | | (otherUtf8TextArray = ArrayPool<byte>.Shared.Rent(length)); |
| | | 315 | | |
| | 0 | 316 | | OperationStatus status = JsonWriterHelper.ToUtf8(otherText, otherUtf8Text, out int written); |
| | 0 | 317 | | Debug.Assert(status != OperationStatus.DestinationTooSmall); |
| | | 318 | | bool result; |
| | 0 | 319 | | if (status == OperationStatus.InvalidData) |
| | 0 | 320 | | { |
| | 0 | 321 | | result = false; |
| | 0 | 322 | | } |
| | | 323 | | else |
| | 0 | 324 | | { |
| | 0 | 325 | | Debug.Assert(status == OperationStatus.Done); |
| | 0 | 326 | | result = TextEquals(index, otherUtf8Text.Slice(0, written), isPropertyName, shouldUnescape: true); |
| | 0 | 327 | | } |
| | | 328 | | |
| | 0 | 329 | | if (otherUtf8TextArray != null) |
| | 0 | 330 | | { |
| | 0 | 331 | | otherUtf8Text.Slice(0, written).Clear(); |
| | 0 | 332 | | ArrayPool<byte>.Shared.Return(otherUtf8TextArray); |
| | 0 | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | return result; |
| | 0 | 336 | | } |
| | | 337 | | |
| | | 338 | | internal bool TextEquals(int index, ReadOnlySpan<byte> otherUtf8Text, bool isPropertyName, bool shouldUnescape) |
| | 3 | 339 | | { |
| | 3 | 340 | | CheckNotDisposed(); |
| | | 341 | | |
| | 3 | 342 | | int matchIndex = isPropertyName ? index - DbRow.Size : index; |
| | | 343 | | |
| | 3 | 344 | | DbRow row = _parsedData.Get(matchIndex); |
| | | 345 | | |
| | 3 | 346 | | CheckExpectedType( |
| | 3 | 347 | | isPropertyName ? JsonTokenType.PropertyName : JsonTokenType.String, |
| | 3 | 348 | | row.TokenType); |
| | | 349 | | |
| | 3 | 350 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 3 | 351 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 352 | | |
| | 3 | 353 | | if (otherUtf8Text.Length > segment.Length || (!shouldUnescape && otherUtf8Text.Length != segment.Length)) |
| | 0 | 354 | | { |
| | 0 | 355 | | return false; |
| | | 356 | | } |
| | | 357 | | |
| | 3 | 358 | | if (row.HasComplexChildren && shouldUnescape) |
| | 0 | 359 | | { |
| | 0 | 360 | | if (otherUtf8Text.Length < segment.Length / JsonConstants.MaxExpansionFactorWhileEscaping) |
| | 0 | 361 | | { |
| | 0 | 362 | | return false; |
| | | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | int idx = segment.IndexOf(JsonConstants.BackSlash); |
| | 0 | 366 | | Debug.Assert(idx != -1); |
| | | 367 | | |
| | 0 | 368 | | if (!otherUtf8Text.StartsWith(segment.Slice(0, idx))) |
| | 0 | 369 | | { |
| | 0 | 370 | | return false; |
| | | 371 | | } |
| | | 372 | | |
| | 0 | 373 | | return JsonReaderHelper.UnescapeAndCompare(segment.Slice(idx), otherUtf8Text.Slice(idx)); |
| | | 374 | | } |
| | | 375 | | |
| | 3 | 376 | | return segment.SequenceEqual(otherUtf8Text); |
| | 3 | 377 | | } |
| | | 378 | | |
| | | 379 | | internal string GetNameOfPropertyValue(int index) |
| | 0 | 380 | | { |
| | | 381 | | // The property name is one row before the property value |
| | 0 | 382 | | return GetString(index - DbRow.Size, JsonTokenType.PropertyName)!; |
| | 0 | 383 | | } |
| | | 384 | | |
| | | 385 | | internal ReadOnlySpan<byte> GetPropertyNameRaw(int index) |
| | 0 | 386 | | { |
| | 0 | 387 | | CheckNotDisposed(); |
| | | 388 | | |
| | 0 | 389 | | DbRow row = _parsedData.Get(index - DbRow.Size); |
| | 0 | 390 | | Debug.Assert(row.TokenType is JsonTokenType.PropertyName); |
| | | 391 | | |
| | 0 | 392 | | return _utf8Json.Span.Slice(row.Location, row.SizeOrLength); |
| | 0 | 393 | | } |
| | | 394 | | |
| | | 395 | | internal bool TryGetValue(int index, [NotNullWhen(true)] out byte[]? value) |
| | 0 | 396 | | { |
| | 0 | 397 | | CheckNotDisposed(); |
| | | 398 | | |
| | 0 | 399 | | DbRow row = _parsedData.Get(index); |
| | | 400 | | |
| | 0 | 401 | | CheckExpectedType(JsonTokenType.String, row.TokenType); |
| | | 402 | | |
| | 0 | 403 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 404 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 405 | | |
| | | 406 | | // Segment needs to be unescaped |
| | 0 | 407 | | if (row.HasComplexChildren) |
| | 0 | 408 | | { |
| | 0 | 409 | | return JsonReaderHelper.TryGetUnescapedBase64Bytes(segment, out value); |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | Debug.Assert(!segment.Contains(JsonConstants.BackSlash)); |
| | 0 | 413 | | return JsonReaderHelper.TryDecodeBase64(segment, out value); |
| | 0 | 414 | | } |
| | | 415 | | |
| | | 416 | | internal bool TryGetValue(int index, out sbyte value) |
| | 0 | 417 | | { |
| | 0 | 418 | | CheckNotDisposed(); |
| | | 419 | | |
| | 0 | 420 | | DbRow row = _parsedData.Get(index); |
| | | 421 | | |
| | 0 | 422 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 423 | | |
| | 0 | 424 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 425 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 426 | | |
| | 0 | 427 | | if (Utf8Parser.TryParse(segment, out sbyte tmp, out int consumed) && |
| | 0 | 428 | | consumed == segment.Length) |
| | 0 | 429 | | { |
| | 0 | 430 | | value = tmp; |
| | 0 | 431 | | return true; |
| | | 432 | | } |
| | | 433 | | |
| | 0 | 434 | | value = 0; |
| | 0 | 435 | | return false; |
| | 0 | 436 | | } |
| | | 437 | | |
| | | 438 | | internal bool TryGetValue(int index, out byte value) |
| | 0 | 439 | | { |
| | 0 | 440 | | CheckNotDisposed(); |
| | | 441 | | |
| | 0 | 442 | | DbRow row = _parsedData.Get(index); |
| | | 443 | | |
| | 0 | 444 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 445 | | |
| | 0 | 446 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 447 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 448 | | |
| | 0 | 449 | | if (Utf8Parser.TryParse(segment, out byte tmp, out int consumed) && |
| | 0 | 450 | | consumed == segment.Length) |
| | 0 | 451 | | { |
| | 0 | 452 | | value = tmp; |
| | 0 | 453 | | return true; |
| | | 454 | | } |
| | | 455 | | |
| | 0 | 456 | | value = 0; |
| | 0 | 457 | | return false; |
| | 0 | 458 | | } |
| | | 459 | | |
| | | 460 | | internal bool TryGetValue(int index, out short value) |
| | 0 | 461 | | { |
| | 0 | 462 | | CheckNotDisposed(); |
| | | 463 | | |
| | 0 | 464 | | DbRow row = _parsedData.Get(index); |
| | | 465 | | |
| | 0 | 466 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 467 | | |
| | 0 | 468 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 469 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 470 | | |
| | 0 | 471 | | if (Utf8Parser.TryParse(segment, out short tmp, out int consumed) && |
| | 0 | 472 | | consumed == segment.Length) |
| | 0 | 473 | | { |
| | 0 | 474 | | value = tmp; |
| | 0 | 475 | | return true; |
| | | 476 | | } |
| | | 477 | | |
| | 0 | 478 | | value = 0; |
| | 0 | 479 | | return false; |
| | 0 | 480 | | } |
| | | 481 | | |
| | | 482 | | internal bool TryGetValue(int index, out ushort value) |
| | 0 | 483 | | { |
| | 0 | 484 | | CheckNotDisposed(); |
| | | 485 | | |
| | 0 | 486 | | DbRow row = _parsedData.Get(index); |
| | | 487 | | |
| | 0 | 488 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 489 | | |
| | 0 | 490 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 491 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 492 | | |
| | 0 | 493 | | if (Utf8Parser.TryParse(segment, out ushort tmp, out int consumed) && |
| | 0 | 494 | | consumed == segment.Length) |
| | 0 | 495 | | { |
| | 0 | 496 | | value = tmp; |
| | 0 | 497 | | return true; |
| | | 498 | | } |
| | | 499 | | |
| | 0 | 500 | | value = 0; |
| | 0 | 501 | | return false; |
| | 0 | 502 | | } |
| | | 503 | | |
| | | 504 | | internal bool TryGetValue(int index, out int value) |
| | 0 | 505 | | { |
| | 0 | 506 | | CheckNotDisposed(); |
| | | 507 | | |
| | 0 | 508 | | DbRow row = _parsedData.Get(index); |
| | | 509 | | |
| | 0 | 510 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 511 | | |
| | 0 | 512 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 513 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 514 | | |
| | 0 | 515 | | if (Utf8Parser.TryParse(segment, out int tmp, out int consumed) && |
| | 0 | 516 | | consumed == segment.Length) |
| | 0 | 517 | | { |
| | 0 | 518 | | value = tmp; |
| | 0 | 519 | | return true; |
| | | 520 | | } |
| | | 521 | | |
| | 0 | 522 | | value = 0; |
| | 0 | 523 | | return false; |
| | 0 | 524 | | } |
| | | 525 | | |
| | | 526 | | internal bool TryGetValue(int index, out uint value) |
| | 0 | 527 | | { |
| | 0 | 528 | | CheckNotDisposed(); |
| | | 529 | | |
| | 0 | 530 | | DbRow row = _parsedData.Get(index); |
| | | 531 | | |
| | 0 | 532 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 533 | | |
| | 0 | 534 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 535 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 536 | | |
| | 0 | 537 | | if (Utf8Parser.TryParse(segment, out uint tmp, out int consumed) && |
| | 0 | 538 | | consumed == segment.Length) |
| | 0 | 539 | | { |
| | 0 | 540 | | value = tmp; |
| | 0 | 541 | | return true; |
| | | 542 | | } |
| | | 543 | | |
| | 0 | 544 | | value = 0; |
| | 0 | 545 | | return false; |
| | 0 | 546 | | } |
| | | 547 | | |
| | | 548 | | internal bool TryGetValue(int index, out long value) |
| | 0 | 549 | | { |
| | 0 | 550 | | CheckNotDisposed(); |
| | | 551 | | |
| | 0 | 552 | | DbRow row = _parsedData.Get(index); |
| | | 553 | | |
| | 0 | 554 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 555 | | |
| | 0 | 556 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 557 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 558 | | |
| | 0 | 559 | | if (Utf8Parser.TryParse(segment, out long tmp, out int consumed) && |
| | 0 | 560 | | consumed == segment.Length) |
| | 0 | 561 | | { |
| | 0 | 562 | | value = tmp; |
| | 0 | 563 | | return true; |
| | | 564 | | } |
| | | 565 | | |
| | 0 | 566 | | value = 0; |
| | 0 | 567 | | return false; |
| | 0 | 568 | | } |
| | | 569 | | |
| | | 570 | | internal bool TryGetValue(int index, out ulong value) |
| | 0 | 571 | | { |
| | 0 | 572 | | CheckNotDisposed(); |
| | | 573 | | |
| | 0 | 574 | | DbRow row = _parsedData.Get(index); |
| | | 575 | | |
| | 0 | 576 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 577 | | |
| | 0 | 578 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 579 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 580 | | |
| | 0 | 581 | | if (Utf8Parser.TryParse(segment, out ulong tmp, out int consumed) && |
| | 0 | 582 | | consumed == segment.Length) |
| | 0 | 583 | | { |
| | 0 | 584 | | value = tmp; |
| | 0 | 585 | | return true; |
| | | 586 | | } |
| | | 587 | | |
| | 0 | 588 | | value = 0; |
| | 0 | 589 | | return false; |
| | 0 | 590 | | } |
| | | 591 | | |
| | | 592 | | internal bool TryGetValue(int index, out double value) |
| | 0 | 593 | | { |
| | 0 | 594 | | CheckNotDisposed(); |
| | | 595 | | |
| | 0 | 596 | | DbRow row = _parsedData.Get(index); |
| | | 597 | | |
| | 0 | 598 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 599 | | |
| | 0 | 600 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 601 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 602 | | |
| | 0 | 603 | | if (Utf8Parser.TryParse(segment, out double tmp, out int bytesConsumed) && |
| | 0 | 604 | | segment.Length == bytesConsumed) |
| | 0 | 605 | | { |
| | 0 | 606 | | value = tmp; |
| | 0 | 607 | | return true; |
| | | 608 | | } |
| | | 609 | | |
| | 0 | 610 | | value = 0; |
| | 0 | 611 | | return false; |
| | 0 | 612 | | } |
| | | 613 | | |
| | | 614 | | internal bool TryGetValue(int index, out float value) |
| | 0 | 615 | | { |
| | 0 | 616 | | CheckNotDisposed(); |
| | | 617 | | |
| | 0 | 618 | | DbRow row = _parsedData.Get(index); |
| | | 619 | | |
| | 0 | 620 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 621 | | |
| | 0 | 622 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 623 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 624 | | |
| | 0 | 625 | | if (Utf8Parser.TryParse(segment, out float tmp, out int bytesConsumed) && |
| | 0 | 626 | | segment.Length == bytesConsumed) |
| | 0 | 627 | | { |
| | 0 | 628 | | value = tmp; |
| | 0 | 629 | | return true; |
| | | 630 | | } |
| | | 631 | | |
| | 0 | 632 | | value = 0; |
| | 0 | 633 | | return false; |
| | 0 | 634 | | } |
| | | 635 | | |
| | | 636 | | internal bool TryGetValue(int index, out decimal value) |
| | 0 | 637 | | { |
| | 0 | 638 | | CheckNotDisposed(); |
| | | 639 | | |
| | 0 | 640 | | DbRow row = _parsedData.Get(index); |
| | | 641 | | |
| | 0 | 642 | | CheckExpectedType(JsonTokenType.Number, row.TokenType); |
| | | 643 | | |
| | 0 | 644 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 645 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 646 | | |
| | 0 | 647 | | if (Utf8Parser.TryParse(segment, out decimal tmp, out int bytesConsumed) && |
| | 0 | 648 | | segment.Length == bytesConsumed) |
| | 0 | 649 | | { |
| | 0 | 650 | | value = tmp; |
| | 0 | 651 | | return true; |
| | | 652 | | } |
| | | 653 | | |
| | 0 | 654 | | value = 0; |
| | 0 | 655 | | return false; |
| | 0 | 656 | | } |
| | | 657 | | |
| | | 658 | | internal bool TryGetValue(int index, out DateTime value) |
| | 0 | 659 | | { |
| | 0 | 660 | | CheckNotDisposed(); |
| | | 661 | | |
| | 0 | 662 | | DbRow row = _parsedData.Get(index); |
| | | 663 | | |
| | 0 | 664 | | CheckExpectedType(JsonTokenType.String, row.TokenType); |
| | | 665 | | |
| | 0 | 666 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 667 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 668 | | |
| | 0 | 669 | | return JsonReaderHelper.TryGetValue(segment, row.HasComplexChildren, out value); |
| | 0 | 670 | | } |
| | | 671 | | |
| | | 672 | | internal bool TryGetValue(int index, out DateTimeOffset value) |
| | 0 | 673 | | { |
| | 0 | 674 | | CheckNotDisposed(); |
| | | 675 | | |
| | 0 | 676 | | DbRow row = _parsedData.Get(index); |
| | | 677 | | |
| | 0 | 678 | | CheckExpectedType(JsonTokenType.String, row.TokenType); |
| | | 679 | | |
| | 0 | 680 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 681 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 682 | | |
| | 0 | 683 | | return JsonReaderHelper.TryGetValue(segment, row.HasComplexChildren, out value); |
| | 0 | 684 | | } |
| | | 685 | | |
| | | 686 | | internal bool TryGetValue(int index, out Guid value) |
| | 0 | 687 | | { |
| | 0 | 688 | | CheckNotDisposed(); |
| | | 689 | | |
| | 0 | 690 | | DbRow row = _parsedData.Get(index); |
| | | 691 | | |
| | 0 | 692 | | CheckExpectedType(JsonTokenType.String, row.TokenType); |
| | | 693 | | |
| | 0 | 694 | | ReadOnlySpan<byte> data = _utf8Json.Span; |
| | 0 | 695 | | ReadOnlySpan<byte> segment = data.Slice(row.Location, row.SizeOrLength); |
| | | 696 | | |
| | 0 | 697 | | return JsonReaderHelper.TryGetValue(segment, row.HasComplexChildren, out value); |
| | 0 | 698 | | } |
| | | 699 | | |
| | | 700 | | internal string GetRawValueAsString(int index) |
| | 0 | 701 | | { |
| | 0 | 702 | | ReadOnlyMemory<byte> segment = GetRawValue(index, includeQuotes: true); |
| | 0 | 703 | | return JsonReaderHelper.TranscodeHelper(segment.Span); |
| | 0 | 704 | | } |
| | | 705 | | |
| | | 706 | | internal string GetPropertyRawValueAsString(int valueIndex) |
| | 0 | 707 | | { |
| | 0 | 708 | | ReadOnlyMemory<byte> segment = GetPropertyRawValue(valueIndex); |
| | 0 | 709 | | return JsonReaderHelper.TranscodeHelper(segment.Span); |
| | 0 | 710 | | } |
| | | 711 | | |
| | | 712 | | internal JsonElement CloneElement(int index) |
| | 0 | 713 | | { |
| | 0 | 714 | | int endIndex = GetEndIndex(index, true); |
| | 0 | 715 | | MetadataDb newDb = _parsedData.CopySegment(index, endIndex); |
| | 0 | 716 | | ReadOnlyMemory<byte> segmentCopy = GetRawValue(index, includeQuotes: true).ToArray(); |
| | | 717 | | |
| | 0 | 718 | | JsonDocument newDocument = |
| | 0 | 719 | | new JsonDocument( |
| | 0 | 720 | | segmentCopy, |
| | 0 | 721 | | newDb, |
| | 0 | 722 | | extraRentedArrayPoolBytes: null, |
| | 0 | 723 | | extraPooledByteBufferWriter: null, |
| | 0 | 724 | | isDisposable: false); |
| | | 725 | | |
| | 0 | 726 | | return newDocument.RootElement; |
| | 0 | 727 | | } |
| | | 728 | | |
| | | 729 | | internal void WriteElementTo( |
| | | 730 | | int index, |
| | | 731 | | Utf8JsonWriter writer) |
| | 0 | 732 | | { |
| | 0 | 733 | | CheckNotDisposed(); |
| | | 734 | | |
| | 0 | 735 | | DbRow row = _parsedData.Get(index); |
| | | 736 | | |
| | 0 | 737 | | switch (row.TokenType) |
| | | 738 | | { |
| | | 739 | | case JsonTokenType.StartObject: |
| | 0 | 740 | | writer.WriteStartObject(); |
| | 0 | 741 | | WriteComplexElement(index, writer); |
| | 0 | 742 | | return; |
| | | 743 | | case JsonTokenType.StartArray: |
| | 0 | 744 | | writer.WriteStartArray(); |
| | 0 | 745 | | WriteComplexElement(index, writer); |
| | 0 | 746 | | return; |
| | | 747 | | case JsonTokenType.String: |
| | 0 | 748 | | WriteString(row, writer); |
| | 0 | 749 | | return; |
| | | 750 | | case JsonTokenType.Number: |
| | 0 | 751 | | writer.WriteNumberValue(_utf8Json.Slice(row.Location, row.SizeOrLength).Span); |
| | 0 | 752 | | return; |
| | | 753 | | case JsonTokenType.True: |
| | 0 | 754 | | writer.WriteBooleanValue(value: true); |
| | 0 | 755 | | return; |
| | | 756 | | case JsonTokenType.False: |
| | 0 | 757 | | writer.WriteBooleanValue(value: false); |
| | 0 | 758 | | return; |
| | | 759 | | case JsonTokenType.Null: |
| | 0 | 760 | | writer.WriteNullValue(); |
| | 0 | 761 | | return; |
| | | 762 | | } |
| | | 763 | | |
| | 0 | 764 | | Debug.Fail($"Unexpected encounter with JsonTokenType {row.TokenType}"); |
| | 0 | 765 | | } |
| | | 766 | | |
| | | 767 | | private void WriteComplexElement(int index, Utf8JsonWriter writer) |
| | 0 | 768 | | { |
| | 0 | 769 | | int endIndex = GetEndIndex(index, true); |
| | | 770 | | |
| | 0 | 771 | | for (int i = index + DbRow.Size; i < endIndex; i += DbRow.Size) |
| | 0 | 772 | | { |
| | 0 | 773 | | DbRow row = _parsedData.Get(i); |
| | | 774 | | |
| | | 775 | | // All of the types which don't need the value span |
| | 0 | 776 | | switch (row.TokenType) |
| | | 777 | | { |
| | | 778 | | case JsonTokenType.String: |
| | 0 | 779 | | WriteString(row, writer); |
| | 0 | 780 | | continue; |
| | | 781 | | case JsonTokenType.Number: |
| | 0 | 782 | | writer.WriteNumberValue(_utf8Json.Slice(row.Location, row.SizeOrLength).Span); |
| | 0 | 783 | | continue; |
| | | 784 | | case JsonTokenType.True: |
| | 0 | 785 | | writer.WriteBooleanValue(value: true); |
| | 0 | 786 | | continue; |
| | | 787 | | case JsonTokenType.False: |
| | 0 | 788 | | writer.WriteBooleanValue(value: false); |
| | 0 | 789 | | continue; |
| | | 790 | | case JsonTokenType.Null: |
| | 0 | 791 | | writer.WriteNullValue(); |
| | 0 | 792 | | continue; |
| | | 793 | | case JsonTokenType.StartObject: |
| | 0 | 794 | | writer.WriteStartObject(); |
| | 0 | 795 | | continue; |
| | | 796 | | case JsonTokenType.EndObject: |
| | 0 | 797 | | writer.WriteEndObject(); |
| | 0 | 798 | | continue; |
| | | 799 | | case JsonTokenType.StartArray: |
| | 0 | 800 | | writer.WriteStartArray(); |
| | 0 | 801 | | continue; |
| | | 802 | | case JsonTokenType.EndArray: |
| | 0 | 803 | | writer.WriteEndArray(); |
| | 0 | 804 | | continue; |
| | | 805 | | case JsonTokenType.PropertyName: |
| | 0 | 806 | | WritePropertyName(row, writer); |
| | 0 | 807 | | continue; |
| | | 808 | | } |
| | | 809 | | |
| | 0 | 810 | | Debug.Fail($"Unexpected encounter with JsonTokenType {row.TokenType}"); |
| | | 811 | | } |
| | 0 | 812 | | } |
| | | 813 | | |
| | | 814 | | private ReadOnlySpan<byte> UnescapeString(in DbRow row, out ArraySegment<byte> rented) |
| | 0 | 815 | | { |
| | 0 | 816 | | Debug.Assert(row.TokenType == JsonTokenType.String || row.TokenType == JsonTokenType.PropertyName); |
| | 0 | 817 | | int loc = row.Location; |
| | 0 | 818 | | int length = row.SizeOrLength; |
| | 0 | 819 | | ReadOnlySpan<byte> text = _utf8Json.Slice(loc, length).Span; |
| | | 820 | | |
| | 0 | 821 | | if (!row.HasComplexChildren) |
| | 0 | 822 | | { |
| | 0 | 823 | | rented = default; |
| | 0 | 824 | | return text; |
| | | 825 | | } |
| | | 826 | | |
| | 0 | 827 | | byte[] rent = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 828 | | JsonReaderHelper.Unescape(text, rent, out int written); |
| | 0 | 829 | | rented = new ArraySegment<byte>(rent, 0, written); |
| | 0 | 830 | | return rented.AsSpan(); |
| | 0 | 831 | | } |
| | | 832 | | |
| | | 833 | | private static void ClearAndReturn(ArraySegment<byte> rented) |
| | 0 | 834 | | { |
| | 0 | 835 | | if (rented.Array != null) |
| | 0 | 836 | | { |
| | 0 | 837 | | rented.AsSpan().Clear(); |
| | 0 | 838 | | ArrayPool<byte>.Shared.Return(rented.Array); |
| | 0 | 839 | | } |
| | 0 | 840 | | } |
| | | 841 | | |
| | | 842 | | internal void WritePropertyName(int index, Utf8JsonWriter writer) |
| | 0 | 843 | | { |
| | 0 | 844 | | CheckNotDisposed(); |
| | | 845 | | |
| | 0 | 846 | | DbRow row = _parsedData.Get(index - DbRow.Size); |
| | 0 | 847 | | Debug.Assert(row.TokenType == JsonTokenType.PropertyName); |
| | 0 | 848 | | WritePropertyName(row, writer); |
| | 0 | 849 | | } |
| | | 850 | | |
| | | 851 | | private void WritePropertyName(in DbRow row, Utf8JsonWriter writer) |
| | 0 | 852 | | { |
| | 0 | 853 | | ArraySegment<byte> rented = default; |
| | | 854 | | |
| | | 855 | | try |
| | 0 | 856 | | { |
| | 0 | 857 | | writer.WritePropertyName(UnescapeString(row, out rented)); |
| | 0 | 858 | | } |
| | | 859 | | finally |
| | 0 | 860 | | { |
| | 0 | 861 | | ClearAndReturn(rented); |
| | 0 | 862 | | } |
| | 0 | 863 | | } |
| | | 864 | | |
| | | 865 | | private void WriteString(in DbRow row, Utf8JsonWriter writer) |
| | 0 | 866 | | { |
| | 0 | 867 | | ArraySegment<byte> rented = default; |
| | | 868 | | |
| | | 869 | | try |
| | 0 | 870 | | { |
| | 0 | 871 | | writer.WriteStringValue(UnescapeString(row, out rented)); |
| | 0 | 872 | | } |
| | | 873 | | finally |
| | 0 | 874 | | { |
| | 0 | 875 | | ClearAndReturn(rented); |
| | 0 | 876 | | } |
| | 0 | 877 | | } |
| | | 878 | | |
| | | 879 | | private static void Parse( |
| | | 880 | | ReadOnlySpan<byte> utf8JsonSpan, |
| | | 881 | | JsonReaderOptions readerOptions, |
| | | 882 | | ref MetadataDb database, |
| | | 883 | | ref StackRowStack stack) |
| | 1400 | 884 | | { |
| | 1400 | 885 | | bool inArray = false; |
| | 1400 | 886 | | int arrayItemsOrPropertyCount = 0; |
| | 1400 | 887 | | int numberOfRowsForMembers = 0; |
| | 1400 | 888 | | int numberOfRowsForValues = 0; |
| | | 889 | | |
| | 1400 | 890 | | Utf8JsonReader reader = new Utf8JsonReader( |
| | 1400 | 891 | | utf8JsonSpan, |
| | 1400 | 892 | | isFinalBlock: true, |
| | 1400 | 893 | | new JsonReaderState(options: readerOptions)); |
| | | 894 | | |
| | 2938 | 895 | | while (reader.Read()) |
| | 1538 | 896 | | { |
| | 1538 | 897 | | JsonTokenType tokenType = reader.TokenType; |
| | | 898 | | |
| | | 899 | | // Since the input payload is contained within a Span, |
| | | 900 | | // token start index can never be larger than int.MaxValue (i.e. utf8JsonSpan.Length). |
| | 1538 | 901 | | Debug.Assert(reader.TokenStartIndex <= int.MaxValue); |
| | 1538 | 902 | | int tokenStart = (int)reader.TokenStartIndex; |
| | | 903 | | |
| | 1538 | 904 | | if (tokenType == JsonTokenType.StartObject) |
| | 0 | 905 | | { |
| | 0 | 906 | | if (inArray) |
| | 0 | 907 | | { |
| | 0 | 908 | | arrayItemsOrPropertyCount++; |
| | 0 | 909 | | } |
| | | 910 | | |
| | 0 | 911 | | numberOfRowsForValues++; |
| | 0 | 912 | | database.Append(tokenType, tokenStart, DbRow.UnknownSize); |
| | 0 | 913 | | var row = new StackRow(arrayItemsOrPropertyCount, numberOfRowsForMembers + 1); |
| | 0 | 914 | | stack.Push(row); |
| | 0 | 915 | | arrayItemsOrPropertyCount = 0; |
| | 0 | 916 | | numberOfRowsForMembers = 0; |
| | 0 | 917 | | } |
| | 1538 | 918 | | else if (tokenType == JsonTokenType.EndObject) |
| | 0 | 919 | | { |
| | 0 | 920 | | int rowIndex = database.FindIndexOfFirstUnsetSizeOrLength(JsonTokenType.StartObject); |
| | | 921 | | |
| | 0 | 922 | | numberOfRowsForValues++; |
| | 0 | 923 | | numberOfRowsForMembers++; |
| | 0 | 924 | | database.SetLength(rowIndex, arrayItemsOrPropertyCount); |
| | | 925 | | |
| | 0 | 926 | | int newRowIndex = database.Length; |
| | 0 | 927 | | database.Append(tokenType, tokenStart, reader.ValueSpan.Length); |
| | 0 | 928 | | database.SetNumberOfRows(rowIndex, numberOfRowsForMembers); |
| | 0 | 929 | | database.SetNumberOfRows(newRowIndex, numberOfRowsForMembers); |
| | | 930 | | |
| | 0 | 931 | | StackRow row = stack.Pop(); |
| | 0 | 932 | | arrayItemsOrPropertyCount = row.SizeOrLength; |
| | 0 | 933 | | numberOfRowsForMembers += row.NumberOfRows; |
| | 0 | 934 | | } |
| | 1538 | 935 | | else if (tokenType == JsonTokenType.StartArray) |
| | 138 | 936 | | { |
| | 138 | 937 | | if (inArray) |
| | 0 | 938 | | { |
| | 0 | 939 | | arrayItemsOrPropertyCount++; |
| | 0 | 940 | | } |
| | | 941 | | |
| | 138 | 942 | | numberOfRowsForMembers++; |
| | 138 | 943 | | database.Append(tokenType, tokenStart, DbRow.UnknownSize); |
| | 138 | 944 | | var row = new StackRow(arrayItemsOrPropertyCount, numberOfRowsForValues + 1); |
| | 138 | 945 | | stack.Push(row); |
| | 138 | 946 | | arrayItemsOrPropertyCount = 0; |
| | 138 | 947 | | numberOfRowsForValues = 0; |
| | 138 | 948 | | } |
| | 1400 | 949 | | else if (tokenType == JsonTokenType.EndArray) |
| | 138 | 950 | | { |
| | 138 | 951 | | int rowIndex = database.FindIndexOfFirstUnsetSizeOrLength(JsonTokenType.StartArray); |
| | | 952 | | |
| | 138 | 953 | | numberOfRowsForValues++; |
| | 138 | 954 | | numberOfRowsForMembers++; |
| | 138 | 955 | | database.SetLength(rowIndex, arrayItemsOrPropertyCount); |
| | 138 | 956 | | database.SetNumberOfRows(rowIndex, numberOfRowsForValues); |
| | | 957 | | |
| | | 958 | | // If the array item count is (e.g.) 12 and the number of rows is (e.g.) 13 |
| | | 959 | | // then the extra row is just this EndArray item, so the array was made up |
| | | 960 | | // of simple values. |
| | | 961 | | // |
| | | 962 | | // If the off-by-one relationship does not hold, then one of the values was |
| | | 963 | | // more than one row, making it a complex object. |
| | | 964 | | // |
| | | 965 | | // This check is similar to tracking the start array and painting it when |
| | | 966 | | // StartObject or StartArray is encountered, but avoids the mixed state |
| | | 967 | | // where "UnknownSize" implies "has complex children". |
| | 138 | 968 | | if (arrayItemsOrPropertyCount + 1 != numberOfRowsForValues) |
| | 0 | 969 | | { |
| | 0 | 970 | | database.SetHasComplexChildren(rowIndex); |
| | 0 | 971 | | } |
| | | 972 | | |
| | 138 | 973 | | int newRowIndex = database.Length; |
| | 138 | 974 | | database.Append(tokenType, tokenStart, reader.ValueSpan.Length); |
| | 138 | 975 | | database.SetNumberOfRows(newRowIndex, numberOfRowsForValues); |
| | | 976 | | |
| | 138 | 977 | | StackRow row = stack.Pop(); |
| | 138 | 978 | | arrayItemsOrPropertyCount = row.SizeOrLength; |
| | 138 | 979 | | numberOfRowsForValues += row.NumberOfRows; |
| | 138 | 980 | | } |
| | 1262 | 981 | | else if (tokenType == JsonTokenType.PropertyName) |
| | 0 | 982 | | { |
| | 0 | 983 | | numberOfRowsForValues++; |
| | 0 | 984 | | numberOfRowsForMembers++; |
| | 0 | 985 | | arrayItemsOrPropertyCount++; |
| | | 986 | | |
| | | 987 | | // Adding 1 to skip the start quote will never overflow |
| | 0 | 988 | | Debug.Assert(tokenStart < int.MaxValue); |
| | | 989 | | |
| | 0 | 990 | | database.Append(tokenType, tokenStart + 1, reader.ValueSpan.Length); |
| | | 991 | | |
| | 0 | 992 | | if (reader.ValueIsEscaped) |
| | 0 | 993 | | { |
| | 0 | 994 | | database.SetHasComplexChildren(database.Length - DbRow.Size); |
| | 0 | 995 | | } |
| | | 996 | | |
| | 0 | 997 | | Debug.Assert(!inArray); |
| | 0 | 998 | | } |
| | | 999 | | else |
| | 1262 | 1000 | | { |
| | 1262 | 1001 | | Debug.Assert(tokenType >= JsonTokenType.String && tokenType <= JsonTokenType.Null); |
| | 1262 | 1002 | | numberOfRowsForValues++; |
| | 1262 | 1003 | | numberOfRowsForMembers++; |
| | | 1004 | | |
| | 1262 | 1005 | | if (inArray) |
| | 0 | 1006 | | { |
| | 0 | 1007 | | arrayItemsOrPropertyCount++; |
| | 0 | 1008 | | } |
| | | 1009 | | |
| | 1262 | 1010 | | if (tokenType == JsonTokenType.String) |
| | 846 | 1011 | | { |
| | | 1012 | | // Adding 1 to skip the start quote will never overflow |
| | 846 | 1013 | | Debug.Assert(tokenStart < int.MaxValue); |
| | | 1014 | | |
| | 846 | 1015 | | database.Append(tokenType, tokenStart + 1, reader.ValueSpan.Length); |
| | | 1016 | | |
| | 846 | 1017 | | if (reader.ValueIsEscaped) |
| | 0 | 1018 | | { |
| | 0 | 1019 | | database.SetHasComplexChildren(database.Length - DbRow.Size); |
| | 0 | 1020 | | } |
| | 846 | 1021 | | } |
| | | 1022 | | else |
| | 416 | 1023 | | { |
| | 416 | 1024 | | database.Append(tokenType, tokenStart, reader.ValueSpan.Length); |
| | 416 | 1025 | | } |
| | 1262 | 1026 | | } |
| | | 1027 | | |
| | 1538 | 1028 | | inArray = reader.IsInArray; |
| | 1538 | 1029 | | } |
| | | 1030 | | |
| | 1400 | 1031 | | Debug.Assert(reader.BytesConsumed == utf8JsonSpan.Length); |
| | 1400 | 1032 | | database.CompleteAllocations(); |
| | 1400 | 1033 | | } |
| | | 1034 | | |
| | | 1035 | | private static void ValidateNoDuplicateProperties(JsonDocument document) |
| | 0 | 1036 | | { |
| | 0 | 1037 | | if (document.RootElement.ValueKind is JsonValueKind.Array or JsonValueKind.Object) |
| | 0 | 1038 | | { |
| | 0 | 1039 | | ValidateDuplicatePropertiesCore(document); |
| | 0 | 1040 | | } |
| | 0 | 1041 | | } |
| | | 1042 | | |
| | | 1043 | | private static void ValidateDuplicatePropertiesCore(JsonDocument document) |
| | 0 | 1044 | | { |
| | 0 | 1045 | | Debug.Assert(document.RootElement.ValueKind is JsonValueKind.Array or JsonValueKind.Object); |
| | | 1046 | | |
| | 0 | 1047 | | using PropertyNameSet propertyNameSet = new PropertyNameSet(); |
| | | 1048 | | |
| | 0 | 1049 | | Stack<int> traversalPath = new Stack<int>(); |
| | 0 | 1050 | | int? databaseIndexOflastProcessedChild = null; |
| | | 1051 | | |
| | 0 | 1052 | | traversalPath.Push(document.RootElement.MetadataDbIndex); |
| | | 1053 | | |
| | | 1054 | | do |
| | 0 | 1055 | | { |
| | 0 | 1056 | | JsonElement curr = new JsonElement(document, traversalPath.Peek()); |
| | | 1057 | | |
| | 0 | 1058 | | switch (curr.ValueKind) |
| | | 1059 | | { |
| | | 1060 | | case JsonValueKind.Object: |
| | 0 | 1061 | | { |
| | 0 | 1062 | | JsonElement.ObjectEnumerator enumerator = new(curr, databaseIndexOflastProcessedChild ?? -1); |
| | | 1063 | | |
| | 0 | 1064 | | while (enumerator.MoveNext()) |
| | 0 | 1065 | | { |
| | 0 | 1066 | | if (enumerator.Current.Value.ValueKind is JsonValueKind.Object or JsonValueKind.Array) |
| | 0 | 1067 | | { |
| | 0 | 1068 | | traversalPath.Push(enumerator.Current.Value.MetadataDbIndex); |
| | 0 | 1069 | | databaseIndexOflastProcessedChild = null; |
| | 0 | 1070 | | goto continueOuter; |
| | | 1071 | | } |
| | 0 | 1072 | | } |
| | | 1073 | | |
| | | 1074 | | // No more children, so process the current element. |
| | 0 | 1075 | | enumerator.Reset(); |
| | 0 | 1076 | | propertyNameSet.SetCapacity(curr.GetPropertyCount()); |
| | | 1077 | | |
| | 0 | 1078 | | foreach (JsonProperty property in enumerator) |
| | 0 | 1079 | | { |
| | 0 | 1080 | | propertyNameSet.AddPropertyName(property, document); |
| | 0 | 1081 | | } |
| | | 1082 | | |
| | 0 | 1083 | | propertyNameSet.Reset(); |
| | 0 | 1084 | | databaseIndexOflastProcessedChild = traversalPath.Pop(); |
| | 0 | 1085 | | break; |
| | | 1086 | | } |
| | | 1087 | | case JsonValueKind.Array: |
| | 0 | 1088 | | { |
| | 0 | 1089 | | JsonElement.ArrayEnumerator enumerator = new(curr, databaseIndexOflastProcessedChild ?? -1); |
| | | 1090 | | |
| | 0 | 1091 | | while (enumerator.MoveNext()) |
| | 0 | 1092 | | { |
| | 0 | 1093 | | if (enumerator.Current.ValueKind is JsonValueKind.Object or JsonValueKind.Array) |
| | 0 | 1094 | | { |
| | 0 | 1095 | | traversalPath.Push(enumerator.Current.MetadataDbIndex); |
| | 0 | 1096 | | databaseIndexOflastProcessedChild = null; |
| | 0 | 1097 | | goto continueOuter; |
| | | 1098 | | } |
| | 0 | 1099 | | } |
| | | 1100 | | |
| | 0 | 1101 | | databaseIndexOflastProcessedChild = traversalPath.Pop(); |
| | 0 | 1102 | | break; |
| | | 1103 | | } |
| | | 1104 | | default: |
| | 0 | 1105 | | Debug.Fail($"Expected only complex children but got {curr.ValueKind}"); |
| | | 1106 | | ThrowHelper.ThrowJsonException(); |
| | | 1107 | | break; |
| | | 1108 | | } |
| | | 1109 | | |
| | 0 | 1110 | | continueOuter: |
| | 0 | 1111 | | ; |
| | 0 | 1112 | | } while (traversalPath.Count is not 0); |
| | 0 | 1113 | | } |
| | | 1114 | | |
| | | 1115 | | private void CheckNotDisposed() |
| | 224 | 1116 | | { |
| | 224 | 1117 | | if (_utf8Json.IsEmpty) |
| | 0 | 1118 | | { |
| | 0 | 1119 | | ThrowHelper.ThrowObjectDisposedException_JsonDocument(); |
| | | 1120 | | } |
| | 224 | 1121 | | } |
| | | 1122 | | |
| | | 1123 | | private static void CheckExpectedType(JsonTokenType expected, JsonTokenType actual) |
| | 3 | 1124 | | { |
| | 3 | 1125 | | if (expected != actual) |
| | 0 | 1126 | | { |
| | 0 | 1127 | | ThrowHelper.ThrowJsonElementWrongTypeException(expected, actual); |
| | | 1128 | | } |
| | 3 | 1129 | | } |
| | | 1130 | | |
| | | 1131 | | private static void CheckSupportedOptions( |
| | | 1132 | | JsonReaderOptions readerOptions, |
| | | 1133 | | string paramName) |
| | 2642 | 1134 | | { |
| | | 1135 | | // Since these are coming from a valid instance of Utf8JsonReader, the JsonReaderOptions must already be val |
| | 2642 | 1136 | | Debug.Assert(readerOptions.CommentHandling >= 0 && readerOptions.CommentHandling <= JsonCommentHandling.Allo |
| | | 1137 | | |
| | 2642 | 1138 | | if (readerOptions.CommentHandling == JsonCommentHandling.Allow) |
| | 0 | 1139 | | { |
| | 0 | 1140 | | throw new ArgumentException(SR.JsonDocumentDoesNotSupportComments, paramName); |
| | | 1141 | | } |
| | 2642 | 1142 | | } |
| | | 1143 | | } |
| | | 1144 | | } |