Beispiel #1
0
            /// <inheritdoc />
            public override bool TryGetObjectProperty(
                IJsonNavigatorNode objectNode,
                string propertyName,
                out ObjectProperty objectProperty)
            {
                ReadOnlyMemory <byte> buffer = JsonBinaryNavigator.GetNodeOfType(
                    JsonNodeType.Object,
                    objectNode);

                ReadOnlySpan <byte> utf8StringPropertyName = Encoding.UTF8.GetBytes(propertyName);

                foreach (ObjectProperty objectPropertyNode in this.GetObjectProperties(objectNode))
                {
                    if (this.TryGetBufferedUtf8StringValue(objectPropertyNode.NameNode, out ReadOnlyMemory <byte> bufferedUtf8StringValue))
                    {
                        // First try and see if we can avoid materializing the UTF16 string.
                        if (utf8StringPropertyName.SequenceEqual(bufferedUtf8StringValue.Span))
                        {
                            objectProperty = objectPropertyNode;
                            return(true);
                        }
                    }
                    else
                    {
                        if (this.GetStringValue(objectPropertyNode.NameNode) == propertyName)
                        {
                            objectProperty = objectPropertyNode;
                            return(true);
                        }
                    }
                }

                objectProperty = default(ObjectProperty);
                return(false);
            }
Beispiel #2
0
            /// <inheritdoc />
            public override bool TryGetObjectProperty(
                IJsonNavigatorNode objectNode,
                string propertyName,
                out ObjectProperty objectProperty)
            {
                _ = JsonBinaryNavigator.GetNodeOfType(
                    JsonNodeType.Object,
                    objectNode);

                Utf8Span utf8StringPropertyName = Utf8Span.TranscodeUtf16(propertyName);

                foreach (ObjectProperty objectPropertyNode in this.GetObjectProperties(objectNode))
                {
                    if (this.TryGetBufferedStringValue(objectPropertyNode.NameNode, out Utf8Memory bufferedUtf8StringValue))
                    {
                        // First try and see if we can avoid materializing the UTF16 string.
                        if (utf8StringPropertyName.Equals(bufferedUtf8StringValue.Span))
                        {
                            objectProperty = objectPropertyNode;
                            return(true);
                        }
                    }
                    else
                    {
                        if (this.GetStringValue(objectPropertyNode.NameNode) == propertyName)
                        {
                            objectProperty = objectPropertyNode;
                            return(true);
                        }
                    }
                }

                objectProperty = default;
                return(false);
            }
                /// <summary>
                /// Parses out a JSON object AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON object AST node</returns>
                private static ObjectNode ParseObjectNode(IJsonReader jsonTextReader)
                {
                    List <ObjectProperty> properties = new List <ObjectProperty>();

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

                    while (jsonTextReader.CurrentTokenType != JsonTokenType.EndObject)
                    {
                        ObjectProperty property = Parser.ParsePropertyNode(jsonTextReader);
                        properties.Add(property);
                    }

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

                    return(ObjectNode.Create(properties));
                }
Beispiel #4
0
                /// <summary>
                /// Parses out a JSON object AST node with a jsonTextReader.
                /// </summary>
                /// <param name="jsonTextReader">The reader to use as a lexer / tokenizer</param>
                /// <returns>JSON object AST node</returns>
                private static ObjectNode ParseObjectNode(IJsonReader jsonTextReader)
                {
                    List <ObjectProperty> properties = new List <ObjectProperty>();

                    if (!jsonTextReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedObjectStartToken))
                    {
                        throw new InvalidOperationException($"Failed to get {nameof(bufferedObjectStartToken)}.");
                    }

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

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

                    while (jsonTextReader.CurrentTokenType != JsonTokenType.EndObject)
                    {
                        ObjectProperty property = Parser.ParsePropertyNode(jsonTextReader);
                        properties.Add(property);
                    }

                    if (!jsonTextReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedObjectEndToken))
                    {
                        throw new InvalidOperationException($"Failed to get {nameof(bufferedObjectEndToken)}.");
                    }

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

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

                    ReadOnlyMemory <byte> bufferedRawObject = startObjectArraySegment.Array;

                    bufferedRawObject = bufferedRawObject.Slice(start: startObjectArraySegment.Offset, length: endObjectArraySegment.Offset - startObjectArraySegment.Offset + 1);

                    return(ObjectNode.Create(properties, bufferedRawObject));
                }
            /// <inheritdoc />
            public override bool TryGetObjectProperty(
                IJsonNavigatorNode objectNode,
                string propertyName,
                out ObjectProperty objectProperty)
            {
                ReadOnlyMemory <byte> buffer = JsonBinaryNavigator.GetNodeOfType(
                    JsonNodeType.Object,
                    objectNode);

                foreach (ObjectProperty objectPropertyNode in this.GetObjectProperties(objectNode))
                {
                    if (this.GetStringValue(objectPropertyNode.NameNode) == propertyName)
                    {
                        objectProperty = objectPropertyNode;
                        return(true);
                    }
                }

                objectProperty = default(ObjectProperty);
                return(false);
            }
