/// <summary>
            /// Initializes a new instance of the JsonTextNavigator class
            /// </summary>
            /// <param name="buffer">The (UTF-8) buffer to navigate.</param>
            /// <param name="skipValidation">whether to skip validation or not.</param>
            public JsonTextNavigator(byte[] buffer, bool skipValidation = false)
            {
                IJsonReader jsonTextReader = JsonReader.Create(buffer, skipValidation);

                if (jsonTextReader.SerializationFormat != JsonSerializationFormat.Text)
                {
                    throw new ArgumentException("jsonTextReader's serialization format must actually be text");
                }

                this.rootNode = JsonTextParser.Parse(jsonTextReader);
            }
                /// <summary>
                /// Parses out a JSON property AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON property AST node</returns>
                private static ObjectProperty ParsePropertyNode(IJsonReader jsonTextReader)
                {
                    FieldNameNode fieldName = FieldNameNode.Create((ArraySegment <byte>)jsonTextReader.GetBufferedRawJsonToken());

                    // Consume the fieldname from the jsonreader
                    jsonTextReader.Read();

                    JsonTextNode value = JsonTextParser.ParseNode(jsonTextReader);

                    return(new ObjectProperty(fieldName, value));
                }
            /// <summary>
            /// Gets the <see cref="JsonNodeType"/> type for a particular node
            /// </summary>
            /// <param name="node">The <see cref="IJsonNavigatorNode"/> of the node you want to know the type of</param>
            /// <returns><see cref="JsonNodeType"/> for the node</returns>
            public override JsonNodeType GetNodeType(IJsonNavigatorNode node)
            {
                if (node == null)
                {
                    throw new ArgumentNullException("node");
                }

                JsonTextNode jsonTextNode = node as JsonTextNode;
                if (jsonTextNode == null)
                {
                    throw new ArgumentException("node must actually be a text node.");
                }

                return jsonTextNode.JsonNodeType;
            }
            /// <summary>
            /// Initializes a new instance of the <see cref="JsonTextNavigator"/> class.
            /// </summary>
            /// <param name="buffer">The (UTF-8) buffer to navigate.</param>
            /// <param name="skipValidation">whether to skip validation or not.</param>
            public JsonTextNavigator(
                ReadOnlyMemory <byte> buffer,
                bool skipValidation = false)
            {
                IJsonReader jsonTextReader = JsonReader.Create(
                    buffer: buffer,
                    jsonStringDictionary: null,
                    skipValidation: skipValidation);

                if (jsonTextReader.SerializationFormat != JsonSerializationFormat.Text)
                {
                    throw new ArgumentException("jsonTextReader's serialization format must actually be text");
                }

                this.rootNode = Parser.Parse(jsonTextReader);
            }
Esempio n. 5
0
                /// <summary>
                /// Parses out a JSON property AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON property AST node</returns>
                private static ObjectProperty ParsePropertyNode(IJsonReader jsonTextReader)
                {
                    if (!jsonTextReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedRawJsonToken))
                    {
                        throw new InvalidOperationException("Failed to get the buffered raw json token.");
                    }

                    FieldNameNode fieldName = FieldNameNode.Create(bufferedRawJsonToken);

                    // Consume the fieldname from the jsonreader
                    jsonTextReader.Read();

                    JsonTextNode value = Parser.ParseNode(jsonTextReader);

                    return(new ObjectProperty(fieldName, value));
                }
                /// <summary>
                /// Gets the root node of a JSON AST from a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>The root node of a JSON AST from a jsonTextReader.</returns>
                public static JsonTextNode Parse(IJsonReader jsonTextReader)
                {
                    if (jsonTextReader.SerializationFormat != JsonSerializationFormat.Text)
                    {
                        throw new ArgumentException("jsonTextReader's serialization format must actually be text");
                    }

                    // Read past the json object not started state.
                    jsonTextReader.Read();

                    JsonTextNode rootNode = Parser.ParseNode(jsonTextReader);

                    // Make sure that we are at the end of the file.
                    if (jsonTextReader.Read())
                    {
                        throw new ArgumentException("Did not fully parse json");
                    }

                    return(rootNode);
                }
Esempio n. 7
0
 public void Visit(JsonTextNode node)
 {
     VisitLiteral(node.TextToken);
 }