Example #1
0
                /// <summary>
                /// Parses out a JSON array AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON array AST node</returns>
                private static ArrayNode ParseArrayNode(IJsonTextReaderPrivateImplementation jsonTextReader)
                {
                    List <JsonTextNavigatorNode> items = new List <JsonTextNavigatorNode>();

                    ReadOnlyMemory <byte> bufferedArrayStartToken = jsonTextReader.GetBufferedJsonToken().Memory;

                    if (!MemoryMarshal.TryGetArray(bufferedArrayStartToken, out ArraySegment <byte> startArrayArraySegment))
                    {
                        throw new InvalidOperationException($"Failed to get {nameof(startArrayArraySegment)}.");
                    }

                    // consume the begin array token
                    jsonTextReader.Read();

                    while (jsonTextReader.CurrentTokenType != JsonTokenType.EndArray)
                    {
                        items.Add(Parser.ParseNode(jsonTextReader));
                    }

                    ReadOnlyMemory <byte> bufferedArrayEndToken = jsonTextReader.GetBufferedJsonToken().Memory;

                    if (!MemoryMarshal.TryGetArray(bufferedArrayEndToken, out ArraySegment <byte> endArrayArraySegment))
                    {
                        throw new InvalidOperationException($"Failed to get {nameof(endArrayArraySegment)}.");
                    }

                    // consume the end array token
                    jsonTextReader.Read();

                    ReadOnlyMemory <byte> bufferedRawArray = startArrayArraySegment.Array;

                    bufferedRawArray = bufferedRawArray.Slice(start: startArrayArraySegment.Offset, length: endArrayArraySegment.Offset - startArrayArraySegment.Offset + 1);

                    return(ArrayNode.Create(items, bufferedRawArray));
                }
                /// <summary>
                /// Parses out a JSON array AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON array AST node</returns>
                private static ArrayNode ParseArrayNode(IJsonReader jsonTextReader)
                {
                    List <JsonTextNode> items = new List <JsonTextNode>();

                    // consume the begin array token
                    jsonTextReader.Read();

                    while (jsonTextReader.CurrentTokenType != JsonTokenType.EndArray)
                    {
                        items.Add(Parser.ParseNode(jsonTextReader));
                    }

                    // consume the end array token
                    jsonTextReader.Read();

                    return(ArrayNode.Create(items));
                }