Beispiel #1
0
            public override IJsonReader CreateReader(IJsonNavigatorNode jsonNavigatorNode)
            {
                if (!(jsonNavigatorNode is JsonTextNavigatorNode jsonTextNavigatorNode))
                {
                    throw new ArgumentException($"{nameof(jsonNavigatorNode)} must be a {nameof(JsonTextNavigatorNode)}.");
                }

                ReadOnlyMemory <byte> buffer = jsonTextNavigatorNode switch
                {
                    LazyNode lazyNode => lazyNode.BufferedValue,
                    ArrayNode arrayNode => arrayNode.BufferedValue,
                    FalseNode falseNode => SingletonBuffers.False,
                    StringNodeBase stringNodeBase => stringNodeBase.BufferedValue.Memory,
                    NullNode nullNode => SingletonBuffers.Null,
                    NumberNode numberNode => numberNode.BufferedToken,
                    ObjectNode objectNode => objectNode.BufferedValue,
                    TrueNode trueNode => SingletonBuffers.True,
                    GuidNode guidNode => guidNode.BufferedToken,
                    BinaryNode binaryNode => binaryNode.BufferedToken,
                    IntegerNode intNode => intNode.BufferedToken,
                    FloatNode floatNode => floatNode.BufferedToken,
                                      _ => throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonTextNavigatorNode)} type: {jsonTextNavigatorNode.GetType()}."),
                };

                return(JsonReader.Create(JsonSerializationFormat.Text, buffer));
            }
Beispiel #2
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>
            /// Gets an IEnumerable of <see cref="IJsonNavigatorNode"/>s for an arrayNode.
            /// </summary>
            /// <param name="arrayNavigatorNode">The <see cref="IJsonNavigatorNode"/> of the array to get the items from</param>
            /// <returns>The IEnumerable of <see cref="IJsonNavigatorNode"/>s for an arrayNode.</returns>
            public override IEnumerable<IJsonNavigatorNode> GetArrayItems(IJsonNavigatorNode arrayNavigatorNode)
            {
                if (arrayNavigatorNode == null)
                {
                    throw new ArgumentNullException("arrayNode");
                }

                ArrayNode arrayNode = arrayNavigatorNode as ArrayNode;
                if (arrayNode == null)
                {
                    throw new ArgumentException("arrayNavigatorNode must actually be an array node");
                }

                return arrayNode.Items;
            }
            /// <summary>
            /// Gets the node at a particular index of an array node
            /// </summary>
            /// <param name="arrayNavigatorNode">The <see cref="IJsonNavigatorNode"/> of the (array) node to index from.</param>
            /// <param name="index">The offset into the array</param>
            /// <returns>The <see cref="IJsonNavigatorNode"/> of the node at a particular index of an array node</returns>
            public override IJsonNavigatorNode GetArrayItemAt(IJsonNavigatorNode arrayNavigatorNode, int index)
            {
                if (arrayNavigatorNode == null)
                {
                    throw new ArgumentNullException("arrayNode");
                }

                ArrayNode arrayNode = arrayNavigatorNode as ArrayNode;
                if (arrayNode == null)
                {
                    throw new ArgumentException("arrayNavigatorNode must actually be an array node");
                }

                return arrayNode.Items[index];
            }
            /// <summary>
            /// Gets the number of elements in an array node.
            /// </summary>
            /// <param name="arrayNavigatorNode">The <see cref="IJsonNavigatorNode"/> of the (array) node to get the count of.</param>
            /// <returns>The number of elements in the array node.</returns>
            public override int GetArrayItemCount(IJsonNavigatorNode arrayNavigatorNode)
            {
                if (arrayNavigatorNode == null)
                {
                    throw new ArgumentNullException("arrayNavigatorNode");
                }

                ArrayNode arrayNode = arrayNavigatorNode as ArrayNode;
                if (arrayNode == null)
                {
                    throw new ArgumentException("arrayNavigatorNode must actually be an array node");
                }

                return arrayNode.Items.Count;
            }
                /// <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));
                }
Beispiel #7
0
 private static ReadOnlyMemory <byte> GetNodeBuffer(JsonTextNavigatorNode jsonTextNavigatorNode)
 {
     return(jsonTextNavigatorNode switch
     {
         LazyNode lazyNode => lazyNode.BufferedValue,
         ArrayNode arrayNode => arrayNode.BufferedValue,
         FalseNode falseNode => SingletonBuffers.False,
         StringNodeBase stringNodeBase => stringNodeBase.BufferedValue.Memory,
         NullNode nullNode => SingletonBuffers.Null,
         NumberNode numberNode => numberNode.BufferedToken,
         ObjectNode objectNode => objectNode.BufferedValue,
         TrueNode trueNode => SingletonBuffers.True,
         GuidNode guidNode => guidNode.BufferedToken,
         BinaryNode binaryNode => binaryNode.BufferedToken,
         IntegerNode intNode => intNode.BufferedToken,
         FloatNode floatNode => floatNode.BufferedToken,
         _ => throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonTextNavigatorNode)} type: {jsonTextNavigatorNode.GetType()}."),
     });