| | | 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.Diagnostics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides a high-performance API for forward-only, read-only access to the UTF-8 encoded JSON text. |
| | | 12 | | /// It processes the text sequentially with no caching and adheres strictly to the JSON RFC |
| | | 13 | | /// by default (https://tools.ietf.org/html/rfc8259). When it encounters invalid JSON, it throws |
| | | 14 | | /// a JsonException with basic error information like line number and byte position on the line. |
| | | 15 | | /// Since this type is a ref struct, it does not directly support async. However, it does provide |
| | | 16 | | /// support for reentrancy to read incomplete data, and continue reading once more data is presented. |
| | | 17 | | /// To be able to set max depth while reading OR allow skipping comments, create an instance of |
| | | 18 | | /// <see cref="JsonReaderState"/> and pass that in to the reader. |
| | | 19 | | /// </summary> |
| | | 20 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 21 | | public ref partial struct Utf8JsonReader |
| | | 22 | | { |
| | | 23 | | private ReadOnlySpan<byte> _buffer; |
| | | 24 | | |
| | | 25 | | private readonly bool _isFinalBlock; |
| | | 26 | | private readonly bool _isInputSequence; |
| | | 27 | | |
| | | 28 | | private long _lineNumber; |
| | | 29 | | private long _bytePositionInLine; |
| | | 30 | | |
| | | 31 | | // bytes consumed in the current segment (not token) |
| | | 32 | | private int _consumed; |
| | | 33 | | private bool _inObject; |
| | | 34 | | private bool _isNotPrimitive; |
| | | 35 | | private JsonTokenType _tokenType; |
| | | 36 | | private JsonTokenType _previousTokenType; |
| | | 37 | | private JsonReaderOptions _readerOptions; |
| | | 38 | | private BitStack _bitStack; |
| | | 39 | | |
| | | 40 | | private long _totalConsumed; |
| | | 41 | | private bool _isLastSegment; |
| | | 42 | | private readonly bool _isMultiSegment; |
| | | 43 | | private bool _trailingCommaBeforeComment; |
| | | 44 | | |
| | | 45 | | private SequencePosition _nextPosition; |
| | | 46 | | private SequencePosition _currentPosition; |
| | | 47 | | private readonly ReadOnlySequence<byte> _sequence; |
| | | 48 | | |
| | 12791 | 49 | | private readonly bool IsLastSpan => _isFinalBlock && (!_isMultiSegment || _isLastSegment); |
| | | 50 | | |
| | 984 | 51 | | internal readonly ReadOnlySequence<byte> OriginalSequence => _sequence; |
| | | 52 | | |
| | 492 | 53 | | internal readonly ReadOnlySpan<byte> OriginalSpan => _sequence.IsEmpty ? _buffer : default; |
| | | 54 | | |
| | 3208 | 55 | | internal readonly int ValueLength => HasValueSequence ? checked((int)ValueSequence.Length) : ValueSpan.Length; |
| | | 56 | | |
| | 4598 | 57 | | internal readonly bool AllowMultipleValues => _readerOptions.AllowMultipleValues; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the value of the last processed token as a ReadOnlySpan<byte> slice |
| | | 61 | | /// of the input payload. If the JSON is provided within a ReadOnlySequence<byte> |
| | | 62 | | /// and the slice that represents the token value fits in a single segment, then |
| | | 63 | | /// <see cref="ValueSpan"/> will contain the sliced value since it can be represented as a span. |
| | | 64 | | /// Otherwise, the <see cref="ValueSequence"/> will contain the token value. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <remarks> |
| | | 67 | | /// If <see cref="HasValueSequence"/> is true, <see cref="ValueSpan"/> contains useless data, likely for |
| | | 68 | | /// a previous single-segment token. Therefore, only access <see cref="ValueSpan"/> if <see cref="HasValueSequen |
| | | 69 | | /// Otherwise, the token value must be accessed from <see cref="ValueSequence"/>. |
| | | 70 | | /// </remarks> |
| | 163367 | 71 | | public ReadOnlySpan<byte> ValueSpan { get; private set; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Returns the total amount of bytes consumed by the <see cref="Utf8JsonReader"/> so far |
| | | 75 | | /// for the current instance of the <see cref="Utf8JsonReader"/> with the given UTF-8 encoded input text. |
| | | 76 | | /// </summary> |
| | | 77 | | public readonly long BytesConsumed |
| | | 78 | | { |
| | | 79 | | get |
| | 121610 | 80 | | { |
| | | 81 | | #if DEBUG |
| | 121610 | 82 | | if (!_isInputSequence) |
| | 44615 | 83 | | { |
| | 44615 | 84 | | Debug.Assert(_totalConsumed == 0); |
| | 44615 | 85 | | } |
| | | 86 | | #endif |
| | 121610 | 87 | | return _totalConsumed + _consumed; |
| | 121610 | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Returns the index that the last processed JSON token starts at |
| | | 93 | | /// within the given UTF-8 encoded input text, skipping any white space. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <remarks> |
| | | 96 | | /// For JSON strings (including property names), this points to before the start quote. |
| | | 97 | | /// For comments, this points to before the first comment delimiter (i.e. '/'). |
| | | 98 | | /// </remarks> |
| | 135608 | 99 | | public long TokenStartIndex { get; private set; } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Tracks the recursive depth of the nested objects / arrays within the JSON text |
| | | 103 | | /// processed so far. This provides the depth of the current token. |
| | | 104 | | /// </summary> |
| | | 105 | | public readonly int CurrentDepth |
| | | 106 | | { |
| | | 107 | | get |
| | 28244 | 108 | | { |
| | 28244 | 109 | | int readerDepth = _bitStack.CurrentDepth; |
| | 28244 | 110 | | if (TokenType is JsonTokenType.StartArray or JsonTokenType.StartObject) |
| | 11668 | 111 | | { |
| | 11668 | 112 | | Debug.Assert(readerDepth >= 1); |
| | 11668 | 113 | | readerDepth--; |
| | 11668 | 114 | | } |
| | 28244 | 115 | | return readerDepth; |
| | 28244 | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 1538 | 119 | | internal readonly bool IsInArray => !_inObject; |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Gets the type of the last processed JSON token in the UTF-8 encoded JSON text. |
| | | 123 | | /// </summary> |
| | 151174 | 124 | | public readonly JsonTokenType TokenType => _tokenType; |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Lets the caller know which of the two 'Value' properties to read to get the |
| | | 128 | | /// token value. For input data within a ReadOnlySpan<byte> this will |
| | | 129 | | /// always return false. For input data within a ReadOnlySequence<byte>, this |
| | | 130 | | /// will only return true if the token value straddles more than a single segment and |
| | | 131 | | /// hence couldn't be represented as a span. |
| | | 132 | | /// </summary> |
| | 118967 | 133 | | public bool HasValueSequence { get; private set; } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Lets the caller know whether the current <see cref="ValueSpan" /> or <see cref="ValueSequence"/> properties |
| | | 137 | | /// contain escape sequences per RFC 8259 section 7, and therefore require unescaping before being consumed. |
| | | 138 | | /// </summary> |
| | 264568 | 139 | | public bool ValueIsEscaped { get; private set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Returns the mode of this instance of the <see cref="Utf8JsonReader"/>. |
| | | 143 | | /// True when the reader was constructed with the input span containing the entire data to process. |
| | | 144 | | /// False when the reader was constructed knowing that the input span may contain partial data with more data to |
| | | 145 | | /// </summary> |
| | 52382 | 146 | | public readonly bool IsFinalBlock => _isFinalBlock; |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Gets the value of the last processed token as a ReadOnlySpan<byte> slice |
| | | 150 | | /// of the input payload. If the JSON is provided within a ReadOnlySequence<byte> |
| | | 151 | | /// and the slice that represents the token value fits in a single segment, then |
| | | 152 | | /// <see cref="ValueSpan"/> will contain the sliced value since it can be represented as a span. |
| | | 153 | | /// Otherwise, the <see cref="ValueSequence"/> will contain the token value. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <remarks> |
| | | 156 | | /// If <see cref="HasValueSequence"/> is false, <see cref="ValueSequence"/> contains useless data, likely for |
| | | 157 | | /// a previous multi-segment token. Therefore, only access <see cref="ValueSequence"/> if <see cref="HasValueSeq |
| | | 158 | | /// Otherwise, the token value must be accessed from <see cref="ValueSpan"/>. |
| | | 159 | | /// </remarks> |
| | 101765 | 160 | | public ReadOnlySequence<byte> ValueSequence { get; private set; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Returns the current <see cref="SequencePosition"/> within the provided UTF-8 encoded |
| | | 164 | | /// input ReadOnlySequence<byte>. If the <see cref="Utf8JsonReader"/> was constructed |
| | | 165 | | /// with a ReadOnlySpan<byte> instead, this will always return a default <see cref="SequencePosition"/>. |
| | | 166 | | /// </summary> |
| | | 167 | | public readonly SequencePosition Position |
| | | 168 | | { |
| | | 169 | | get |
| | 0 | 170 | | { |
| | 0 | 171 | | if (_isInputSequence) |
| | 0 | 172 | | { |
| | 0 | 173 | | Debug.Assert(_currentPosition.GetObject() != null); |
| | 0 | 174 | | return _sequence.GetPosition(_consumed, _currentPosition); |
| | | 175 | | } |
| | 0 | 176 | | return default; |
| | 0 | 177 | | } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Returns the current snapshot of the <see cref="Utf8JsonReader"/> state which must |
| | | 182 | | /// be captured by the caller and passed back in to the <see cref="Utf8JsonReader"/> ctor with more data. |
| | | 183 | | /// Unlike the <see cref="Utf8JsonReader"/>, which is a ref struct, the state can survive |
| | | 184 | | /// across async/await boundaries and hence this type is required to provide support for reading |
| | | 185 | | /// in more data asynchronously before continuing with a new instance of the <see cref="Utf8JsonReader"/>. |
| | | 186 | | /// </summary> |
| | 116968 | 187 | | public readonly JsonReaderState CurrentState => new JsonReaderState |
| | 116968 | 188 | | ( |
| | 116968 | 189 | | lineNumber: _lineNumber, |
| | 116968 | 190 | | bytePositionInLine: _bytePositionInLine, |
| | 116968 | 191 | | inObject: _inObject, |
| | 116968 | 192 | | isNotPrimitive: _isNotPrimitive, |
| | 116968 | 193 | | valueIsEscaped: ValueIsEscaped, |
| | 116968 | 194 | | trailingCommaBeforeComment: _trailingCommaBeforeComment, |
| | 116968 | 195 | | tokenType: _tokenType, |
| | 116968 | 196 | | previousTokenType: _previousTokenType, |
| | 116968 | 197 | | readerOptions: _readerOptions, |
| | 116968 | 198 | | bitStack: _bitStack |
| | 116968 | 199 | | ); |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Constructs a new <see cref="Utf8JsonReader"/> instance. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <param name="jsonData">The ReadOnlySpan<byte> containing the UTF-8 encoded JSON text to process.</para |
| | | 205 | | /// <param name="isFinalBlock">True when the input span contains the entire data to process. |
| | | 206 | | /// Set to false only if it is known that the input span contains partial data with more data to follow.</param> |
| | | 207 | | /// <param name="state">If this is the first call to the ctor, pass in a default state. Otherwise, |
| | | 208 | | /// capture the state from the previous instance of the <see cref="Utf8JsonReader"/> and pass that back.</param> |
| | | 209 | | /// <remarks> |
| | | 210 | | /// Since this type is a ref struct, it is a stack-only type and all the limitations of ref structs apply to it. |
| | | 211 | | /// This is the reason why the ctor accepts a <see cref="JsonReaderState"/>. |
| | | 212 | | /// </remarks> |
| | | 213 | | public Utf8JsonReader(ReadOnlySpan<byte> jsonData, bool isFinalBlock, JsonReaderState state) |
| | 30072 | 214 | | { |
| | 30072 | 215 | | _buffer = jsonData; |
| | | 216 | | |
| | 30072 | 217 | | _isFinalBlock = isFinalBlock; |
| | 30072 | 218 | | _isInputSequence = false; |
| | | 219 | | |
| | 30072 | 220 | | _lineNumber = state._lineNumber; |
| | 30072 | 221 | | _bytePositionInLine = state._bytePositionInLine; |
| | 30072 | 222 | | _inObject = state._inObject; |
| | 30072 | 223 | | _isNotPrimitive = state._isNotPrimitive; |
| | 30072 | 224 | | ValueIsEscaped = state._valueIsEscaped; |
| | 30072 | 225 | | _trailingCommaBeforeComment = state._trailingCommaBeforeComment; |
| | 30072 | 226 | | _tokenType = state._tokenType; |
| | 30072 | 227 | | _previousTokenType = state._previousTokenType; |
| | 30072 | 228 | | _readerOptions = state._readerOptions; |
| | 30072 | 229 | | if (_readerOptions.MaxDepth == 0) |
| | 710 | 230 | | { |
| | 710 | 231 | | _readerOptions.MaxDepth = JsonReaderOptions.DefaultMaxDepth; // If max depth is not set, revert to the |
| | 710 | 232 | | } |
| | 30072 | 233 | | _bitStack = state._bitStack; |
| | | 234 | | |
| | 30072 | 235 | | _consumed = 0; |
| | 30072 | 236 | | TokenStartIndex = 0; |
| | 30072 | 237 | | _totalConsumed = 0; |
| | 30072 | 238 | | _isLastSegment = _isFinalBlock; |
| | 30072 | 239 | | _isMultiSegment = false; |
| | | 240 | | |
| | 30072 | 241 | | ValueSpan = ReadOnlySpan<byte>.Empty; |
| | | 242 | | |
| | 30072 | 243 | | _currentPosition = default; |
| | 30072 | 244 | | _nextPosition = default; |
| | 30072 | 245 | | _sequence = default; |
| | 30072 | 246 | | HasValueSequence = false; |
| | 30072 | 247 | | ValueSequence = ReadOnlySequence<byte>.Empty; |
| | 30072 | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <summary> |
| | | 251 | | /// Constructs a new <see cref="Utf8JsonReader"/> instance. |
| | | 252 | | /// </summary> |
| | | 253 | | /// <param name="jsonData">The ReadOnlySpan<byte> containing the UTF-8 encoded JSON text to process.</para |
| | | 254 | | /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonReader"/> |
| | | 255 | | /// that is different from the JSON RFC (for example how to handle comments or maximum depth allowed when readin |
| | | 256 | | /// By default, the <see cref="Utf8JsonReader"/> follows the JSON RFC strictly (i.e. comments within the JSON ar |
| | | 257 | | /// <remarks> |
| | | 258 | | /// <para> |
| | | 259 | | /// Since this type is a ref struct, it is a stack-only type and all the limitations of ref structs apply to |
| | | 260 | | /// </para> |
| | | 261 | | /// <para> |
| | | 262 | | /// This assumes that the entire JSON payload is passed in (equivalent to <see cref="IsFinalBlock"/> = true) |
| | | 263 | | /// </para> |
| | | 264 | | /// </remarks> |
| | | 265 | | public Utf8JsonReader(ReadOnlySpan<byte> jsonData, JsonReaderOptions options = default) |
| | 28 | 266 | | : this(jsonData, isFinalBlock: true, new JsonReaderState(options)) |
| | 28 | 267 | | { |
| | 28 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Read the next JSON token from input source. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <returns>True if the token was read successfully, else false.</returns> |
| | | 274 | | /// <exception cref="JsonException"> |
| | | 275 | | /// Thrown when an invalid JSON token is encountered according to the JSON RFC |
| | | 276 | | /// or if the current depth exceeds the recursive limit set by the max depth. |
| | | 277 | | /// </exception> |
| | | 278 | | public bool Read() |
| | 68398 | 279 | | { |
| | 68398 | 280 | | bool retVal = _isMultiSegment ? ReadMultiSegment() : ReadSingleSegment(); |
| | | 281 | | |
| | 32148 | 282 | | if (!retVal) |
| | 2328 | 283 | | { |
| | 2328 | 284 | | if (_isFinalBlock && TokenType is JsonTokenType.None && !_readerOptions.AllowMultipleValues) |
| | 738 | 285 | | { |
| | 738 | 286 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedJsonTokens); |
| | | 287 | | } |
| | 1590 | 288 | | } |
| | 31410 | 289 | | return retVal; |
| | 31410 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Skips the children of the current JSON token. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <exception cref="InvalidOperationException"> |
| | | 296 | | /// Thrown when the reader was given partial data with more data to follow (i.e. <see cref="IsFinalBlock"/> is f |
| | | 297 | | /// </exception> |
| | | 298 | | /// <exception cref="JsonException"> |
| | | 299 | | /// Thrown when an invalid JSON token is encountered while skipping, according to the JSON RFC, |
| | | 300 | | /// or if the current depth exceeds the recursive limit set by the max depth. |
| | | 301 | | /// </exception> |
| | | 302 | | /// <remarks> |
| | | 303 | | /// When <see cref="TokenType"/> is <see cref="JsonTokenType.PropertyName" />, the reader first moves to the pro |
| | | 304 | | /// When <see cref="TokenType"/> (originally, or after advancing) is <see cref="JsonTokenType.StartObject" /> or |
| | | 305 | | /// <see cref="JsonTokenType.StartArray" />, the reader advances to the matching |
| | | 306 | | /// <see cref="JsonTokenType.EndObject" /> or <see cref="JsonTokenType.EndArray" />. |
| | | 307 | | /// |
| | | 308 | | /// For all other token types, the reader does not move. After the next call to <see cref="Read"/>, the reader w |
| | | 309 | | /// the next value (when in an array), the next property name (when in an object), or the end array/object token |
| | | 310 | | /// </remarks> |
| | | 311 | | public void Skip() |
| | 0 | 312 | | { |
| | 0 | 313 | | if (!_isFinalBlock) |
| | 0 | 314 | | { |
| | 0 | 315 | | ThrowHelper.ThrowInvalidOperationException_CannotSkipOnPartial(); |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | SkipHelper(); |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 322 | | private void SkipHelper() |
| | 1380 | 323 | | { |
| | 1380 | 324 | | Debug.Assert(_isFinalBlock); |
| | | 325 | | |
| | 1380 | 326 | | if (TokenType is JsonTokenType.PropertyName) |
| | 0 | 327 | | { |
| | 0 | 328 | | bool result = Read(); |
| | | 329 | | // Since _isFinalBlock == true here, and the JSON token is not a primitive value or comment. |
| | | 330 | | // Read() is guaranteed to return true OR throw for invalid/incomplete data. |
| | 0 | 331 | | Debug.Assert(result); |
| | 0 | 332 | | } |
| | | 333 | | |
| | 1380 | 334 | | if (TokenType is JsonTokenType.StartObject or JsonTokenType.StartArray) |
| | 1380 | 335 | | { |
| | 1380 | 336 | | int depth = CurrentDepth; |
| | | 337 | | do |
| | 2558 | 338 | | { |
| | 2558 | 339 | | bool result = Read(); |
| | | 340 | | // Since _isFinalBlock == true here, and the JSON token is not a primitive value or comment. |
| | | 341 | | // Read() is guaranteed to return true OR throw for invalid/incomplete data. |
| | 1316 | 342 | | Debug.Assert(result); |
| | 1316 | 343 | | } |
| | 1316 | 344 | | while (depth < CurrentDepth); |
| | 138 | 345 | | } |
| | 138 | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Tries to skip the children of the current JSON token. |
| | | 350 | | /// </summary> |
| | | 351 | | /// <returns>True if there was enough data for the children to be skipped successfully, else false.</returns> |
| | | 352 | | /// <exception cref="JsonException"> |
| | | 353 | | /// Thrown when an invalid JSON token is encountered while skipping, according to the JSON RFC, |
| | | 354 | | /// or if the current depth exceeds the recursive limit set by the max depth. |
| | | 355 | | /// </exception> |
| | | 356 | | /// <remarks> |
| | | 357 | | /// <para> |
| | | 358 | | /// If the reader did not have enough data to completely skip the children of the current token, |
| | | 359 | | /// it will be reset to the state it was in before the method was called. |
| | | 360 | | /// </para> |
| | | 361 | | /// <para> |
| | | 362 | | /// When <see cref="TokenType"/> is <see cref="JsonTokenType.PropertyName" />, the reader first moves to the |
| | | 363 | | /// When <see cref="TokenType"/> (originally, or after advancing) is <see cref="JsonTokenType.StartObject" / |
| | | 364 | | /// <see cref="JsonTokenType.StartArray" />, the reader advances to the matching |
| | | 365 | | /// <see cref="JsonTokenType.EndObject" /> or <see cref="JsonTokenType.EndArray" />. |
| | | 366 | | /// |
| | | 367 | | /// For all other token types, the reader does not move. After the next call to <see cref="Read"/>, the read |
| | | 368 | | /// the next value (when in an array), the next property name (when in an object), or the end array/object t |
| | | 369 | | /// </para> |
| | | 370 | | /// </remarks> |
| | | 371 | | public bool TrySkip() |
| | 1380 | 372 | | { |
| | 1380 | 373 | | if (_isFinalBlock) |
| | 1380 | 374 | | { |
| | 1380 | 375 | | SkipHelper(); |
| | 138 | 376 | | return true; |
| | | 377 | | } |
| | | 378 | | |
| | 0 | 379 | | Utf8JsonReader restore = this; |
| | 0 | 380 | | bool success = TrySkipPartial(targetDepth: CurrentDepth); |
| | 0 | 381 | | if (!success) |
| | 0 | 382 | | { |
| | | 383 | | // Roll back the reader if it contains partial data. |
| | 0 | 384 | | this = restore; |
| | 0 | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | return success; |
| | 138 | 388 | | } |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Tries to skip the children of the current JSON token, advancing the reader even if there is not enough data. |
| | | 392 | | /// The skip operation can be resumed later, provided that the same <paramref name="targetDepth" /> is passed. |
| | | 393 | | /// </summary> |
| | | 394 | | /// <param name="targetDepth">The target depth we want to eventually skip to.</param> |
| | | 395 | | /// <returns>True if the entire JSON value has been skipped.</returns> |
| | | 396 | | internal bool TrySkipPartial(int targetDepth) |
| | 0 | 397 | | { |
| | 0 | 398 | | Debug.Assert(0 <= targetDepth && targetDepth <= CurrentDepth); |
| | | 399 | | |
| | 0 | 400 | | if (targetDepth == CurrentDepth) |
| | 0 | 401 | | { |
| | | 402 | | // This is the first call to TrySkipHelper. |
| | 0 | 403 | | if (TokenType is JsonTokenType.PropertyName) |
| | 0 | 404 | | { |
| | | 405 | | // Skip any property name tokens preceding the value. |
| | 0 | 406 | | if (!Read()) |
| | 0 | 407 | | { |
| | 0 | 408 | | return false; |
| | | 409 | | } |
| | 0 | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | if (TokenType is not (JsonTokenType.StartObject or JsonTokenType.StartArray)) |
| | 0 | 413 | | { |
| | | 414 | | // The next value is not an object or array, so there is nothing to skip. |
| | 0 | 415 | | return true; |
| | | 416 | | } |
| | 0 | 417 | | } |
| | | 418 | | |
| | | 419 | | // Start or resume iterating through the JSON object or array. |
| | | 420 | | do |
| | 0 | 421 | | { |
| | 0 | 422 | | if (!Read()) |
| | 0 | 423 | | { |
| | 0 | 424 | | return false; |
| | | 425 | | } |
| | 0 | 426 | | } |
| | 0 | 427 | | while (targetDepth < CurrentDepth); |
| | | 428 | | |
| | 0 | 429 | | Debug.Assert(targetDepth == CurrentDepth); |
| | 0 | 430 | | return true; |
| | 0 | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Compares the UTF-8 encoded text to the unescaped JSON token value in the source and returns true if they mat |
| | | 435 | | /// </summary> |
| | | 436 | | /// <param name="utf8Text">The UTF-8 encoded text to compare against.</param> |
| | | 437 | | /// <returns>True if the JSON token value in the source matches the UTF-8 encoded look up text.</returns> |
| | | 438 | | /// <exception cref="InvalidOperationException"> |
| | | 439 | | /// Thrown if trying to find a text match on a JSON token that is not a string |
| | | 440 | | /// (i.e. other than <see cref="JsonTokenType.String"/> or <see cref="JsonTokenType.PropertyName"/>). |
| | | 441 | | /// <seealso cref="TokenType" /> |
| | | 442 | | /// </exception> |
| | | 443 | | /// <remarks> |
| | | 444 | | /// <para> |
| | | 445 | | /// If the look up text is invalid UTF-8 text, the method will return false since you cannot have |
| | | 446 | | /// invalid UTF-8 within the JSON payload. |
| | | 447 | | /// </para> |
| | | 448 | | /// <para> |
| | | 449 | | /// The comparison of the JSON token value in the source and the look up text is done by first unescaping th |
| | | 450 | | /// if required. The look up text is matched as is, without any modifications to it. |
| | | 451 | | /// </para> |
| | | 452 | | /// </remarks> |
| | | 453 | | public readonly bool ValueTextEquals(ReadOnlySpan<byte> utf8Text) |
| | 0 | 454 | | { |
| | 0 | 455 | | if (!IsTokenTypeString(TokenType)) |
| | 0 | 456 | | { |
| | 0 | 457 | | ThrowHelper.ThrowInvalidOperationException_ExpectedStringComparison(TokenType); |
| | | 458 | | } |
| | | 459 | | |
| | 0 | 460 | | return TextEqualsHelper(utf8Text); |
| | 0 | 461 | | } |
| | | 462 | | |
| | | 463 | | /// <summary> |
| | | 464 | | /// Compares the string text to the unescaped JSON token value in the source and returns true if they match. |
| | | 465 | | /// </summary> |
| | | 466 | | /// <param name="text">The text to compare against.</param> |
| | | 467 | | /// <returns>True if the JSON token value in the source matches the look up text.</returns> |
| | | 468 | | /// <exception cref="InvalidOperationException"> |
| | | 469 | | /// Thrown if trying to find a text match on a JSON token that is not a string |
| | | 470 | | /// (i.e. other than <see cref="JsonTokenType.String"/> or <see cref="JsonTokenType.PropertyName"/>). |
| | | 471 | | /// <seealso cref="TokenType" /> |
| | | 472 | | /// </exception> |
| | | 473 | | /// <remarks> |
| | | 474 | | /// <para> |
| | | 475 | | /// If the look up text is invalid UTF-8 text, the method will return false since you cannot have |
| | | 476 | | /// invalid UTF-8 within the JSON payload. |
| | | 477 | | /// </para> |
| | | 478 | | /// <para> |
| | | 479 | | /// The comparison of the JSON token value in the source and the look up text is done by first unescaping th |
| | | 480 | | /// if required. The look up text is matched as is, without any modifications to it. |
| | | 481 | | /// </para> |
| | | 482 | | /// </remarks> |
| | | 483 | | public readonly bool ValueTextEquals(string? text) |
| | 0 | 484 | | { |
| | 0 | 485 | | return ValueTextEquals(text.AsSpan()); |
| | 0 | 486 | | } |
| | | 487 | | |
| | | 488 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 489 | | private readonly bool TextEqualsHelper(ReadOnlySpan<byte> otherUtf8Text) |
| | 0 | 490 | | { |
| | 0 | 491 | | if (HasValueSequence) |
| | 0 | 492 | | { |
| | 0 | 493 | | return CompareToSequence(otherUtf8Text); |
| | | 494 | | } |
| | | 495 | | |
| | 0 | 496 | | if (ValueIsEscaped) |
| | 0 | 497 | | { |
| | 0 | 498 | | return UnescapeAndCompare(otherUtf8Text); |
| | | 499 | | } |
| | | 500 | | |
| | 0 | 501 | | return otherUtf8Text.SequenceEqual(ValueSpan); |
| | 0 | 502 | | } |
| | | 503 | | |
| | | 504 | | /// <summary> |
| | | 505 | | /// Compares the text to the unescaped JSON token value in the source and returns true if they match. |
| | | 506 | | /// </summary> |
| | | 507 | | /// <param name="text">The text to compare against.</param> |
| | | 508 | | /// <returns>True if the JSON token value in the source matches the look up text.</returns> |
| | | 509 | | /// <exception cref="InvalidOperationException"> |
| | | 510 | | /// Thrown if trying to find a text match on a JSON token that is not a string |
| | | 511 | | /// (i.e. other than <see cref="JsonTokenType.String"/> or <see cref="JsonTokenType.PropertyName"/>). |
| | | 512 | | /// <seealso cref="TokenType" /> |
| | | 513 | | /// </exception> |
| | | 514 | | /// <remarks> |
| | | 515 | | /// <para> |
| | | 516 | | /// If the look up text is invalid or incomplete UTF-16 text (i.e. unpaired surrogates), the method will ret |
| | | 517 | | /// since you cannot have invalid UTF-16 within the JSON payload. |
| | | 518 | | /// </para> |
| | | 519 | | /// <para> |
| | | 520 | | /// The comparison of the JSON token value in the source and the look up text is done by first unescaping th |
| | | 521 | | /// if required. The look up text is matched as is, without any modifications to it. |
| | | 522 | | /// </para> |
| | | 523 | | /// </remarks> |
| | | 524 | | public readonly bool ValueTextEquals(ReadOnlySpan<char> text) |
| | 0 | 525 | | { |
| | 0 | 526 | | if (!IsTokenTypeString(TokenType)) |
| | 0 | 527 | | { |
| | 0 | 528 | | ThrowHelper.ThrowInvalidOperationException_ExpectedStringComparison(TokenType); |
| | | 529 | | } |
| | | 530 | | |
| | 0 | 531 | | if (MatchNotPossible(text.Length)) |
| | 0 | 532 | | { |
| | 0 | 533 | | return false; |
| | | 534 | | } |
| | | 535 | | |
| | 0 | 536 | | byte[]? otherUtf8TextArray = null; |
| | | 537 | | |
| | | 538 | | scoped Span<byte> otherUtf8Text; |
| | | 539 | | |
| | 0 | 540 | | int length = checked(text.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); |
| | | 541 | | |
| | 0 | 542 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 543 | | { |
| | 0 | 544 | | otherUtf8TextArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 545 | | otherUtf8Text = otherUtf8TextArray; |
| | 0 | 546 | | } |
| | | 547 | | else |
| | 0 | 548 | | { |
| | 0 | 549 | | otherUtf8Text = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 550 | | } |
| | | 551 | | |
| | 0 | 552 | | OperationStatus status = JsonWriterHelper.ToUtf8(text, otherUtf8Text, out int written); |
| | 0 | 553 | | Debug.Assert(status != OperationStatus.DestinationTooSmall); |
| | | 554 | | bool result; |
| | 0 | 555 | | if (status == OperationStatus.InvalidData) |
| | 0 | 556 | | { |
| | 0 | 557 | | result = false; |
| | 0 | 558 | | } |
| | | 559 | | else |
| | 0 | 560 | | { |
| | 0 | 561 | | Debug.Assert(status == OperationStatus.Done); |
| | 0 | 562 | | result = TextEqualsHelper(otherUtf8Text.Slice(0, written)); |
| | 0 | 563 | | } |
| | | 564 | | |
| | 0 | 565 | | if (otherUtf8TextArray != null) |
| | 0 | 566 | | { |
| | 0 | 567 | | otherUtf8Text.Slice(0, written).Clear(); |
| | 0 | 568 | | ArrayPool<byte>.Shared.Return(otherUtf8TextArray); |
| | 0 | 569 | | } |
| | | 570 | | |
| | 0 | 571 | | return result; |
| | 0 | 572 | | } |
| | | 573 | | |
| | | 574 | | private readonly bool CompareToSequence(ReadOnlySpan<byte> other) |
| | 0 | 575 | | { |
| | 0 | 576 | | Debug.Assert(HasValueSequence); |
| | | 577 | | |
| | 0 | 578 | | if (ValueIsEscaped) |
| | 0 | 579 | | { |
| | 0 | 580 | | return UnescapeSequenceAndCompare(other); |
| | | 581 | | } |
| | | 582 | | |
| | 0 | 583 | | ReadOnlySequence<byte> localSequence = ValueSequence; |
| | | 584 | | |
| | 0 | 585 | | Debug.Assert(!localSequence.IsSingleSegment); |
| | | 586 | | |
| | 0 | 587 | | if (localSequence.Length != other.Length) |
| | 0 | 588 | | { |
| | 0 | 589 | | return false; |
| | | 590 | | } |
| | | 591 | | |
| | 0 | 592 | | int matchedSoFar = 0; |
| | | 593 | | |
| | 0 | 594 | | foreach (ReadOnlyMemory<byte> memory in localSequence) |
| | 0 | 595 | | { |
| | 0 | 596 | | ReadOnlySpan<byte> span = memory.Span; |
| | | 597 | | |
| | 0 | 598 | | if (other.Slice(matchedSoFar).StartsWith(span)) |
| | 0 | 599 | | { |
| | 0 | 600 | | matchedSoFar += span.Length; |
| | 0 | 601 | | } |
| | | 602 | | else |
| | 0 | 603 | | { |
| | 0 | 604 | | return false; |
| | | 605 | | } |
| | 0 | 606 | | } |
| | 0 | 607 | | return true; |
| | 0 | 608 | | } |
| | | 609 | | |
| | | 610 | | private readonly bool UnescapeAndCompare(ReadOnlySpan<byte> other) |
| | 0 | 611 | | { |
| | 0 | 612 | | Debug.Assert(!HasValueSequence); |
| | 0 | 613 | | ReadOnlySpan<byte> localSpan = ValueSpan; |
| | | 614 | | |
| | 0 | 615 | | if (localSpan.Length < other.Length || localSpan.Length / JsonConstants.MaxExpansionFactorWhileEscaping > ot |
| | 0 | 616 | | { |
| | 0 | 617 | | return false; |
| | | 618 | | } |
| | | 619 | | |
| | 0 | 620 | | int idx = localSpan.IndexOf(JsonConstants.BackSlash); |
| | 0 | 621 | | Debug.Assert(idx != -1); |
| | | 622 | | |
| | 0 | 623 | | if (!other.StartsWith(localSpan.Slice(0, idx))) |
| | 0 | 624 | | { |
| | 0 | 625 | | return false; |
| | | 626 | | } |
| | | 627 | | |
| | 0 | 628 | | return JsonReaderHelper.UnescapeAndCompare(localSpan.Slice(idx), other.Slice(idx)); |
| | 0 | 629 | | } |
| | | 630 | | |
| | | 631 | | private readonly bool UnescapeSequenceAndCompare(ReadOnlySpan<byte> other) |
| | 0 | 632 | | { |
| | 0 | 633 | | Debug.Assert(HasValueSequence); |
| | 0 | 634 | | Debug.Assert(!ValueSequence.IsSingleSegment); |
| | | 635 | | |
| | 0 | 636 | | ReadOnlySequence<byte> localSequence = ValueSequence; |
| | 0 | 637 | | long sequenceLength = localSequence.Length; |
| | | 638 | | |
| | | 639 | | // The JSON token value will at most shrink by 6 when unescaping. |
| | | 640 | | // If it is still larger than the lookup string, there is no value in unescaping and doing the comparison. |
| | 0 | 641 | | if (sequenceLength < other.Length || sequenceLength / JsonConstants.MaxExpansionFactorWhileEscaping > other. |
| | 0 | 642 | | { |
| | 0 | 643 | | return false; |
| | | 644 | | } |
| | | 645 | | |
| | 0 | 646 | | int matchedSoFar = 0; |
| | | 647 | | |
| | 0 | 648 | | bool result = false; |
| | | 649 | | |
| | 0 | 650 | | foreach (ReadOnlyMemory<byte> memory in localSequence) |
| | 0 | 651 | | { |
| | 0 | 652 | | ReadOnlySpan<byte> span = memory.Span; |
| | | 653 | | |
| | 0 | 654 | | int idx = span.IndexOf(JsonConstants.BackSlash); |
| | | 655 | | |
| | 0 | 656 | | if (idx != -1) |
| | 0 | 657 | | { |
| | 0 | 658 | | if (!other.Slice(matchedSoFar).StartsWith(span.Slice(0, idx))) |
| | 0 | 659 | | { |
| | 0 | 660 | | break; |
| | | 661 | | } |
| | 0 | 662 | | matchedSoFar += idx; |
| | | 663 | | |
| | 0 | 664 | | other = other.Slice(matchedSoFar); |
| | 0 | 665 | | localSequence = localSequence.Slice(matchedSoFar); |
| | | 666 | | |
| | 0 | 667 | | if (localSequence.IsSingleSegment) |
| | 0 | 668 | | { |
| | 0 | 669 | | result = JsonReaderHelper.UnescapeAndCompare(localSequence.First.Span, other); |
| | 0 | 670 | | } |
| | | 671 | | else |
| | 0 | 672 | | { |
| | 0 | 673 | | result = JsonReaderHelper.UnescapeAndCompare(localSequence, other); |
| | 0 | 674 | | } |
| | 0 | 675 | | break; |
| | | 676 | | } |
| | | 677 | | |
| | 0 | 678 | | if (!other.Slice(matchedSoFar).StartsWith(span)) |
| | 0 | 679 | | { |
| | 0 | 680 | | break; |
| | | 681 | | } |
| | 0 | 682 | | matchedSoFar += span.Length; |
| | 0 | 683 | | } |
| | | 684 | | |
| | 0 | 685 | | return result; |
| | 0 | 686 | | } |
| | | 687 | | |
| | | 688 | | // Returns true if the TokenType is a primitive string "value", i.e. PropertyName or String |
| | | 689 | | // Otherwise, return false. |
| | | 690 | | private static bool IsTokenTypeString(JsonTokenType tokenType) |
| | 0 | 691 | | { |
| | 0 | 692 | | return tokenType == JsonTokenType.PropertyName || tokenType == JsonTokenType.String; |
| | 0 | 693 | | } |
| | | 694 | | |
| | | 695 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 696 | | private readonly bool MatchNotPossible(int charTextLength) |
| | 0 | 697 | | { |
| | 0 | 698 | | if (HasValueSequence) |
| | 0 | 699 | | { |
| | 0 | 700 | | return MatchNotPossibleSequence(charTextLength); |
| | | 701 | | } |
| | | 702 | | |
| | 0 | 703 | | int sourceLength = ValueSpan.Length; |
| | | 704 | | |
| | | 705 | | // Transcoding from UTF-16 to UTF-8 will change the length by somwhere between 1x and 3x. |
| | | 706 | | // Unescaping the token value will at most shrink its length by 6x. |
| | | 707 | | // There is no point incurring the transcoding/unescaping/comparing cost if: |
| | | 708 | | // - The token value is smaller than charTextLength |
| | | 709 | | // - The token value needs to be transcoded AND unescaped and it is more than 6x larger than charTextLength |
| | | 710 | | // - For an ASCII UTF-16 characters, transcoding = 1x, escaping = 6x => 6x factor |
| | | 711 | | // - For non-ASCII UTF-16 characters within the BMP, transcoding = 2-3x, but they are represented as a |
| | | 712 | | // - For non-ASCII UTF-16 characters outside of the BMP, transcoding = 4x, but the surrogate pair (2 ch |
| | | 713 | | // - The token value needs to be transcoded, but NOT escaped and it is more than 3x larger than charTextLeng |
| | | 714 | | // - For an ASCII UTF-16 characters, transcoding = 1x, |
| | | 715 | | // - For non-ASCII UTF-16 characters within the BMP, transcoding = 2-3x, |
| | | 716 | | // - For non-ASCII UTF-16 characters outside of the BMP, transcoding = 2x, (surrogate pairs - 2 charact |
| | | 717 | | |
| | 0 | 718 | | if (sourceLength < charTextLength |
| | 0 | 719 | | || sourceLength / (ValueIsEscaped ? JsonConstants.MaxExpansionFactorWhileEscaping : JsonConstants.MaxExp |
| | 0 | 720 | | { |
| | 0 | 721 | | return true; |
| | | 722 | | } |
| | 0 | 723 | | return false; |
| | 0 | 724 | | } |
| | | 725 | | |
| | | 726 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 727 | | private readonly bool MatchNotPossibleSequence(int charTextLength) |
| | 0 | 728 | | { |
| | 0 | 729 | | long sourceLength = ValueSequence.Length; |
| | | 730 | | |
| | 0 | 731 | | if (sourceLength < charTextLength |
| | 0 | 732 | | || sourceLength / (ValueIsEscaped ? JsonConstants.MaxExpansionFactorWhileEscaping : JsonConstants.MaxExp |
| | 0 | 733 | | { |
| | 0 | 734 | | return true; |
| | | 735 | | } |
| | 0 | 736 | | return false; |
| | 0 | 737 | | } |
| | | 738 | | |
| | | 739 | | private void StartObject() |
| | 82 | 740 | | { |
| | 82 | 741 | | if (_bitStack.CurrentDepth >= _readerOptions.MaxDepth) |
| | 0 | 742 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ObjectDepthTooLarge); |
| | | 743 | | |
| | 82 | 744 | | _bitStack.PushTrue(); |
| | | 745 | | |
| | 82 | 746 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | 82 | 747 | | _consumed++; |
| | 82 | 748 | | _bytePositionInLine++; |
| | 82 | 749 | | _tokenType = JsonTokenType.StartObject; |
| | 82 | 750 | | _inObject = true; |
| | 82 | 751 | | } |
| | | 752 | | |
| | | 753 | | private void EndObject() |
| | 0 | 754 | | { |
| | 0 | 755 | | if (!_inObject || _bitStack.CurrentDepth <= 0) |
| | 0 | 756 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.MismatchedObjectArray, JsonConstants.Cl |
| | | 757 | | |
| | 0 | 758 | | if (_trailingCommaBeforeComment) |
| | 0 | 759 | | { |
| | 0 | 760 | | if (!_readerOptions.AllowTrailingCommas) |
| | 0 | 761 | | { |
| | 0 | 762 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBeforeObject |
| | | 763 | | } |
| | 0 | 764 | | _trailingCommaBeforeComment = false; |
| | 0 | 765 | | } |
| | | 766 | | |
| | 0 | 767 | | _tokenType = JsonTokenType.EndObject; |
| | 0 | 768 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | | 769 | | |
| | 0 | 770 | | UpdateBitStackOnEndToken(); |
| | 0 | 771 | | } |
| | | 772 | | |
| | | 773 | | private void StartArray() |
| | 1354 | 774 | | { |
| | 1354 | 775 | | if (_bitStack.CurrentDepth >= _readerOptions.MaxDepth) |
| | 0 | 776 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ArrayDepthTooLarge); |
| | | 777 | | |
| | 1354 | 778 | | _bitStack.PushFalse(); |
| | | 779 | | |
| | 1354 | 780 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | 1354 | 781 | | _consumed++; |
| | 1354 | 782 | | _bytePositionInLine++; |
| | 1354 | 783 | | _tokenType = JsonTokenType.StartArray; |
| | 1354 | 784 | | _inObject = false; |
| | 1354 | 785 | | } |
| | | 786 | | |
| | | 787 | | private void EndArray() |
| | 332 | 788 | | { |
| | 332 | 789 | | if (_inObject || _bitStack.CurrentDepth <= 0) |
| | 0 | 790 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.MismatchedObjectArray, JsonConstants.Cl |
| | | 791 | | |
| | 332 | 792 | | if (_trailingCommaBeforeComment) |
| | 0 | 793 | | { |
| | 0 | 794 | | if (!_readerOptions.AllowTrailingCommas) |
| | 0 | 795 | | { |
| | 0 | 796 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBeforeArrayE |
| | | 797 | | } |
| | 0 | 798 | | _trailingCommaBeforeComment = false; |
| | 0 | 799 | | } |
| | | 800 | | |
| | 332 | 801 | | _tokenType = JsonTokenType.EndArray; |
| | 332 | 802 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | | 803 | | |
| | 332 | 804 | | UpdateBitStackOnEndToken(); |
| | 332 | 805 | | } |
| | | 806 | | |
| | | 807 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 808 | | private void UpdateBitStackOnEndToken() |
| | 332 | 809 | | { |
| | 332 | 810 | | _consumed++; |
| | 332 | 811 | | _bytePositionInLine++; |
| | 332 | 812 | | _inObject = _bitStack.Pop(); |
| | 332 | 813 | | } |
| | | 814 | | |
| | | 815 | | private bool ReadSingleSegment() |
| | 35682 | 816 | | { |
| | 35682 | 817 | | bool retVal = false; |
| | 35682 | 818 | | ValueSpan = default; |
| | 35682 | 819 | | ValueIsEscaped = false; |
| | | 820 | | |
| | 35682 | 821 | | if (!HasMoreData()) |
| | 1495 | 822 | | { |
| | 1495 | 823 | | goto Done; |
| | | 824 | | } |
| | | 825 | | |
| | 34187 | 826 | | byte first = _buffer[_consumed]; |
| | | 827 | | |
| | | 828 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | | 829 | | // SkipWhiteSpace only skips the whitespace characters as defined by JSON RFC 8259 section 2. |
| | | 830 | | // We do not validate if 'first' is an invalid JSON byte here (such as control characters). |
| | | 831 | | // Those cases are captured in ConsumeNextToken and ConsumeValue. |
| | 34187 | 832 | | if (first <= JsonConstants.Space) |
| | 3871 | 833 | | { |
| | 3871 | 834 | | SkipWhiteSpace(); |
| | 3871 | 835 | | if (!HasMoreData()) |
| | 0 | 836 | | { |
| | 0 | 837 | | goto Done; |
| | | 838 | | } |
| | 3871 | 839 | | first = _buffer[_consumed]; |
| | 3871 | 840 | | } |
| | | 841 | | |
| | 34187 | 842 | | TokenStartIndex = _consumed; |
| | | 843 | | |
| | 34187 | 844 | | if (_tokenType == JsonTokenType.None) |
| | 30072 | 845 | | { |
| | 30072 | 846 | | goto ReadFirstToken; |
| | | 847 | | } |
| | | 848 | | |
| | 4115 | 849 | | if (first == JsonConstants.Slash) |
| | 785 | 850 | | { |
| | 785 | 851 | | retVal = ConsumeNextTokenOrRollback(first); |
| | 0 | 852 | | goto Done; |
| | | 853 | | } |
| | | 854 | | |
| | 3330 | 855 | | if (_tokenType == JsonTokenType.StartObject) |
| | 212 | 856 | | { |
| | 212 | 857 | | if (first == JsonConstants.CloseBrace) |
| | 0 | 858 | | { |
| | 0 | 859 | | EndObject(); |
| | 0 | 860 | | } |
| | | 861 | | else |
| | 212 | 862 | | { |
| | 212 | 863 | | if (first != JsonConstants.Quote) |
| | 204 | 864 | | { |
| | 204 | 865 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 866 | | } |
| | | 867 | | |
| | 8 | 868 | | int prevConsumed = _consumed; |
| | 8 | 869 | | long prevPosition = _bytePositionInLine; |
| | 8 | 870 | | long prevLineNumber = _lineNumber; |
| | 8 | 871 | | retVal = ConsumePropertyName(); |
| | 0 | 872 | | if (!retVal) |
| | 0 | 873 | | { |
| | | 874 | | // roll back potential changes |
| | 0 | 875 | | _consumed = prevConsumed; |
| | 0 | 876 | | _tokenType = JsonTokenType.StartObject; |
| | 0 | 877 | | _bytePositionInLine = prevPosition; |
| | 0 | 878 | | _lineNumber = prevLineNumber; |
| | 0 | 879 | | } |
| | 0 | 880 | | goto Done; |
| | | 881 | | } |
| | 0 | 882 | | } |
| | 3118 | 883 | | else if (_tokenType == JsonTokenType.StartArray) |
| | 1495 | 884 | | { |
| | 1495 | 885 | | if (first == JsonConstants.CloseBracket) |
| | 235 | 886 | | { |
| | 235 | 887 | | EndArray(); |
| | 235 | 888 | | } |
| | | 889 | | else |
| | 1260 | 890 | | { |
| | 1260 | 891 | | retVal = ConsumeValue(first); |
| | 717 | 892 | | goto Done; |
| | | 893 | | } |
| | 235 | 894 | | } |
| | 1623 | 895 | | else if (_tokenType == JsonTokenType.PropertyName) |
| | 0 | 896 | | { |
| | 0 | 897 | | retVal = ConsumeValue(first); |
| | 0 | 898 | | goto Done; |
| | | 899 | | } |
| | | 900 | | else |
| | 1623 | 901 | | { |
| | 1623 | 902 | | retVal = ConsumeNextTokenOrRollback(first); |
| | 0 | 903 | | goto Done; |
| | | 904 | | } |
| | | 905 | | |
| | 235 | 906 | | retVal = true; |
| | | 907 | | |
| | 17557 | 908 | | Done: |
| | 17557 | 909 | | return retVal; |
| | | 910 | | |
| | 30072 | 911 | | ReadFirstToken: |
| | 30072 | 912 | | retVal = ReadFirstToken(first); |
| | 15110 | 913 | | goto Done; |
| | 17557 | 914 | | } |
| | | 915 | | |
| | | 916 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 917 | | private bool HasMoreData() |
| | 39594 | 918 | | { |
| | 39594 | 919 | | if (_consumed >= (uint)_buffer.Length) |
| | 1495 | 920 | | { |
| | 1495 | 921 | | if (_isNotPrimitive && IsLastSpan) |
| | 138 | 922 | | { |
| | 138 | 923 | | if (_bitStack.CurrentDepth != 0) |
| | 0 | 924 | | { |
| | 0 | 925 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ZeroDepthAtEnd); |
| | | 926 | | } |
| | | 927 | | |
| | 138 | 928 | | if (_readerOptions.CommentHandling == JsonCommentHandling.Allow && _tokenType == JsonTokenType.Comme |
| | 0 | 929 | | { |
| | 0 | 930 | | return false; |
| | | 931 | | } |
| | | 932 | | |
| | 138 | 933 | | if (_tokenType is not JsonTokenType.EndArray and not JsonTokenType.EndObject) |
| | 0 | 934 | | { |
| | 0 | 935 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidEndOfJsonNonPrimitive); |
| | | 936 | | } |
| | 138 | 937 | | } |
| | 1495 | 938 | | return false; |
| | | 939 | | } |
| | 38099 | 940 | | return true; |
| | 39594 | 941 | | } |
| | | 942 | | |
| | | 943 | | // Unlike the parameter-less overload of HasMoreData, if there is no more data when this method is called, we kn |
| | | 944 | | // This is because, this method is only called after a ',' (i.e. we expect a value/property name) or after |
| | | 945 | | // a property name, which means it must be followed by a value. |
| | | 946 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 947 | | private bool HasMoreData(ExceptionResource resource) |
| | 0 | 948 | | { |
| | 0 | 949 | | if (_consumed >= (uint)_buffer.Length) |
| | 0 | 950 | | { |
| | 0 | 951 | | if (IsLastSpan) |
| | 0 | 952 | | { |
| | 0 | 953 | | ThrowHelper.ThrowJsonReaderException(ref this, resource); |
| | | 954 | | } |
| | 0 | 955 | | return false; |
| | | 956 | | } |
| | 0 | 957 | | return true; |
| | 0 | 958 | | } |
| | | 959 | | |
| | | 960 | | private bool ReadFirstToken(byte first) |
| | 30072 | 961 | | { |
| | 30072 | 962 | | if (first == JsonConstants.OpenBrace) |
| | 966 | 963 | | { |
| | 966 | 964 | | _bitStack.SetFirstBit(); |
| | 966 | 965 | | _tokenType = JsonTokenType.StartObject; |
| | 966 | 966 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | 966 | 967 | | _consumed++; |
| | 966 | 968 | | _bytePositionInLine++; |
| | 966 | 969 | | _inObject = true; |
| | 966 | 970 | | _isNotPrimitive = true; |
| | 966 | 971 | | } |
| | 29106 | 972 | | else if (first == JsonConstants.OpenBracket) |
| | 3918 | 973 | | { |
| | 3918 | 974 | | _bitStack.ResetFirstBit(); |
| | 3918 | 975 | | _tokenType = JsonTokenType.StartArray; |
| | 3918 | 976 | | ValueSpan = _buffer.Slice(_consumed, 1); |
| | 3918 | 977 | | _consumed++; |
| | 3918 | 978 | | _bytePositionInLine++; |
| | 3918 | 979 | | _isNotPrimitive = true; |
| | 3918 | 980 | | } |
| | | 981 | | else |
| | 25188 | 982 | | { |
| | | 983 | | // Create local copy to avoid bounds checks. |
| | 25188 | 984 | | ReadOnlySpan<byte> localBuffer = _buffer; |
| | | 985 | | |
| | 25188 | 986 | | if (JsonHelpers.IsDigit(first) || first == '-') |
| | 4308 | 987 | | { |
| | 4308 | 988 | | if (!TryGetNumber(localBuffer.Slice(_consumed), out int numberOfBytes)) |
| | 0 | 989 | | { |
| | 0 | 990 | | return false; |
| | | 991 | | } |
| | 3132 | 992 | | _tokenType = JsonTokenType.Number; |
| | 3132 | 993 | | _consumed += numberOfBytes; |
| | 3132 | 994 | | _bytePositionInLine += numberOfBytes; |
| | 3132 | 995 | | } |
| | 20880 | 996 | | else if (!ConsumeValue(first)) |
| | 369 | 997 | | { |
| | 369 | 998 | | return false; |
| | | 999 | | } |
| | | 1000 | | |
| | 9857 | 1001 | | _isNotPrimitive = _tokenType is JsonTokenType.StartObject or JsonTokenType.StartArray; |
| | | 1002 | | // Intentionally fall out of the if-block to return true |
| | 9857 | 1003 | | } |
| | 14741 | 1004 | | return true; |
| | 15110 | 1005 | | } |
| | | 1006 | | |
| | | 1007 | | private void SkipWhiteSpace() |
| | 8451 | 1008 | | { |
| | | 1009 | | // Create local copy to avoid bounds checks. |
| | 8451 | 1010 | | ReadOnlySpan<byte> localBuffer = _buffer; |
| | 20443 | 1011 | | for (; _consumed < localBuffer.Length; _consumed++) |
| | 13820 | 1012 | | { |
| | 13820 | 1013 | | byte val = localBuffer[_consumed]; |
| | | 1014 | | |
| | | 1015 | | // JSON RFC 8259 section 2 says only these 4 characters count, not all of the Unicode definitions of whi |
| | 13820 | 1016 | | if (val is not JsonConstants.Space and |
| | 13820 | 1017 | | not JsonConstants.CarriageReturn and |
| | 13820 | 1018 | | not JsonConstants.LineFeed and |
| | 13820 | 1019 | | not JsonConstants.Tab) |
| | 7824 | 1020 | | { |
| | 7824 | 1021 | | break; |
| | | 1022 | | } |
| | | 1023 | | |
| | 5996 | 1024 | | if (val == JsonConstants.LineFeed) |
| | 4504 | 1025 | | { |
| | 4504 | 1026 | | _lineNumber++; |
| | 4504 | 1027 | | _bytePositionInLine = 0; |
| | 4504 | 1028 | | } |
| | | 1029 | | else |
| | 1492 | 1030 | | { |
| | 1492 | 1031 | | _bytePositionInLine++; |
| | 1492 | 1032 | | } |
| | 5996 | 1033 | | } |
| | 8451 | 1034 | | } |
| | | 1035 | | |
| | | 1036 | | /// <summary> |
| | | 1037 | | /// This method contains the logic for processing the next value token and determining |
| | | 1038 | | /// what type of data it is. |
| | | 1039 | | /// </summary> |
| | | 1040 | | private bool ConsumeValue(byte marker) |
| | 22180 | 1041 | | { |
| | 22303 | 1042 | | while (true) |
| | 22303 | 1043 | | { |
| | 22303 | 1044 | | Debug.Assert((_trailingCommaBeforeComment && _readerOptions.CommentHandling == JsonCommentHandling.Allow |
| | 22303 | 1045 | | Debug.Assert((_trailingCommaBeforeComment && marker != JsonConstants.Slash) || !_trailingCommaBeforeComm |
| | 22303 | 1046 | | _trailingCommaBeforeComment = false; |
| | | 1047 | | |
| | 22303 | 1048 | | if (marker == JsonConstants.Quote) |
| | 7078 | 1049 | | { |
| | 7078 | 1050 | | return ConsumeString(); |
| | | 1051 | | } |
| | 15225 | 1052 | | else if (marker == JsonConstants.OpenBrace) |
| | 41 | 1053 | | { |
| | 41 | 1054 | | StartObject(); |
| | 41 | 1055 | | } |
| | 15184 | 1056 | | else if (marker == JsonConstants.OpenBracket) |
| | 677 | 1057 | | { |
| | 677 | 1058 | | StartArray(); |
| | 677 | 1059 | | } |
| | 14507 | 1060 | | else if (JsonHelpers.IsDigit(marker) || marker == '-') |
| | 166 | 1061 | | { |
| | 166 | 1062 | | return ConsumeNumber(); |
| | | 1063 | | } |
| | 14341 | 1064 | | else if (marker == 'f') |
| | 210 | 1065 | | { |
| | 210 | 1066 | | return ConsumeLiteral(JsonConstants.FalseValue, JsonTokenType.False); |
| | | 1067 | | } |
| | 14131 | 1068 | | else if (marker == 't') |
| | 126 | 1069 | | { |
| | 126 | 1070 | | return ConsumeLiteral(JsonConstants.TrueValue, JsonTokenType.True); |
| | | 1071 | | } |
| | 14005 | 1072 | | else if (marker == 'n') |
| | 168 | 1073 | | { |
| | 168 | 1074 | | return ConsumeLiteral(JsonConstants.NullValue, JsonTokenType.Null); |
| | | 1075 | | } |
| | | 1076 | | else |
| | 13837 | 1077 | | { |
| | 13837 | 1078 | | switch (_readerOptions.CommentHandling) |
| | | 1079 | | { |
| | | 1080 | | case JsonCommentHandling.Disallow: |
| | 11429 | 1081 | | break; |
| | | 1082 | | case JsonCommentHandling.Allow: |
| | 0 | 1083 | | if (marker == JsonConstants.Slash) |
| | 0 | 1084 | | { |
| | 0 | 1085 | | return ConsumeComment(); |
| | | 1086 | | } |
| | 0 | 1087 | | break; |
| | | 1088 | | default: |
| | 2408 | 1089 | | Debug.Assert(_readerOptions.CommentHandling == JsonCommentHandling.Skip); |
| | 2408 | 1090 | | if (marker == JsonConstants.Slash) |
| | 574 | 1091 | | { |
| | 574 | 1092 | | if (SkipComment()) |
| | 492 | 1093 | | { |
| | 492 | 1094 | | if (_consumed >= (uint)_buffer.Length) |
| | 369 | 1095 | | { |
| | 369 | 1096 | | if (_isNotPrimitive && IsLastSpan && _tokenType != JsonTokenType.EndArray && _to |
| | 0 | 1097 | | { |
| | 0 | 1098 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidEndO |
| | | 1099 | | } |
| | 369 | 1100 | | return false; |
| | | 1101 | | } |
| | | 1102 | | |
| | 123 | 1103 | | marker = _buffer[_consumed]; |
| | | 1104 | | |
| | | 1105 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not ne |
| | 123 | 1106 | | if (marker <= JsonConstants.Space) |
| | 41 | 1107 | | { |
| | 41 | 1108 | | SkipWhiteSpace(); |
| | 41 | 1109 | | if (!HasMoreData()) |
| | 0 | 1110 | | { |
| | 0 | 1111 | | return false; |
| | | 1112 | | } |
| | 41 | 1113 | | marker = _buffer[_consumed]; |
| | 41 | 1114 | | } |
| | | 1115 | | |
| | 123 | 1116 | | TokenStartIndex = _consumed; |
| | | 1117 | | |
| | | 1118 | | // Skip comments and consume the actual JSON value. |
| | 123 | 1119 | | continue; |
| | | 1120 | | } |
| | 0 | 1121 | | return false; |
| | | 1122 | | } |
| | 1834 | 1123 | | break; |
| | | 1124 | | } |
| | 13263 | 1125 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, marke |
| | | 1126 | | } |
| | 718 | 1127 | | break; |
| | | 1128 | | } |
| | 718 | 1129 | | return true; |
| | 7811 | 1130 | | } |
| | | 1131 | | |
| | | 1132 | | // Consumes 'null', or 'true', or 'false' |
| | | 1133 | | private bool ConsumeLiteral(ReadOnlySpan<byte> literal, JsonTokenType tokenType) |
| | 504 | 1134 | | { |
| | 504 | 1135 | | ReadOnlySpan<byte> span = _buffer.Slice(_consumed); |
| | 504 | 1136 | | Debug.Assert(span.Length > 0); |
| | 504 | 1137 | | Debug.Assert(span[0] == 'n' || span[0] == 't' || span[0] == 'f'); |
| | | 1138 | | |
| | 504 | 1139 | | if (!span.StartsWith(literal)) |
| | 504 | 1140 | | { |
| | 504 | 1141 | | return CheckLiteral(span, literal); |
| | | 1142 | | } |
| | | 1143 | | |
| | 0 | 1144 | | ValueSpan = span.Slice(0, literal.Length); |
| | 0 | 1145 | | _tokenType = tokenType; |
| | 0 | 1146 | | _consumed += literal.Length; |
| | 0 | 1147 | | _bytePositionInLine += literal.Length; |
| | 0 | 1148 | | return true; |
| | 0 | 1149 | | } |
| | | 1150 | | |
| | | 1151 | | private bool CheckLiteral(ReadOnlySpan<byte> span, ReadOnlySpan<byte> literal) |
| | 504 | 1152 | | { |
| | 504 | 1153 | | Debug.Assert(span.Length > 0 && span[0] == literal[0]); |
| | | 1154 | | |
| | 504 | 1155 | | int indexOfFirstMismatch = 0; |
| | | 1156 | | |
| | 1008 | 1157 | | for (int i = 1; i < literal.Length; i++) |
| | 504 | 1158 | | { |
| | 504 | 1159 | | if (span.Length > i) |
| | 504 | 1160 | | { |
| | 504 | 1161 | | if (span[i] != literal[i]) |
| | 504 | 1162 | | { |
| | 504 | 1163 | | _bytePositionInLine += i; |
| | 504 | 1164 | | ThrowInvalidLiteral(span); |
| | 0 | 1165 | | } |
| | 0 | 1166 | | } |
| | | 1167 | | else |
| | 0 | 1168 | | { |
| | 0 | 1169 | | indexOfFirstMismatch = i; |
| | 0 | 1170 | | break; |
| | | 1171 | | } |
| | 0 | 1172 | | } |
| | | 1173 | | |
| | 0 | 1174 | | Debug.Assert(indexOfFirstMismatch > 0 && indexOfFirstMismatch < literal.Length); |
| | | 1175 | | |
| | 0 | 1176 | | if (IsLastSpan) |
| | 0 | 1177 | | { |
| | 0 | 1178 | | _bytePositionInLine += indexOfFirstMismatch; |
| | 0 | 1179 | | ThrowInvalidLiteral(span); |
| | 0 | 1180 | | } |
| | 0 | 1181 | | return false; |
| | 0 | 1182 | | } |
| | | 1183 | | |
| | | 1184 | | private void ThrowInvalidLiteral(ReadOnlySpan<byte> span) |
| | 504 | 1185 | | { |
| | 504 | 1186 | | byte firstByte = span[0]; |
| | | 1187 | | |
| | | 1188 | | ExceptionResource resource; |
| | 504 | 1189 | | switch (firstByte) |
| | | 1190 | | { |
| | | 1191 | | case (byte)'t': |
| | 126 | 1192 | | resource = ExceptionResource.ExpectedTrue; |
| | 126 | 1193 | | break; |
| | | 1194 | | case (byte)'f': |
| | 210 | 1195 | | resource = ExceptionResource.ExpectedFalse; |
| | 210 | 1196 | | break; |
| | | 1197 | | default: |
| | 168 | 1198 | | Debug.Assert(firstByte == 'n'); |
| | 168 | 1199 | | resource = ExceptionResource.ExpectedNull; |
| | 168 | 1200 | | break; |
| | | 1201 | | } |
| | 504 | 1202 | | ThrowHelper.ThrowJsonReaderException(ref this, resource, bytes: span); |
| | | 1203 | | } |
| | | 1204 | | |
| | | 1205 | | private bool ConsumeNumber() |
| | 166 | 1206 | | { |
| | 166 | 1207 | | if (!TryGetNumber(_buffer.Slice(_consumed), out int consumed)) |
| | 0 | 1208 | | { |
| | 0 | 1209 | | return false; |
| | | 1210 | | } |
| | | 1211 | | |
| | 40 | 1212 | | _tokenType = JsonTokenType.Number; |
| | 40 | 1213 | | _consumed += consumed; |
| | 40 | 1214 | | _bytePositionInLine += consumed; |
| | | 1215 | | |
| | 40 | 1216 | | if (_consumed >= (uint)_buffer.Length) |
| | 0 | 1217 | | { |
| | 0 | 1218 | | Debug.Assert(IsLastSpan); |
| | | 1219 | | |
| | | 1220 | | // If there is no more data, and the JSON is not a single value, throw. |
| | 0 | 1221 | | if (_isNotPrimitive) |
| | 0 | 1222 | | { |
| | 0 | 1223 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, _buffer |
| | | 1224 | | } |
| | 0 | 1225 | | } |
| | | 1226 | | |
| | | 1227 | | // If there is more data and the JSON is not a single value, assert that there is an end of number delimiter |
| | | 1228 | | // Else, if either the JSON is a single value XOR if there is no more data, don't assert anything since ther |
| | 40 | 1229 | | Debug.Assert( |
| | 40 | 1230 | | ((_consumed < _buffer.Length) && |
| | 40 | 1231 | | !_isNotPrimitive && |
| | 40 | 1232 | | JsonConstants.Delimiters.Contains(_buffer[_consumed])) |
| | 40 | 1233 | | || (_isNotPrimitive ^ (_consumed >= (uint)_buffer.Length))); |
| | | 1234 | | |
| | 40 | 1235 | | return true; |
| | 40 | 1236 | | } |
| | | 1237 | | |
| | | 1238 | | private bool ConsumePropertyName() |
| | 8 | 1239 | | { |
| | 8 | 1240 | | _trailingCommaBeforeComment = false; |
| | | 1241 | | |
| | 8 | 1242 | | if (!ConsumeString()) |
| | 0 | 1243 | | { |
| | 0 | 1244 | | return false; |
| | | 1245 | | } |
| | | 1246 | | |
| | 0 | 1247 | | if (!HasMoreData(ExceptionResource.ExpectedValueAfterPropertyNameNotFound)) |
| | 0 | 1248 | | { |
| | 0 | 1249 | | return false; |
| | | 1250 | | } |
| | | 1251 | | |
| | 0 | 1252 | | byte first = _buffer[_consumed]; |
| | | 1253 | | |
| | | 1254 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | | 1255 | | // We do not validate if 'first' is an invalid JSON byte here (such as control characters). |
| | | 1256 | | // Those cases are captured below where we only accept ':'. |
| | 0 | 1257 | | if (first <= JsonConstants.Space) |
| | 0 | 1258 | | { |
| | 0 | 1259 | | SkipWhiteSpace(); |
| | 0 | 1260 | | if (!HasMoreData(ExceptionResource.ExpectedValueAfterPropertyNameNotFound)) |
| | 0 | 1261 | | { |
| | 0 | 1262 | | return false; |
| | | 1263 | | } |
| | 0 | 1264 | | first = _buffer[_consumed]; |
| | 0 | 1265 | | } |
| | | 1266 | | |
| | | 1267 | | // The next character must be a key / value separator. Validate and skip. |
| | 0 | 1268 | | if (first != JsonConstants.KeyValueSeparator) |
| | 0 | 1269 | | { |
| | 0 | 1270 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedSeparatorAfterPropertyNameNotFo |
| | | 1271 | | } |
| | | 1272 | | |
| | 0 | 1273 | | _consumed++; |
| | 0 | 1274 | | _bytePositionInLine++; |
| | 0 | 1275 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 1276 | | return true; |
| | 0 | 1277 | | } |
| | | 1278 | | |
| | | 1279 | | private bool ConsumeString() |
| | 7086 | 1280 | | { |
| | 7086 | 1281 | | Debug.Assert(_buffer.Length >= _consumed + 1); |
| | 7086 | 1282 | | Debug.Assert(_buffer[_consumed] == JsonConstants.Quote); |
| | | 1283 | | |
| | | 1284 | | // Create local copy to avoid bounds checks. |
| | 7086 | 1285 | | ReadOnlySpan<byte> localBuffer = _buffer.Slice(_consumed + 1); |
| | | 1286 | | |
| | | 1287 | | // Vectorized search for either quote, backslash, or any control character. |
| | | 1288 | | // If the first found byte is a quote, we have reached an end of string, and |
| | | 1289 | | // can avoid validation. |
| | | 1290 | | // Otherwise, in the uncommon case, iterate one character at a time and validate. |
| | 7086 | 1291 | | int idx = localBuffer.IndexOfQuoteOrAnyControlOrBackSlash(); |
| | | 1292 | | |
| | 7086 | 1293 | | if (idx >= 0) |
| | 6918 | 1294 | | { |
| | 6918 | 1295 | | byte foundByte = localBuffer[idx]; |
| | 6918 | 1296 | | if (foundByte == JsonConstants.Quote) |
| | 6684 | 1297 | | { |
| | 6684 | 1298 | | _bytePositionInLine += idx + 2; // Add 2 for the start and end quotes. |
| | 6684 | 1299 | | ValueSpan = localBuffer.Slice(0, idx); |
| | 6684 | 1300 | | ValueIsEscaped = false; |
| | 6684 | 1301 | | _tokenType = JsonTokenType.String; |
| | 6684 | 1302 | | _consumed += idx + 2; |
| | 6684 | 1303 | | return true; |
| | | 1304 | | } |
| | | 1305 | | else |
| | 234 | 1306 | | { |
| | 234 | 1307 | | return ConsumeStringAndValidate(localBuffer, idx); |
| | | 1308 | | } |
| | | 1309 | | } |
| | | 1310 | | else |
| | 168 | 1311 | | { |
| | 168 | 1312 | | if (IsLastSpan) |
| | 168 | 1313 | | { |
| | 168 | 1314 | | _bytePositionInLine += localBuffer.Length + 1; // Account for the start quote |
| | 168 | 1315 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.EndOfStringNotFound); |
| | | 1316 | | } |
| | 0 | 1317 | | return false; |
| | | 1318 | | } |
| | 6684 | 1319 | | } |
| | | 1320 | | |
| | | 1321 | | // Found a backslash or control characters which are considered invalid within a string. |
| | | 1322 | | // Search through the rest of the string one byte at a time. |
| | | 1323 | | // https://tools.ietf.org/html/rfc8259#section-7 |
| | | 1324 | | private bool ConsumeStringAndValidate(ReadOnlySpan<byte> data, int idx) |
| | 234 | 1325 | | { |
| | 234 | 1326 | | Debug.Assert(idx >= 0 && idx < data.Length); |
| | 234 | 1327 | | Debug.Assert(data[idx] != JsonConstants.Quote); |
| | 234 | 1328 | | Debug.Assert(data[idx] == JsonConstants.BackSlash || data[idx] < JsonConstants.Space); |
| | | 1329 | | |
| | 234 | 1330 | | long prevLineBytePosition = _bytePositionInLine; |
| | 234 | 1331 | | long prevLineNumber = _lineNumber; |
| | | 1332 | | |
| | 234 | 1333 | | _bytePositionInLine += idx + 1; // Add 1 for the first quote |
| | | 1334 | | |
| | 234 | 1335 | | bool nextCharEscaped = false; |
| | 234 | 1336 | | for (; idx < data.Length; idx++) |
| | 234 | 1337 | | { |
| | 234 | 1338 | | byte currentByte = data[idx]; |
| | 234 | 1339 | | if (currentByte == JsonConstants.Quote) |
| | 0 | 1340 | | { |
| | 0 | 1341 | | if (!nextCharEscaped) |
| | 0 | 1342 | | { |
| | 0 | 1343 | | goto Done; |
| | | 1344 | | } |
| | 0 | 1345 | | nextCharEscaped = false; |
| | 0 | 1346 | | } |
| | 234 | 1347 | | else if (currentByte == JsonConstants.BackSlash) |
| | 0 | 1348 | | { |
| | 0 | 1349 | | nextCharEscaped = !nextCharEscaped; |
| | 0 | 1350 | | } |
| | 234 | 1351 | | else if (nextCharEscaped) |
| | 0 | 1352 | | { |
| | 0 | 1353 | | int index = JsonConstants.EscapableChars.IndexOf(currentByte); |
| | 0 | 1354 | | if (index == -1) |
| | 0 | 1355 | | { |
| | 0 | 1356 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterAfterEscapeWith |
| | | 1357 | | } |
| | | 1358 | | |
| | 0 | 1359 | | if (currentByte == 'u') |
| | 0 | 1360 | | { |
| | | 1361 | | // Expecting 4 hex digits to follow the escaped 'u' |
| | 0 | 1362 | | _bytePositionInLine++; // move past the 'u' |
| | 0 | 1363 | | if (ValidateHexDigits(data, idx + 1)) |
| | 0 | 1364 | | { |
| | 0 | 1365 | | idx += 4; // Skip the 4 hex digits, the for loop accounts for idx incrementing past the 'u |
| | 0 | 1366 | | } |
| | | 1367 | | else |
| | 0 | 1368 | | { |
| | | 1369 | | // We found less than 4 hex digits. Check if there is more data to follow, otherwise throw. |
| | 0 | 1370 | | idx = data.Length; |
| | 0 | 1371 | | break; |
| | | 1372 | | } |
| | | 1373 | | |
| | 0 | 1374 | | } |
| | 0 | 1375 | | nextCharEscaped = false; |
| | 0 | 1376 | | } |
| | 234 | 1377 | | else if (currentByte < JsonConstants.Space) |
| | 234 | 1378 | | { |
| | 234 | 1379 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterWithinString, curre |
| | | 1380 | | } |
| | | 1381 | | |
| | 0 | 1382 | | _bytePositionInLine++; |
| | 0 | 1383 | | } |
| | | 1384 | | |
| | 0 | 1385 | | if (idx >= data.Length) |
| | 0 | 1386 | | { |
| | 0 | 1387 | | if (IsLastSpan) |
| | 0 | 1388 | | { |
| | 0 | 1389 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.EndOfStringNotFound); |
| | | 1390 | | } |
| | 0 | 1391 | | _lineNumber = prevLineNumber; |
| | 0 | 1392 | | _bytePositionInLine = prevLineBytePosition; |
| | 0 | 1393 | | return false; |
| | | 1394 | | } |
| | | 1395 | | |
| | 0 | 1396 | | Done: |
| | 0 | 1397 | | _bytePositionInLine++; // Add 1 for the end quote |
| | 0 | 1398 | | ValueSpan = data.Slice(0, idx); |
| | 0 | 1399 | | ValueIsEscaped = true; |
| | 0 | 1400 | | _tokenType = JsonTokenType.String; |
| | 0 | 1401 | | _consumed += idx + 2; |
| | 0 | 1402 | | return true; |
| | 0 | 1403 | | } |
| | | 1404 | | |
| | | 1405 | | private bool ValidateHexDigits(ReadOnlySpan<byte> data, int idx) |
| | 0 | 1406 | | { |
| | 0 | 1407 | | for (int j = idx; j < data.Length; j++) |
| | 0 | 1408 | | { |
| | 0 | 1409 | | byte nextByte = data[j]; |
| | 0 | 1410 | | if (!JsonReaderHelper.IsHexDigit(nextByte)) |
| | 0 | 1411 | | { |
| | 0 | 1412 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidHexCharacterWithinString, ne |
| | | 1413 | | } |
| | 0 | 1414 | | if (j - idx >= 3) |
| | 0 | 1415 | | { |
| | 0 | 1416 | | return true; |
| | | 1417 | | } |
| | 0 | 1418 | | _bytePositionInLine++; |
| | 0 | 1419 | | } |
| | | 1420 | | |
| | 0 | 1421 | | return false; |
| | 0 | 1422 | | } |
| | | 1423 | | |
| | | 1424 | | // https://tools.ietf.org/html/rfc7159#section-6 |
| | | 1425 | | private bool TryGetNumber(ReadOnlySpan<byte> data, out int consumed) |
| | 4474 | 1426 | | { |
| | | 1427 | | // TODO: https://github.com/dotnet/runtime/issues/27837 |
| | 4474 | 1428 | | Debug.Assert(data.Length > 0); |
| | | 1429 | | |
| | 4474 | 1430 | | consumed = 0; |
| | 4474 | 1431 | | int i = 0; |
| | | 1432 | | |
| | 4474 | 1433 | | ConsumeNumberResult signResult = ConsumeNegativeSign(ref data, ref i); |
| | 4353 | 1434 | | if (signResult == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1435 | | { |
| | 0 | 1436 | | return false; |
| | | 1437 | | } |
| | | 1438 | | |
| | 4353 | 1439 | | Debug.Assert(signResult == ConsumeNumberResult.OperationIncomplete); |
| | | 1440 | | |
| | 4353 | 1441 | | byte nextByte = data[i]; |
| | 4353 | 1442 | | Debug.Assert(nextByte >= '0' && nextByte <= '9'); |
| | | 1443 | | |
| | 4353 | 1444 | | if (nextByte == '0') |
| | 242 | 1445 | | { |
| | 242 | 1446 | | ConsumeNumberResult result = ConsumeZero(ref data, ref i); |
| | 0 | 1447 | | if (result == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1448 | | { |
| | 0 | 1449 | | return false; |
| | | 1450 | | } |
| | 0 | 1451 | | if (result == ConsumeNumberResult.Success) |
| | 0 | 1452 | | { |
| | 0 | 1453 | | goto Done; |
| | | 1454 | | } |
| | | 1455 | | |
| | 0 | 1456 | | Debug.Assert(result == ConsumeNumberResult.OperationIncomplete); |
| | 0 | 1457 | | nextByte = data[i]; |
| | 0 | 1458 | | } |
| | | 1459 | | else |
| | 4111 | 1460 | | { |
| | 4111 | 1461 | | i++; |
| | 4111 | 1462 | | ConsumeNumberResult result = ConsumeIntegerDigits(ref data, ref i); |
| | 4111 | 1463 | | if (result == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1464 | | { |
| | 0 | 1465 | | return false; |
| | | 1466 | | } |
| | 4111 | 1467 | | if (result == ConsumeNumberResult.Success) |
| | 3028 | 1468 | | { |
| | 3028 | 1469 | | goto Done; |
| | | 1470 | | } |
| | | 1471 | | |
| | 1083 | 1472 | | Debug.Assert(result == ConsumeNumberResult.OperationIncomplete); |
| | 1083 | 1473 | | nextByte = data[i]; |
| | 1083 | 1474 | | if (nextByte != '.' && nextByte != 'E' && nextByte != 'e') |
| | 603 | 1475 | | { |
| | 603 | 1476 | | _bytePositionInLine += i; |
| | 603 | 1477 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, nextByt |
| | | 1478 | | } |
| | 480 | 1479 | | } |
| | | 1480 | | |
| | 480 | 1481 | | Debug.Assert(nextByte == '.' || nextByte == 'E' || nextByte == 'e'); |
| | | 1482 | | |
| | 480 | 1483 | | if (nextByte == '.') |
| | 264 | 1484 | | { |
| | 264 | 1485 | | i++; |
| | 264 | 1486 | | ConsumeNumberResult result = ConsumeDecimalDigits(ref data, ref i); |
| | 222 | 1487 | | if (result == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1488 | | { |
| | 0 | 1489 | | return false; |
| | | 1490 | | } |
| | 222 | 1491 | | if (result == ConsumeNumberResult.Success) |
| | 96 | 1492 | | { |
| | 96 | 1493 | | goto Done; |
| | | 1494 | | } |
| | | 1495 | | |
| | 126 | 1496 | | Debug.Assert(result == ConsumeNumberResult.OperationIncomplete); |
| | 126 | 1497 | | nextByte = data[i]; |
| | 126 | 1498 | | if (nextByte != 'E' && nextByte != 'e') |
| | 126 | 1499 | | { |
| | 126 | 1500 | | _bytePositionInLine += i; |
| | 126 | 1501 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedNextDigitEValueNotFound, ne |
| | | 1502 | | } |
| | 0 | 1503 | | } |
| | | 1504 | | |
| | 216 | 1505 | | Debug.Assert(nextByte == 'E' || nextByte == 'e'); |
| | 216 | 1506 | | i++; |
| | | 1507 | | |
| | 216 | 1508 | | signResult = ConsumeSign(ref data, ref i); |
| | 132 | 1509 | | if (signResult == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1510 | | { |
| | 0 | 1511 | | return false; |
| | | 1512 | | } |
| | | 1513 | | |
| | 132 | 1514 | | Debug.Assert(signResult == ConsumeNumberResult.OperationIncomplete); |
| | | 1515 | | |
| | 132 | 1516 | | i++; |
| | 132 | 1517 | | ConsumeNumberResult resultExponent = ConsumeIntegerDigits(ref data, ref i); |
| | 132 | 1518 | | if (resultExponent == ConsumeNumberResult.NeedMoreData) |
| | 0 | 1519 | | { |
| | 0 | 1520 | | return false; |
| | | 1521 | | } |
| | 132 | 1522 | | if (resultExponent == ConsumeNumberResult.Success) |
| | 48 | 1523 | | { |
| | 48 | 1524 | | goto Done; |
| | | 1525 | | } |
| | | 1526 | | |
| | 84 | 1527 | | Debug.Assert(resultExponent == ConsumeNumberResult.OperationIncomplete); |
| | | 1528 | | |
| | 84 | 1529 | | _bytePositionInLine += i; |
| | 84 | 1530 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, data[i]); |
| | | 1531 | | |
| | 3172 | 1532 | | Done: |
| | 3172 | 1533 | | ValueSpan = data.Slice(0, i); |
| | 3172 | 1534 | | consumed = i; |
| | 3172 | 1535 | | return true; |
| | 3172 | 1536 | | } |
| | | 1537 | | |
| | | 1538 | | private ConsumeNumberResult ConsumeNegativeSign(ref ReadOnlySpan<byte> data, scoped ref int i) |
| | 4474 | 1539 | | { |
| | 4474 | 1540 | | byte nextByte = data[i]; |
| | | 1541 | | |
| | 4474 | 1542 | | if (nextByte == '-') |
| | 209 | 1543 | | { |
| | 209 | 1544 | | i++; |
| | 209 | 1545 | | if (i >= data.Length) |
| | 0 | 1546 | | { |
| | 0 | 1547 | | if (IsLastSpan) |
| | 0 | 1548 | | { |
| | 0 | 1549 | | _bytePositionInLine += i; |
| | 0 | 1550 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundEndOfData) |
| | | 1551 | | } |
| | 0 | 1552 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1553 | | } |
| | | 1554 | | |
| | 209 | 1555 | | nextByte = data[i]; |
| | 209 | 1556 | | if (!JsonHelpers.IsDigit(nextByte)) |
| | 121 | 1557 | | { |
| | 121 | 1558 | | _bytePositionInLine += i; |
| | 121 | 1559 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundAfterSign, nex |
| | | 1560 | | } |
| | 88 | 1561 | | } |
| | 4353 | 1562 | | return ConsumeNumberResult.OperationIncomplete; |
| | 4353 | 1563 | | } |
| | | 1564 | | |
| | | 1565 | | private ConsumeNumberResult ConsumeZero(ref ReadOnlySpan<byte> data, scoped ref int i) |
| | 242 | 1566 | | { |
| | 242 | 1567 | | Debug.Assert(data[i] == (byte)'0'); |
| | 242 | 1568 | | i++; |
| | | 1569 | | byte nextByte; |
| | 242 | 1570 | | if (i < data.Length) |
| | 242 | 1571 | | { |
| | 242 | 1572 | | nextByte = data[i]; |
| | 242 | 1573 | | if (JsonConstants.Delimiters.Contains(nextByte)) |
| | 0 | 1574 | | { |
| | 0 | 1575 | | return ConsumeNumberResult.Success; |
| | | 1576 | | } |
| | 242 | 1577 | | } |
| | | 1578 | | else |
| | 0 | 1579 | | { |
| | 0 | 1580 | | if (IsLastSpan) |
| | 0 | 1581 | | { |
| | | 1582 | | // A payload containing a single value: "0" is valid |
| | | 1583 | | // If we are dealing with multi-value JSON, |
| | | 1584 | | // ConsumeNumber will validate that we have a delimiter following the "0". |
| | 0 | 1585 | | return ConsumeNumberResult.Success; |
| | | 1586 | | } |
| | | 1587 | | else |
| | 0 | 1588 | | { |
| | 0 | 1589 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1590 | | } |
| | | 1591 | | } |
| | 242 | 1592 | | nextByte = data[i]; |
| | 242 | 1593 | | if (nextByte != '.' && nextByte != 'E' && nextByte != 'e') |
| | 242 | 1594 | | { |
| | 242 | 1595 | | _bytePositionInLine += i; |
| | 242 | 1596 | | ThrowHelper.ThrowJsonReaderException(ref this, |
| | 242 | 1597 | | JsonHelpers.IsInRangeInclusive(nextByte, '0', '9') ? ExceptionResource.InvalidLeadingZeroInNumber : |
| | 242 | 1598 | | nextByte); |
| | | 1599 | | } |
| | | 1600 | | |
| | 0 | 1601 | | return ConsumeNumberResult.OperationIncomplete; |
| | 0 | 1602 | | } |
| | | 1603 | | |
| | | 1604 | | private ConsumeNumberResult ConsumeIntegerDigits(ref ReadOnlySpan<byte> data, scoped ref int i) |
| | 4465 | 1605 | | { |
| | 4465 | 1606 | | byte nextByte = default; |
| | 55763 | 1607 | | for (; i < data.Length; i++) |
| | 29376 | 1608 | | { |
| | 29376 | 1609 | | nextByte = data[i]; |
| | 29376 | 1610 | | if (!JsonHelpers.IsDigit(nextByte)) |
| | 3727 | 1611 | | { |
| | 3727 | 1612 | | break; |
| | | 1613 | | } |
| | 25649 | 1614 | | } |
| | 4465 | 1615 | | if (i >= data.Length) |
| | 738 | 1616 | | { |
| | 738 | 1617 | | if (IsLastSpan) |
| | 738 | 1618 | | { |
| | | 1619 | | // A payload containing a single value of integers (e.g. "12") is valid |
| | | 1620 | | // If we are dealing with multi-value JSON, |
| | | 1621 | | // ConsumeNumber will validate that we have a delimiter following the integer. |
| | 738 | 1622 | | return ConsumeNumberResult.Success; |
| | | 1623 | | } |
| | | 1624 | | else |
| | 0 | 1625 | | { |
| | 0 | 1626 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1627 | | } |
| | | 1628 | | } |
| | 3727 | 1629 | | if (JsonConstants.Delimiters.Contains(nextByte)) |
| | 2434 | 1630 | | { |
| | 2434 | 1631 | | return ConsumeNumberResult.Success; |
| | | 1632 | | } |
| | | 1633 | | |
| | 1293 | 1634 | | return ConsumeNumberResult.OperationIncomplete; |
| | 4465 | 1635 | | } |
| | | 1636 | | |
| | | 1637 | | private ConsumeNumberResult ConsumeDecimalDigits(ref ReadOnlySpan<byte> data, scoped ref int i) |
| | 264 | 1638 | | { |
| | 264 | 1639 | | if (i >= data.Length) |
| | 0 | 1640 | | { |
| | 0 | 1641 | | if (IsLastSpan) |
| | 0 | 1642 | | { |
| | 0 | 1643 | | _bytePositionInLine += i; |
| | 0 | 1644 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundEndOfData); |
| | | 1645 | | } |
| | 0 | 1646 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1647 | | } |
| | 264 | 1648 | | byte nextByte = data[i]; |
| | 264 | 1649 | | if (!JsonHelpers.IsDigit(nextByte)) |
| | 42 | 1650 | | { |
| | 42 | 1651 | | _bytePositionInLine += i; |
| | 42 | 1652 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundAfterDecimal, next |
| | | 1653 | | } |
| | 222 | 1654 | | i++; |
| | | 1655 | | |
| | 222 | 1656 | | return ConsumeIntegerDigits(ref data, ref i); |
| | 222 | 1657 | | } |
| | | 1658 | | |
| | | 1659 | | private ConsumeNumberResult ConsumeSign(ref ReadOnlySpan<byte> data, scoped ref int i) |
| | 216 | 1660 | | { |
| | 216 | 1661 | | if (i >= data.Length) |
| | 0 | 1662 | | { |
| | 0 | 1663 | | if (IsLastSpan) |
| | 0 | 1664 | | { |
| | 0 | 1665 | | _bytePositionInLine += i; |
| | 0 | 1666 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundEndOfData); |
| | | 1667 | | } |
| | 0 | 1668 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1669 | | } |
| | | 1670 | | |
| | 216 | 1671 | | byte nextByte = data[i]; |
| | 216 | 1672 | | if (nextByte == '+' || nextByte == '-') |
| | 0 | 1673 | | { |
| | 0 | 1674 | | i++; |
| | 0 | 1675 | | if (i >= data.Length) |
| | 0 | 1676 | | { |
| | 0 | 1677 | | if (IsLastSpan) |
| | 0 | 1678 | | { |
| | 0 | 1679 | | _bytePositionInLine += i; |
| | 0 | 1680 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundEndOfData) |
| | | 1681 | | } |
| | 0 | 1682 | | return ConsumeNumberResult.NeedMoreData; |
| | | 1683 | | } |
| | 0 | 1684 | | nextByte = data[i]; |
| | 0 | 1685 | | } |
| | | 1686 | | |
| | 216 | 1687 | | if (!JsonHelpers.IsDigit(nextByte)) |
| | 84 | 1688 | | { |
| | 84 | 1689 | | _bytePositionInLine += i; |
| | 84 | 1690 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.RequiredDigitNotFoundAfterSign, nextByt |
| | | 1691 | | } |
| | | 1692 | | |
| | 132 | 1693 | | return ConsumeNumberResult.OperationIncomplete; |
| | 132 | 1694 | | } |
| | | 1695 | | |
| | | 1696 | | private bool ConsumeNextTokenOrRollback(byte marker) |
| | 2408 | 1697 | | { |
| | 2408 | 1698 | | int prevConsumed = _consumed; |
| | 2408 | 1699 | | long prevPosition = _bytePositionInLine; |
| | 2408 | 1700 | | long prevLineNumber = _lineNumber; |
| | 2408 | 1701 | | JsonTokenType prevTokenType = _tokenType; |
| | 2408 | 1702 | | bool prevTrailingCommaBeforeComment = _trailingCommaBeforeComment; |
| | 2408 | 1703 | | ConsumeTokenResult result = ConsumeNextToken(marker); |
| | 0 | 1704 | | if (result == ConsumeTokenResult.Success) |
| | 0 | 1705 | | { |
| | 0 | 1706 | | return true; |
| | | 1707 | | } |
| | 0 | 1708 | | if (result == ConsumeTokenResult.NotEnoughDataRollBackState) |
| | 0 | 1709 | | { |
| | 0 | 1710 | | _consumed = prevConsumed; |
| | 0 | 1711 | | _tokenType = prevTokenType; |
| | 0 | 1712 | | _bytePositionInLine = prevPosition; |
| | 0 | 1713 | | _lineNumber = prevLineNumber; |
| | 0 | 1714 | | _trailingCommaBeforeComment = prevTrailingCommaBeforeComment; |
| | 0 | 1715 | | } |
| | 0 | 1716 | | return false; |
| | 0 | 1717 | | } |
| | | 1718 | | |
| | | 1719 | | /// <summary> |
| | | 1720 | | /// This method consumes the next token regardless of whether we are inside an object or an array. |
| | | 1721 | | /// For an object, it reads the next property name token. For an array, it just reads the next value. |
| | | 1722 | | /// </summary> |
| | | 1723 | | private ConsumeTokenResult ConsumeNextToken(byte marker) |
| | 2408 | 1724 | | { |
| | 2408 | 1725 | | if (_readerOptions.CommentHandling != JsonCommentHandling.Disallow) |
| | 1170 | 1726 | | { |
| | 1170 | 1727 | | if (_readerOptions.CommentHandling == JsonCommentHandling.Allow) |
| | 0 | 1728 | | { |
| | 0 | 1729 | | if (marker == JsonConstants.Slash) |
| | 0 | 1730 | | { |
| | 0 | 1731 | | return ConsumeComment() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackS |
| | | 1732 | | } |
| | 0 | 1733 | | if (_tokenType == JsonTokenType.Comment) |
| | 0 | 1734 | | { |
| | 0 | 1735 | | return ConsumeNextTokenFromLastNonCommentToken(); |
| | | 1736 | | } |
| | 0 | 1737 | | } |
| | | 1738 | | else |
| | 1170 | 1739 | | { |
| | 1170 | 1740 | | Debug.Assert(_readerOptions.CommentHandling == JsonCommentHandling.Skip); |
| | 1170 | 1741 | | return ConsumeNextTokenUntilAfterAllCommentsAreSkipped(marker); |
| | | 1742 | | } |
| | 0 | 1743 | | } |
| | | 1744 | | |
| | 1238 | 1745 | | if (_bitStack.CurrentDepth == 0) |
| | 1179 | 1746 | | { |
| | 1179 | 1747 | | if (_readerOptions.AllowMultipleValues) |
| | 0 | 1748 | | { |
| | 0 | 1749 | | return ReadFirstToken(marker) ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBac |
| | | 1750 | | } |
| | | 1751 | | |
| | 1179 | 1752 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndAfterSingleJson, marker); |
| | | 1753 | | } |
| | | 1754 | | |
| | 59 | 1755 | | if (marker == JsonConstants.ListSeparator) |
| | 40 | 1756 | | { |
| | 40 | 1757 | | _consumed++; |
| | 40 | 1758 | | _bytePositionInLine++; |
| | | 1759 | | |
| | 40 | 1760 | | if (_consumed >= (uint)_buffer.Length) |
| | 0 | 1761 | | { |
| | 0 | 1762 | | if (IsLastSpan) |
| | 0 | 1763 | | { |
| | 0 | 1764 | | _consumed--; |
| | 0 | 1765 | | _bytePositionInLine--; |
| | 0 | 1766 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyOrValueN |
| | | 1767 | | } |
| | 0 | 1768 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | | 1769 | | } |
| | 40 | 1770 | | byte first = _buffer[_consumed]; |
| | | 1771 | | |
| | | 1772 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 40 | 1773 | | if (first <= JsonConstants.Space) |
| | 0 | 1774 | | { |
| | 0 | 1775 | | SkipWhiteSpace(); |
| | | 1776 | | // The next character must be a start of a property name or value. |
| | 0 | 1777 | | if (!HasMoreData(ExceptionResource.ExpectedStartOfPropertyOrValueNotFound)) |
| | 0 | 1778 | | { |
| | 0 | 1779 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | | 1780 | | } |
| | 0 | 1781 | | first = _buffer[_consumed]; |
| | 0 | 1782 | | } |
| | | 1783 | | |
| | 40 | 1784 | | TokenStartIndex = _consumed; |
| | | 1785 | | |
| | 40 | 1786 | | if (_readerOptions.CommentHandling == JsonCommentHandling.Allow && first == JsonConstants.Slash) |
| | 0 | 1787 | | { |
| | 0 | 1788 | | _trailingCommaBeforeComment = true; |
| | 0 | 1789 | | return ConsumeComment() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackState |
| | | 1790 | | } |
| | | 1791 | | |
| | 40 | 1792 | | if (_inObject) |
| | 0 | 1793 | | { |
| | 0 | 1794 | | if (first != JsonConstants.Quote) |
| | 0 | 1795 | | { |
| | 0 | 1796 | | if (first == JsonConstants.CloseBrace) |
| | 0 | 1797 | | { |
| | 0 | 1798 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 1799 | | { |
| | 0 | 1800 | | EndObject(); |
| | 0 | 1801 | | return ConsumeTokenResult.Success; |
| | | 1802 | | } |
| | 0 | 1803 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBefo |
| | | 1804 | | } |
| | 0 | 1805 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 1806 | | } |
| | 0 | 1807 | | return ConsumePropertyName() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBack |
| | | 1808 | | } |
| | | 1809 | | else |
| | 40 | 1810 | | { |
| | 40 | 1811 | | if (first == JsonConstants.CloseBracket) |
| | 0 | 1812 | | { |
| | 0 | 1813 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 1814 | | { |
| | 0 | 1815 | | EndArray(); |
| | 0 | 1816 | | return ConsumeTokenResult.Success; |
| | | 1817 | | } |
| | 0 | 1818 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBeforeAr |
| | | 1819 | | } |
| | 40 | 1820 | | return ConsumeValue(first) ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackSt |
| | | 1821 | | } |
| | | 1822 | | } |
| | 19 | 1823 | | else if (marker == JsonConstants.CloseBrace) |
| | 0 | 1824 | | { |
| | 0 | 1825 | | EndObject(); |
| | 0 | 1826 | | } |
| | 19 | 1827 | | else if (marker == JsonConstants.CloseBracket) |
| | 0 | 1828 | | { |
| | 0 | 1829 | | EndArray(); |
| | 0 | 1830 | | } |
| | | 1831 | | else |
| | 19 | 1832 | | { |
| | 19 | 1833 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.FoundInvalidCharacter, marker); |
| | | 1834 | | } |
| | 0 | 1835 | | return ConsumeTokenResult.Success; |
| | 0 | 1836 | | } |
| | | 1837 | | |
| | | 1838 | | private ConsumeTokenResult ConsumeNextTokenFromLastNonCommentToken() |
| | 0 | 1839 | | { |
| | 0 | 1840 | | Debug.Assert(_readerOptions.CommentHandling == JsonCommentHandling.Allow); |
| | 0 | 1841 | | Debug.Assert(_tokenType == JsonTokenType.Comment); |
| | | 1842 | | |
| | 0 | 1843 | | if (JsonReaderHelper.IsTokenTypePrimitive(_previousTokenType)) |
| | 0 | 1844 | | { |
| | 0 | 1845 | | _tokenType = _inObject ? JsonTokenType.StartObject : JsonTokenType.StartArray; |
| | 0 | 1846 | | } |
| | | 1847 | | else |
| | 0 | 1848 | | { |
| | 0 | 1849 | | _tokenType = _previousTokenType; |
| | 0 | 1850 | | } |
| | | 1851 | | |
| | 0 | 1852 | | Debug.Assert(_tokenType != JsonTokenType.Comment); |
| | | 1853 | | |
| | 0 | 1854 | | if (!HasMoreData()) |
| | 0 | 1855 | | { |
| | 0 | 1856 | | goto RollBack; |
| | | 1857 | | } |
| | | 1858 | | |
| | 0 | 1859 | | byte first = _buffer[_consumed]; |
| | | 1860 | | |
| | | 1861 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 0 | 1862 | | if (first <= JsonConstants.Space) |
| | 0 | 1863 | | { |
| | 0 | 1864 | | SkipWhiteSpace(); |
| | 0 | 1865 | | if (!HasMoreData()) |
| | 0 | 1866 | | { |
| | 0 | 1867 | | goto RollBack; |
| | | 1868 | | } |
| | 0 | 1869 | | first = _buffer[_consumed]; |
| | 0 | 1870 | | } |
| | | 1871 | | |
| | 0 | 1872 | | if (_bitStack.CurrentDepth == 0 && _tokenType != JsonTokenType.None) |
| | 0 | 1873 | | { |
| | 0 | 1874 | | if (_readerOptions.AllowMultipleValues) |
| | 0 | 1875 | | { |
| | 0 | 1876 | | return ReadFirstToken(first) ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBack |
| | | 1877 | | } |
| | | 1878 | | |
| | 0 | 1879 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndAfterSingleJson, first); |
| | | 1880 | | } |
| | | 1881 | | |
| | 0 | 1882 | | Debug.Assert(first != JsonConstants.Slash); |
| | | 1883 | | |
| | 0 | 1884 | | TokenStartIndex = _consumed; |
| | | 1885 | | |
| | 0 | 1886 | | if (first == JsonConstants.ListSeparator) |
| | 0 | 1887 | | { |
| | | 1888 | | // A comma without some JSON value preceding it is invalid |
| | 0 | 1889 | | if (_previousTokenType <= JsonTokenType.StartObject || _previousTokenType == JsonTokenType.StartArray || |
| | 0 | 1890 | | { |
| | 0 | 1891 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyOrValueAfter |
| | | 1892 | | } |
| | | 1893 | | |
| | 0 | 1894 | | _consumed++; |
| | 0 | 1895 | | _bytePositionInLine++; |
| | | 1896 | | |
| | 0 | 1897 | | if (_consumed >= (uint)_buffer.Length) |
| | 0 | 1898 | | { |
| | 0 | 1899 | | if (IsLastSpan) |
| | 0 | 1900 | | { |
| | 0 | 1901 | | _consumed--; |
| | 0 | 1902 | | _bytePositionInLine--; |
| | 0 | 1903 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyOrValueN |
| | | 1904 | | } |
| | 0 | 1905 | | goto RollBack; |
| | | 1906 | | } |
| | 0 | 1907 | | first = _buffer[_consumed]; |
| | | 1908 | | |
| | | 1909 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 0 | 1910 | | if (first <= JsonConstants.Space) |
| | 0 | 1911 | | { |
| | 0 | 1912 | | SkipWhiteSpace(); |
| | | 1913 | | // The next character must be a start of a property name or value. |
| | 0 | 1914 | | if (!HasMoreData(ExceptionResource.ExpectedStartOfPropertyOrValueNotFound)) |
| | 0 | 1915 | | { |
| | 0 | 1916 | | goto RollBack; |
| | | 1917 | | } |
| | 0 | 1918 | | first = _buffer[_consumed]; |
| | 0 | 1919 | | } |
| | | 1920 | | |
| | 0 | 1921 | | TokenStartIndex = _consumed; |
| | | 1922 | | |
| | 0 | 1923 | | if (first == JsonConstants.Slash) |
| | 0 | 1924 | | { |
| | 0 | 1925 | | _trailingCommaBeforeComment = true; |
| | 0 | 1926 | | if (ConsumeComment()) |
| | 0 | 1927 | | { |
| | 0 | 1928 | | goto Done; |
| | | 1929 | | } |
| | | 1930 | | else |
| | 0 | 1931 | | { |
| | 0 | 1932 | | goto RollBack; |
| | | 1933 | | } |
| | | 1934 | | } |
| | | 1935 | | |
| | 0 | 1936 | | if (_inObject) |
| | 0 | 1937 | | { |
| | 0 | 1938 | | if (first != JsonConstants.Quote) |
| | 0 | 1939 | | { |
| | 0 | 1940 | | if (first == JsonConstants.CloseBrace) |
| | 0 | 1941 | | { |
| | 0 | 1942 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 1943 | | { |
| | 0 | 1944 | | EndObject(); |
| | 0 | 1945 | | goto Done; |
| | | 1946 | | } |
| | 0 | 1947 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBefo |
| | | 1948 | | } |
| | | 1949 | | |
| | 0 | 1950 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 1951 | | } |
| | 0 | 1952 | | if (ConsumePropertyName()) |
| | 0 | 1953 | | { |
| | 0 | 1954 | | goto Done; |
| | | 1955 | | } |
| | | 1956 | | else |
| | 0 | 1957 | | { |
| | 0 | 1958 | | goto RollBack; |
| | | 1959 | | } |
| | | 1960 | | } |
| | | 1961 | | else |
| | 0 | 1962 | | { |
| | 0 | 1963 | | if (first == JsonConstants.CloseBracket) |
| | 0 | 1964 | | { |
| | 0 | 1965 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 1966 | | { |
| | 0 | 1967 | | EndArray(); |
| | 0 | 1968 | | goto Done; |
| | | 1969 | | } |
| | 0 | 1970 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBeforeAr |
| | | 1971 | | } |
| | | 1972 | | |
| | 0 | 1973 | | if (ConsumeValue(first)) |
| | 0 | 1974 | | { |
| | 0 | 1975 | | goto Done; |
| | | 1976 | | } |
| | | 1977 | | else |
| | 0 | 1978 | | { |
| | 0 | 1979 | | goto RollBack; |
| | | 1980 | | } |
| | | 1981 | | } |
| | | 1982 | | } |
| | 0 | 1983 | | else if (first == JsonConstants.CloseBrace) |
| | 0 | 1984 | | { |
| | 0 | 1985 | | EndObject(); |
| | 0 | 1986 | | } |
| | 0 | 1987 | | else if (first == JsonConstants.CloseBracket) |
| | 0 | 1988 | | { |
| | 0 | 1989 | | EndArray(); |
| | 0 | 1990 | | } |
| | 0 | 1991 | | else if (_tokenType == JsonTokenType.None) |
| | 0 | 1992 | | { |
| | 0 | 1993 | | if (ReadFirstToken(first)) |
| | 0 | 1994 | | { |
| | 0 | 1995 | | goto Done; |
| | | 1996 | | } |
| | | 1997 | | else |
| | 0 | 1998 | | { |
| | 0 | 1999 | | goto RollBack; |
| | | 2000 | | } |
| | | 2001 | | } |
| | 0 | 2002 | | else if (_tokenType == JsonTokenType.StartObject) |
| | 0 | 2003 | | { |
| | 0 | 2004 | | Debug.Assert(first != JsonConstants.CloseBrace); |
| | 0 | 2005 | | if (first != JsonConstants.Quote) |
| | 0 | 2006 | | { |
| | 0 | 2007 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound, fi |
| | | 2008 | | } |
| | | 2009 | | |
| | 0 | 2010 | | int prevConsumed = _consumed; |
| | 0 | 2011 | | long prevPosition = _bytePositionInLine; |
| | 0 | 2012 | | long prevLineNumber = _lineNumber; |
| | 0 | 2013 | | if (!ConsumePropertyName()) |
| | 0 | 2014 | | { |
| | | 2015 | | // roll back potential changes |
| | 0 | 2016 | | _consumed = prevConsumed; |
| | 0 | 2017 | | _tokenType = JsonTokenType.StartObject; |
| | 0 | 2018 | | _bytePositionInLine = prevPosition; |
| | 0 | 2019 | | _lineNumber = prevLineNumber; |
| | 0 | 2020 | | goto RollBack; |
| | | 2021 | | } |
| | 0 | 2022 | | goto Done; |
| | | 2023 | | } |
| | 0 | 2024 | | else if (_tokenType == JsonTokenType.StartArray) |
| | 0 | 2025 | | { |
| | 0 | 2026 | | Debug.Assert(first != JsonConstants.CloseBracket); |
| | 0 | 2027 | | if (!ConsumeValue(first)) |
| | 0 | 2028 | | { |
| | 0 | 2029 | | goto RollBack; |
| | | 2030 | | } |
| | 0 | 2031 | | goto Done; |
| | | 2032 | | } |
| | 0 | 2033 | | else if (_tokenType == JsonTokenType.PropertyName) |
| | 0 | 2034 | | { |
| | 0 | 2035 | | if (!ConsumeValue(first)) |
| | 0 | 2036 | | { |
| | 0 | 2037 | | goto RollBack; |
| | | 2038 | | } |
| | 0 | 2039 | | goto Done; |
| | | 2040 | | } |
| | | 2041 | | else |
| | 0 | 2042 | | { |
| | 0 | 2043 | | Debug.Assert(_tokenType is JsonTokenType.EndArray or JsonTokenType.EndObject); |
| | 0 | 2044 | | if (_inObject) |
| | 0 | 2045 | | { |
| | 0 | 2046 | | Debug.Assert(first != JsonConstants.CloseBrace); |
| | 0 | 2047 | | if (first != JsonConstants.Quote) |
| | 0 | 2048 | | { |
| | 0 | 2049 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 2050 | | } |
| | | 2051 | | |
| | 0 | 2052 | | if (ConsumePropertyName()) |
| | 0 | 2053 | | { |
| | 0 | 2054 | | goto Done; |
| | | 2055 | | } |
| | | 2056 | | else |
| | 0 | 2057 | | { |
| | 0 | 2058 | | goto RollBack; |
| | | 2059 | | } |
| | | 2060 | | } |
| | | 2061 | | else |
| | 0 | 2062 | | { |
| | 0 | 2063 | | Debug.Assert(first != JsonConstants.CloseBracket); |
| | | 2064 | | |
| | 0 | 2065 | | if (ConsumeValue(first)) |
| | 0 | 2066 | | { |
| | 0 | 2067 | | goto Done; |
| | | 2068 | | } |
| | | 2069 | | else |
| | 0 | 2070 | | { |
| | 0 | 2071 | | goto RollBack; |
| | | 2072 | | } |
| | | 2073 | | } |
| | | 2074 | | } |
| | | 2075 | | |
| | 0 | 2076 | | Done: |
| | 0 | 2077 | | return ConsumeTokenResult.Success; |
| | | 2078 | | |
| | 0 | 2079 | | RollBack: |
| | 0 | 2080 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | 0 | 2081 | | } |
| | | 2082 | | |
| | | 2083 | | private bool SkipAllComments(scoped ref byte marker) |
| | 1170 | 2084 | | { |
| | 1170 | 2085 | | while (marker == JsonConstants.Slash) |
| | 507 | 2086 | | { |
| | 507 | 2087 | | if (SkipComment()) |
| | 0 | 2088 | | { |
| | 0 | 2089 | | if (!HasMoreData()) |
| | 0 | 2090 | | { |
| | 0 | 2091 | | goto IncompleteNoRollback; |
| | | 2092 | | } |
| | | 2093 | | |
| | 0 | 2094 | | marker = _buffer[_consumed]; |
| | | 2095 | | |
| | | 2096 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 0 | 2097 | | if (marker <= JsonConstants.Space) |
| | 0 | 2098 | | { |
| | 0 | 2099 | | SkipWhiteSpace(); |
| | 0 | 2100 | | if (!HasMoreData()) |
| | 0 | 2101 | | { |
| | 0 | 2102 | | goto IncompleteNoRollback; |
| | | 2103 | | } |
| | 0 | 2104 | | marker = _buffer[_consumed]; |
| | 0 | 2105 | | } |
| | 0 | 2106 | | } |
| | | 2107 | | else |
| | 0 | 2108 | | { |
| | 0 | 2109 | | goto IncompleteNoRollback; |
| | | 2110 | | } |
| | 0 | 2111 | | } |
| | 663 | 2112 | | return true; |
| | | 2113 | | |
| | 0 | 2114 | | IncompleteNoRollback: |
| | 0 | 2115 | | return false; |
| | 663 | 2116 | | } |
| | | 2117 | | |
| | | 2118 | | private bool SkipAllComments(scoped ref byte marker, ExceptionResource resource) |
| | 0 | 2119 | | { |
| | 0 | 2120 | | while (marker == JsonConstants.Slash) |
| | 0 | 2121 | | { |
| | 0 | 2122 | | if (SkipComment()) |
| | 0 | 2123 | | { |
| | | 2124 | | // The next character must be a start of a property name or value. |
| | 0 | 2125 | | if (!HasMoreData(resource)) |
| | 0 | 2126 | | { |
| | 0 | 2127 | | goto IncompleteRollback; |
| | | 2128 | | } |
| | | 2129 | | |
| | 0 | 2130 | | marker = _buffer[_consumed]; |
| | | 2131 | | |
| | | 2132 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 0 | 2133 | | if (marker <= JsonConstants.Space) |
| | 0 | 2134 | | { |
| | 0 | 2135 | | SkipWhiteSpace(); |
| | | 2136 | | // The next character must be a start of a property name or value. |
| | 0 | 2137 | | if (!HasMoreData(resource)) |
| | 0 | 2138 | | { |
| | 0 | 2139 | | goto IncompleteRollback; |
| | | 2140 | | } |
| | 0 | 2141 | | marker = _buffer[_consumed]; |
| | 0 | 2142 | | } |
| | 0 | 2143 | | } |
| | | 2144 | | else |
| | 0 | 2145 | | { |
| | 0 | 2146 | | goto IncompleteRollback; |
| | | 2147 | | } |
| | 0 | 2148 | | } |
| | 0 | 2149 | | return true; |
| | | 2150 | | |
| | 0 | 2151 | | IncompleteRollback: |
| | 0 | 2152 | | return false; |
| | 0 | 2153 | | } |
| | | 2154 | | |
| | | 2155 | | private ConsumeTokenResult ConsumeNextTokenUntilAfterAllCommentsAreSkipped(byte marker) |
| | 1170 | 2156 | | { |
| | 1170 | 2157 | | if (!SkipAllComments(ref marker)) |
| | 0 | 2158 | | { |
| | 0 | 2159 | | goto IncompleteNoRollback; |
| | | 2160 | | } |
| | | 2161 | | |
| | 663 | 2162 | | TokenStartIndex = _consumed; |
| | | 2163 | | |
| | 663 | 2164 | | if (_tokenType == JsonTokenType.StartObject) |
| | 0 | 2165 | | { |
| | 0 | 2166 | | if (marker == JsonConstants.CloseBrace) |
| | 0 | 2167 | | { |
| | 0 | 2168 | | EndObject(); |
| | 0 | 2169 | | } |
| | | 2170 | | else |
| | 0 | 2171 | | { |
| | 0 | 2172 | | if (marker != JsonConstants.Quote) |
| | 0 | 2173 | | { |
| | 0 | 2174 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 2175 | | } |
| | | 2176 | | |
| | 0 | 2177 | | int prevConsumed = _consumed; |
| | 0 | 2178 | | long prevPosition = _bytePositionInLine; |
| | 0 | 2179 | | long prevLineNumber = _lineNumber; |
| | 0 | 2180 | | if (!ConsumePropertyName()) |
| | 0 | 2181 | | { |
| | | 2182 | | // roll back potential changes |
| | 0 | 2183 | | _consumed = prevConsumed; |
| | 0 | 2184 | | _tokenType = JsonTokenType.StartObject; |
| | 0 | 2185 | | _bytePositionInLine = prevPosition; |
| | 0 | 2186 | | _lineNumber = prevLineNumber; |
| | 0 | 2187 | | goto IncompleteNoRollback; |
| | | 2188 | | } |
| | 0 | 2189 | | goto Done; |
| | | 2190 | | } |
| | 0 | 2191 | | } |
| | 663 | 2192 | | else if (_tokenType == JsonTokenType.StartArray) |
| | 0 | 2193 | | { |
| | 0 | 2194 | | if (marker == JsonConstants.CloseBracket) |
| | 0 | 2195 | | { |
| | 0 | 2196 | | EndArray(); |
| | 0 | 2197 | | } |
| | | 2198 | | else |
| | 0 | 2199 | | { |
| | 0 | 2200 | | if (!ConsumeValue(marker)) |
| | 0 | 2201 | | { |
| | 0 | 2202 | | goto IncompleteNoRollback; |
| | | 2203 | | } |
| | 0 | 2204 | | goto Done; |
| | | 2205 | | } |
| | 0 | 2206 | | } |
| | 663 | 2207 | | else if (_tokenType == JsonTokenType.PropertyName) |
| | 0 | 2208 | | { |
| | 0 | 2209 | | if (!ConsumeValue(marker)) |
| | 0 | 2210 | | { |
| | 0 | 2211 | | goto IncompleteNoRollback; |
| | | 2212 | | } |
| | 0 | 2213 | | goto Done; |
| | | 2214 | | } |
| | 663 | 2215 | | else if (_bitStack.CurrentDepth == 0) |
| | 663 | 2216 | | { |
| | 663 | 2217 | | if (_readerOptions.AllowMultipleValues) |
| | 0 | 2218 | | { |
| | 0 | 2219 | | return ReadFirstToken(marker) ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBac |
| | | 2220 | | } |
| | | 2221 | | |
| | 663 | 2222 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndAfterSingleJson, marker); |
| | | 2223 | | } |
| | 0 | 2224 | | else if (marker == JsonConstants.ListSeparator) |
| | 0 | 2225 | | { |
| | 0 | 2226 | | _consumed++; |
| | 0 | 2227 | | _bytePositionInLine++; |
| | | 2228 | | |
| | 0 | 2229 | | if (_consumed >= (uint)_buffer.Length) |
| | 0 | 2230 | | { |
| | 0 | 2231 | | if (IsLastSpan) |
| | 0 | 2232 | | { |
| | 0 | 2233 | | _consumed--; |
| | 0 | 2234 | | _bytePositionInLine--; |
| | 0 | 2235 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyOrValueN |
| | | 2236 | | } |
| | 0 | 2237 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | | 2238 | | } |
| | 0 | 2239 | | marker = _buffer[_consumed]; |
| | | 2240 | | |
| | | 2241 | | // This check is done as an optimization to avoid calling SkipWhiteSpace when not necessary. |
| | 0 | 2242 | | if (marker <= JsonConstants.Space) |
| | 0 | 2243 | | { |
| | 0 | 2244 | | SkipWhiteSpace(); |
| | | 2245 | | // The next character must be a start of a property name or value. |
| | 0 | 2246 | | if (!HasMoreData(ExceptionResource.ExpectedStartOfPropertyOrValueNotFound)) |
| | 0 | 2247 | | { |
| | 0 | 2248 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | | 2249 | | } |
| | 0 | 2250 | | marker = _buffer[_consumed]; |
| | 0 | 2251 | | } |
| | | 2252 | | |
| | 0 | 2253 | | if (!SkipAllComments(ref marker, ExceptionResource.ExpectedStartOfPropertyOrValueNotFound)) |
| | 0 | 2254 | | { |
| | 0 | 2255 | | goto IncompleteRollback; |
| | | 2256 | | } |
| | | 2257 | | |
| | 0 | 2258 | | TokenStartIndex = _consumed; |
| | | 2259 | | |
| | 0 | 2260 | | if (_inObject) |
| | 0 | 2261 | | { |
| | 0 | 2262 | | if (marker != JsonConstants.Quote) |
| | 0 | 2263 | | { |
| | 0 | 2264 | | if (marker == JsonConstants.CloseBrace) |
| | 0 | 2265 | | { |
| | 0 | 2266 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 2267 | | { |
| | 0 | 2268 | | EndObject(); |
| | 0 | 2269 | | goto Done; |
| | | 2270 | | } |
| | 0 | 2271 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBefo |
| | | 2272 | | } |
| | | 2273 | | |
| | 0 | 2274 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfPropertyNotFound |
| | | 2275 | | } |
| | 0 | 2276 | | return ConsumePropertyName() ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBack |
| | | 2277 | | } |
| | | 2278 | | else |
| | 0 | 2279 | | { |
| | 0 | 2280 | | if (marker == JsonConstants.CloseBracket) |
| | 0 | 2281 | | { |
| | 0 | 2282 | | if (_readerOptions.AllowTrailingCommas) |
| | 0 | 2283 | | { |
| | 0 | 2284 | | EndArray(); |
| | 0 | 2285 | | goto Done; |
| | | 2286 | | } |
| | 0 | 2287 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.TrailingCommaNotAllowedBeforeAr |
| | | 2288 | | } |
| | | 2289 | | |
| | 0 | 2290 | | return ConsumeValue(marker) ? ConsumeTokenResult.Success : ConsumeTokenResult.NotEnoughDataRollBackS |
| | | 2291 | | } |
| | | 2292 | | } |
| | 0 | 2293 | | else if (marker == JsonConstants.CloseBrace) |
| | 0 | 2294 | | { |
| | 0 | 2295 | | EndObject(); |
| | 0 | 2296 | | } |
| | 0 | 2297 | | else if (marker == JsonConstants.CloseBracket) |
| | 0 | 2298 | | { |
| | 0 | 2299 | | EndArray(); |
| | 0 | 2300 | | } |
| | | 2301 | | else |
| | 0 | 2302 | | { |
| | 0 | 2303 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.FoundInvalidCharacter, marker); |
| | | 2304 | | } |
| | | 2305 | | |
| | 0 | 2306 | | Done: |
| | 0 | 2307 | | return ConsumeTokenResult.Success; |
| | 0 | 2308 | | IncompleteNoRollback: |
| | 0 | 2309 | | return ConsumeTokenResult.IncompleteNoRollBackNecessary; |
| | 0 | 2310 | | IncompleteRollback: |
| | 0 | 2311 | | return ConsumeTokenResult.NotEnoughDataRollBackState; |
| | 0 | 2312 | | } |
| | | 2313 | | |
| | | 2314 | | private bool SkipComment() |
| | 1081 | 2315 | | { |
| | | 2316 | | // Create local copy to avoid bounds checks. |
| | 1081 | 2317 | | ReadOnlySpan<byte> localBuffer = _buffer.Slice(_consumed + 1); |
| | | 2318 | | |
| | 1081 | 2319 | | if (localBuffer.Length > 0) |
| | 1081 | 2320 | | { |
| | 1081 | 2321 | | byte marker = localBuffer[0]; |
| | 1081 | 2322 | | if (marker == JsonConstants.Slash) |
| | 492 | 2323 | | { |
| | 492 | 2324 | | return SkipSingleLineComment(localBuffer.Slice(1), out _); |
| | | 2325 | | } |
| | 589 | 2326 | | else if (marker == JsonConstants.Asterisk) |
| | 0 | 2327 | | { |
| | 0 | 2328 | | return SkipMultiLineComment(localBuffer.Slice(1), out _); |
| | | 2329 | | } |
| | | 2330 | | else |
| | 589 | 2331 | | { |
| | 589 | 2332 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, JsonC |
| | | 2333 | | } |
| | | 2334 | | } |
| | | 2335 | | |
| | 0 | 2336 | | if (IsLastSpan) |
| | 0 | 2337 | | { |
| | 0 | 2338 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, JsonConst |
| | | 2339 | | } |
| | 0 | 2340 | | return false; |
| | 492 | 2341 | | } |
| | | 2342 | | |
| | | 2343 | | private bool SkipSingleLineComment(ReadOnlySpan<byte> localBuffer, out int idx) |
| | 492 | 2344 | | { |
| | 492 | 2345 | | idx = FindLineSeparator(localBuffer); |
| | | 2346 | | int toConsume; |
| | 492 | 2347 | | if (idx != -1) |
| | 123 | 2348 | | { |
| | 123 | 2349 | | toConsume = idx; |
| | 123 | 2350 | | if (localBuffer[idx] == JsonConstants.LineFeed) |
| | 82 | 2351 | | { |
| | 82 | 2352 | | goto EndOfComment; |
| | | 2353 | | } |
| | | 2354 | | |
| | | 2355 | | // If we are here, we have definintely found a \r. So now to check if \n follows. |
| | 41 | 2356 | | Debug.Assert(localBuffer[idx] == JsonConstants.CarriageReturn); |
| | | 2357 | | |
| | 41 | 2358 | | if (idx < localBuffer.Length - 1) |
| | 41 | 2359 | | { |
| | 41 | 2360 | | if (localBuffer[idx + 1] == JsonConstants.LineFeed) |
| | 0 | 2361 | | { |
| | 0 | 2362 | | toConsume++; |
| | 0 | 2363 | | } |
| | | 2364 | | |
| | 41 | 2365 | | goto EndOfComment; |
| | | 2366 | | } |
| | | 2367 | | |
| | 0 | 2368 | | if (IsLastSpan) |
| | 0 | 2369 | | { |
| | 0 | 2370 | | goto EndOfComment; |
| | | 2371 | | } |
| | | 2372 | | else |
| | 0 | 2373 | | { |
| | | 2374 | | // there might be LF in the next segment |
| | 0 | 2375 | | return false; |
| | | 2376 | | } |
| | | 2377 | | } |
| | | 2378 | | |
| | 369 | 2379 | | if (IsLastSpan) |
| | 369 | 2380 | | { |
| | 369 | 2381 | | idx = localBuffer.Length; |
| | 369 | 2382 | | toConsume = idx; |
| | | 2383 | | // Assume everything on this line is a comment and there is no more data. |
| | 369 | 2384 | | _bytePositionInLine += 2 + localBuffer.Length; |
| | 369 | 2385 | | goto Done; |
| | | 2386 | | } |
| | | 2387 | | else |
| | 0 | 2388 | | { |
| | 0 | 2389 | | return false; |
| | | 2390 | | } |
| | | 2391 | | |
| | 123 | 2392 | | EndOfComment: |
| | 123 | 2393 | | toConsume++; |
| | 123 | 2394 | | _bytePositionInLine = 0; |
| | 123 | 2395 | | _lineNumber++; |
| | | 2396 | | |
| | 492 | 2397 | | Done: |
| | 492 | 2398 | | _consumed += 2 + toConsume; |
| | 492 | 2399 | | return true; |
| | 492 | 2400 | | } |
| | | 2401 | | |
| | | 2402 | | private int FindLineSeparator(ReadOnlySpan<byte> localBuffer) |
| | 492 | 2403 | | { |
| | 492 | 2404 | | int totalIdx = 0; |
| | 492 | 2405 | | while (true) |
| | 492 | 2406 | | { |
| | 492 | 2407 | | int idx = localBuffer.IndexOfAny(JsonConstants.LineFeed, JsonConstants.CarriageReturn, JsonConstants.Sta |
| | | 2408 | | |
| | 492 | 2409 | | if (idx == -1) |
| | 369 | 2410 | | { |
| | 369 | 2411 | | return -1; |
| | | 2412 | | } |
| | | 2413 | | |
| | 123 | 2414 | | totalIdx += idx; |
| | | 2415 | | |
| | 123 | 2416 | | if (localBuffer[idx] != JsonConstants.StartingByteOfNonStandardSeparator) |
| | 123 | 2417 | | { |
| | 123 | 2418 | | return totalIdx; |
| | | 2419 | | } |
| | | 2420 | | |
| | 0 | 2421 | | totalIdx++; |
| | 0 | 2422 | | localBuffer = localBuffer.Slice(idx + 1); |
| | | 2423 | | |
| | 0 | 2424 | | ThrowOnDangerousLineSeparator(localBuffer); |
| | 0 | 2425 | | } |
| | 492 | 2426 | | } |
| | | 2427 | | |
| | | 2428 | | // assumes first byte (JsonConstants.StartingByteOfNonStandardSeparator) is already read |
| | | 2429 | | private void ThrowOnDangerousLineSeparator(ReadOnlySpan<byte> localBuffer) |
| | 0 | 2430 | | { |
| | | 2431 | | // \u2028 and \u2029 are considered respectively line and paragraph separators |
| | | 2432 | | // UTF-8 representation for them is E2, 80, A8/A9 |
| | | 2433 | | // we have already read E2, we need to check for remaining 2 bytes |
| | | 2434 | | |
| | 0 | 2435 | | if (localBuffer.Length < 2) |
| | 0 | 2436 | | { |
| | 0 | 2437 | | return; |
| | | 2438 | | } |
| | | 2439 | | |
| | 0 | 2440 | | byte next = localBuffer[1]; |
| | 0 | 2441 | | if (localBuffer[0] == 0x80 && (next == 0xA8 || next == 0xA9)) |
| | 0 | 2442 | | { |
| | 0 | 2443 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.UnexpectedEndOfLineSeparator); |
| | | 2444 | | } |
| | 0 | 2445 | | } |
| | | 2446 | | |
| | | 2447 | | private bool SkipMultiLineComment(ReadOnlySpan<byte> localBuffer, out int idx) |
| | 0 | 2448 | | { |
| | 0 | 2449 | | idx = 0; |
| | 0 | 2450 | | while (true) |
| | 0 | 2451 | | { |
| | 0 | 2452 | | int foundIdx = localBuffer.Slice(idx).IndexOf(JsonConstants.Slash); |
| | 0 | 2453 | | if (foundIdx == -1) |
| | 0 | 2454 | | { |
| | 0 | 2455 | | if (IsLastSpan) |
| | 0 | 2456 | | { |
| | 0 | 2457 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.EndOfCommentNotFound); |
| | | 2458 | | } |
| | 0 | 2459 | | return false; |
| | | 2460 | | } |
| | 0 | 2461 | | if (foundIdx != 0 && localBuffer[foundIdx + idx - 1] == JsonConstants.Asterisk) |
| | 0 | 2462 | | { |
| | | 2463 | | // foundIdx points just after '*' in the end-of-comment delimiter. Hence increment idx by one |
| | | 2464 | | // position less to make it point right before beginning of end-of-comment delimiter i.e. */ |
| | 0 | 2465 | | idx += foundIdx - 1; |
| | 0 | 2466 | | break; |
| | | 2467 | | } |
| | 0 | 2468 | | idx += foundIdx + 1; |
| | 0 | 2469 | | } |
| | | 2470 | | |
| | | 2471 | | // Consume the /* and */ characters that are part of the multi-line comment. |
| | | 2472 | | // idx points right before the final '*' (which is right before the last '/'). Hence increment _consumed |
| | | 2473 | | // by 4 to exclude the start/end-of-comment delimiters. |
| | 0 | 2474 | | _consumed += 4 + idx; |
| | | 2475 | | |
| | 0 | 2476 | | (int newLines, int newLineIndex) = JsonReaderHelper.CountNewLines(localBuffer.Slice(0, idx)); |
| | 0 | 2477 | | _lineNumber += newLines; |
| | 0 | 2478 | | if (newLineIndex != -1) |
| | 0 | 2479 | | { |
| | | 2480 | | // newLineIndex points at last newline character and byte positions in the new line start |
| | | 2481 | | // after that. Hence add 1 to skip the newline character. |
| | 0 | 2482 | | _bytePositionInLine = idx - newLineIndex + 1; |
| | 0 | 2483 | | } |
| | | 2484 | | else |
| | 0 | 2485 | | { |
| | 0 | 2486 | | _bytePositionInLine += 4 + idx; |
| | 0 | 2487 | | } |
| | 0 | 2488 | | return true; |
| | 0 | 2489 | | } |
| | | 2490 | | |
| | | 2491 | | private bool ConsumeComment() |
| | 0 | 2492 | | { |
| | | 2493 | | // Create local copy to avoid bounds checks. |
| | 0 | 2494 | | ReadOnlySpan<byte> localBuffer = _buffer.Slice(_consumed + 1); |
| | | 2495 | | |
| | 0 | 2496 | | if (localBuffer.Length > 0) |
| | 0 | 2497 | | { |
| | 0 | 2498 | | byte marker = localBuffer[0]; |
| | 0 | 2499 | | if (marker == JsonConstants.Slash) |
| | 0 | 2500 | | { |
| | 0 | 2501 | | return ConsumeSingleLineComment(localBuffer.Slice(1), _consumed); |
| | | 2502 | | } |
| | 0 | 2503 | | else if (marker == JsonConstants.Asterisk) |
| | 0 | 2504 | | { |
| | 0 | 2505 | | return ConsumeMultiLineComment(localBuffer.Slice(1), _consumed); |
| | | 2506 | | } |
| | | 2507 | | else |
| | 0 | 2508 | | { |
| | 0 | 2509 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.InvalidCharacterAtStartOfComment, m |
| | | 2510 | | } |
| | | 2511 | | } |
| | | 2512 | | |
| | 0 | 2513 | | if (IsLastSpan) |
| | 0 | 2514 | | { |
| | 0 | 2515 | | ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.UnexpectedEndOfDataWhileReadingComment) |
| | | 2516 | | } |
| | 0 | 2517 | | return false; |
| | 0 | 2518 | | } |
| | | 2519 | | |
| | | 2520 | | private bool ConsumeSingleLineComment(ReadOnlySpan<byte> localBuffer, int previousConsumed) |
| | 0 | 2521 | | { |
| | 0 | 2522 | | if (!SkipSingleLineComment(localBuffer, out int idx)) |
| | 0 | 2523 | | { |
| | 0 | 2524 | | return false; |
| | | 2525 | | } |
| | | 2526 | | |
| | | 2527 | | // Exclude the // at start of the comment. idx points right before the line separator |
| | | 2528 | | // at the end of the comment. |
| | 0 | 2529 | | ValueSpan = _buffer.Slice(previousConsumed + 2, idx); |
| | 0 | 2530 | | if (_tokenType != JsonTokenType.Comment) |
| | 0 | 2531 | | { |
| | 0 | 2532 | | _previousTokenType = _tokenType; |
| | 0 | 2533 | | } |
| | 0 | 2534 | | _tokenType = JsonTokenType.Comment; |
| | 0 | 2535 | | return true; |
| | 0 | 2536 | | } |
| | | 2537 | | |
| | | 2538 | | private bool ConsumeMultiLineComment(ReadOnlySpan<byte> localBuffer, int previousConsumed) |
| | 0 | 2539 | | { |
| | 0 | 2540 | | if (!SkipMultiLineComment(localBuffer, out int idx)) |
| | 0 | 2541 | | { |
| | 0 | 2542 | | return false; |
| | | 2543 | | } |
| | | 2544 | | |
| | | 2545 | | // Exclude the /* at start of the comment. idx already points right before the terminal '*/' |
| | | 2546 | | // for the end of multiline comment. |
| | 0 | 2547 | | ValueSpan = _buffer.Slice(previousConsumed + 2, idx); |
| | 0 | 2548 | | if (_tokenType != JsonTokenType.Comment) |
| | 0 | 2549 | | { |
| | 0 | 2550 | | _previousTokenType = _tokenType; |
| | 0 | 2551 | | } |
| | 0 | 2552 | | _tokenType = JsonTokenType.Comment; |
| | 0 | 2553 | | return true; |
| | 0 | 2554 | | } |
| | | 2555 | | |
| | | 2556 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | 0 | 2557 | | private string DebuggerDisplay => $"TokenType = {DebugTokenType}, TokenStartIndex = {TokenStartIndex}, Consumed |
| | | 2558 | | |
| | | 2559 | | // Using TokenType.ToString() (or {TokenType}) fails to render in the debug window. The |
| | | 2560 | | // message "The runtime refused to evaluate the expression at this time." is shown. This |
| | | 2561 | | // is a workaround until we root cause and fix the issue. |
| | | 2562 | | private string DebugTokenType |
| | 0 | 2563 | | => TokenType switch |
| | 0 | 2564 | | { |
| | 0 | 2565 | | JsonTokenType.Comment => nameof(JsonTokenType.Comment), |
| | 0 | 2566 | | JsonTokenType.EndArray => nameof(JsonTokenType.EndArray), |
| | 0 | 2567 | | JsonTokenType.EndObject => nameof(JsonTokenType.EndObject), |
| | 0 | 2568 | | JsonTokenType.False => nameof(JsonTokenType.False), |
| | 0 | 2569 | | JsonTokenType.None => nameof(JsonTokenType.None), |
| | 0 | 2570 | | JsonTokenType.Null => nameof(JsonTokenType.Null), |
| | 0 | 2571 | | JsonTokenType.Number => nameof(JsonTokenType.Number), |
| | 0 | 2572 | | JsonTokenType.PropertyName => nameof(JsonTokenType.PropertyName), |
| | 0 | 2573 | | JsonTokenType.StartArray => nameof(JsonTokenType.StartArray), |
| | 0 | 2574 | | JsonTokenType.StartObject => nameof(JsonTokenType.StartObject), |
| | 0 | 2575 | | JsonTokenType.String => nameof(JsonTokenType.String), |
| | 0 | 2576 | | JsonTokenType.True => nameof(JsonTokenType.True), |
| | 0 | 2577 | | _ => ((byte)TokenType).ToString() |
| | 0 | 2578 | | }; |
| | | 2579 | | |
| | | 2580 | | private ReadOnlySpan<byte> GetUnescapedSpan() |
| | 1634 | 2581 | | { |
| | 1634 | 2582 | | ReadOnlySpan<byte> span = HasValueSequence ? ValueSequence.ToArray() : ValueSpan; |
| | 1634 | 2583 | | if (ValueIsEscaped) |
| | 0 | 2584 | | { |
| | 0 | 2585 | | span = JsonReaderHelper.GetUnescaped(span); |
| | 0 | 2586 | | } |
| | | 2587 | | |
| | 1634 | 2588 | | return span; |
| | 1634 | 2589 | | } |
| | | 2590 | | } |
| | | 2591 | | } |