Beispiel #6
0
 /// <inheritdoc />
 public abstract bool TryGetObjectProperty(
     IJsonNavigatorNode objectNode,
     string propertyName,
     out ObjectProperty objectProperty);
Beispiel #7
0
            /// <inheritdoc />
            public override bool TryGetObjectProperty(IJsonNavigatorNode node, string propertyName, out ObjectProperty objectProperty)
            {
                if (node is LazyNode lazyNode)
                {
                    node = lazyNode.Value;
                }

                if (!(node is ObjectNode objectNode))
                {
                    throw new ArgumentException($"{node} was not of type: {nameof(ObjectNode)}.");
                }

                Utf8Memory propertyNameAsUtf8 = Utf8Memory.Create(propertyName);

                foreach (ObjectProperty property in objectNode.Properties)
                {
                    if (!this.TryGetBufferedStringValue(property.NameNode, out Utf8Memory candidate))
                    {
                        throw new InvalidOperationException("Failed to get property name buffered value.");
                    }

                    if (propertyNameAsUtf8.Span == candidate.Span)
                    {
                        objectProperty = property;
                        return(true);
                    }
                }

                objectProperty = default;
                return(false);
            }
            /// <inheritdoc />
            public override bool TryGetObjectProperty(IJsonNavigatorNode objectNavigatorNode, string propertyName, out ObjectProperty objectProperty)
            {
                if (objectNavigatorNode == null)
                {
                    throw new ArgumentNullException("objectNavigatorNode");
                }

                if (propertyName == null)
                {
                    throw new ArgumentNullException("propertyName");
                }

                ObjectNode objectNode = objectNavigatorNode as ObjectNode;

                if (objectNode == null)
                {
                    throw new ArgumentException("objectNavigatorNode must actually be an array node");
                }

                objectProperty = default(ObjectProperty);
                IReadOnlyList <ObjectProperty> properties = ((ObjectNode)objectNode).Properties;

                foreach (ObjectProperty property in properties)
                {
                    if (this.GetStringValue(property.NameNode) == propertyName)
                    {
                        objectProperty = property;
                        return(true);
                    }
                }

                return(false);
            }
            /// <summary>
            /// Tries to get a object property from an object with a particular property name.
            /// </summary>
            /// <param name="objectNavigatorNode">The <see cref="ObjectProperty"/> of object node to get a property from.</param>
            /// <param name="propertyName">The name of the property to search for.</param>
            /// <param name="objectProperty">The <see cref="IJsonNavigatorNode"/> with the specified property name if it exists.</param>
            /// <returns><code>true</code> if the JsonNavigator successfully found the <see cref="ObjectProperty"/> with the specified property name; <code>false</code> otherwise.</returns>
            public override bool TryGetObjectProperty(IJsonNavigatorNode objectNavigatorNode, string propertyName, out ObjectProperty objectProperty)
            {
                foreach (ObjectProperty objectPropertyNode in this.GetObjectProperties(objectNavigatorNode))
                {
                    if (this.GetStringValue(objectPropertyNode.NameNode) == propertyName)
                    {
                        objectProperty = objectPropertyNode;
                        return(true);
                    }
                }

                objectProperty = default(ObjectProperty);
                return(false);
            }