public static string Build(JsonPathElement element) { var builder = new StringBuilder(); builder.AppendElement(element, false); return(builder.ToString()); }
public void Constructor_AbsolutePath_Throws() { var elements = new JsonPathElement[] { new JsonPathRootElement(), new JsonPathPropertyElement("a") }; Action action = () => new RelativeJsonPathExpression(elements); action.Should().Throw <ArgumentException>(); }
public void Constructor_RelativePath_Succeeds() { var elements = new JsonPathElement[] { new JsonPathPropertyElement("a") }; var actual = new RelativeJsonPathExpression(elements); actual.Elements.Should().BeEquivalentTo(elements); }
public void Constructor_RootIsNotFirstElement_Throws() { var elements = new JsonPathElement[] { new JsonPathPropertyElement("a"), new JsonPathRootElement() }; Action action = () => new JsonPathExpression(elements); action.Should().Throw <ArgumentException>(); }
private void Parse_Worker(string expression, bool expectedRecursiveDescent, JsonPathElement expectedElement) { var expected = expectedRecursiveDescent ? new JsonPathRecursiveDescentElement(expectedElement) : expectedElement; var actual = JsonPathExpressionStringParser.Parse(expression); actual.Should().BeEquivalentTo(expected); }
private void AddElement(JsonPathElement element) { if (_isRecursiveDescent) { element = new JsonPathRecursiveDescentElement(element); } _elements.Add(element); _isRecursiveDescent = false; }
private static void AppendElement(this StringBuilder builder, JsonPathElement element, bool useDot) { switch (element) { case JsonPathRootElement rootElement: builder.AppendRoot(); break; case JsonPathRecursiveDescentElement recursiveDescentElement: builder.AppendRecursiveDescent(recursiveDescentElement); break; case JsonPathPropertyElement propertyElement: builder.AppendProperty(propertyElement, useDot); break; case JsonPathAnyPropertyElement anyPropertyElement: builder.AppendAnyProperty(useDot); break; case JsonPathPropertyListElement propertyListElement: builder.AppendPropertyList(propertyListElement); break; case JsonPathArrayIndexElement arrayIndexElement: builder.AppendArrayIndex(arrayIndexElement); break; case JsonPathAnyArrayIndexElement anyArrayIndexElement: builder.AppendAnyArrayIndex(); break; case JsonPathArrayIndexListElement arrayIndexListElement: builder.AppendArrayIndexList(arrayIndexListElement); break; case JsonPathArraySliceElement arraySliceElement: builder.AppendArraySlice(arraySliceElement); break; case JsonPathExpressionElement expressionElement: builder.AppendExpression(expressionElement); break; case JsonPathFilterExpressionElement filterExpressionElement: builder.AppendFilterExpression(filterExpressionElement); break; default: throw new ArgumentOutOfRangeException(nameof(element), element, $"Unknown element type: {element.GetType()}"); } }
private void Build_Worker(JsonPathElement element, bool recursiveDescent, string expected) { var elements = new List <JsonPathElement> { recursiveDescent ? new JsonPathRecursiveDescentElement(element) : element }; string actual = JsonPathExpressionStringBuilder.Build(elements); actual.Should().Be(expected); }
public void Create_ReturnsObjectOfTheSameType() { var path = new RelativeJsonPathExpression(new JsonPathElement[] { new JsonPathPropertyElement("a") }); var elements = new JsonPathElement[] { new JsonPathPropertyElement("a"), new JsonPathArrayIndexElement(42) }; var actual = path.Create(elements); actual.Elements.Should().BeEquivalentTo(elements); actual.GetType().Should().Be <RelativeJsonPathExpression>(); }
private static bool IsStrict(JsonPathElement element) { switch (element.Type) { case JsonPathElementType.Root: case JsonPathElementType.Property: case JsonPathElementType.ArrayIndex: return(true); case JsonPathElementType.AnyProperty: case JsonPathElementType.PropertyList: case JsonPathElementType.AnyArrayIndex: case JsonPathElementType.ArrayIndexList: case JsonPathElementType.ArraySlice: case JsonPathElementType.Expression: case JsonPathElementType.FilterExpression: return(false); default: throw new ArgumentOutOfRangeException(nameof(element), element, $"Unknown JSON path element type: {element.GetType()}"); } }
private IReadOnlyDictionary <JsonPathElement, JsonPathExpressionMatchingNode <TJsonPathExpression> > GetNonStrictNodes(JsonPathElement element) { switch (element.Type) { case JsonPathElementType.Root: return(Empty); case JsonPathElementType.Property: case JsonPathElementType.AnyProperty: case JsonPathElementType.PropertyList: return(_properties); case JsonPathElementType.ArrayIndex: case JsonPathElementType.AnyArrayIndex: case JsonPathElementType.ArrayIndexList: case JsonPathElementType.ArraySlice: case JsonPathElementType.Expression: case JsonPathElementType.FilterExpression: return(_indexes); case JsonPathElementType.RecursiveDescent: throw new ArgumentOutOfRangeException(nameof(element), element, "This should not happen: recursive descent is not allowed here"); default: throw new ArgumentOutOfRangeException(nameof(element), element, $"Unknown JSON path element type: {element.GetType()}"); } }
/// <summary> /// Replace last JsonPath element in current JsonPath expression with another JsonPath element /// </summary> /// <typeparam name="TJsonPathExpression">JsonPath expression type</typeparam> /// <param name="path">JsonPath expression</param> /// <param name="element">New JsonPath element</param> /// <returns>JsonPath expression with replaced last JsonPath element</returns> /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="element"/> is null</exception> public static TJsonPathExpression ReplaceLastWith <TJsonPathExpression>(this TJsonPathExpression path, JsonPathElement element) where TJsonPathExpression : JsonPathExpression { if (path is null) { throw new ArgumentNullException(nameof(path)); } if (element is null) { throw new ArgumentNullException(nameof(element)); } var elements = new List <JsonPathElement>(path.Elements) { [path.Elements.Count - 1] = element }; return((TJsonPathExpression)path.Create(elements)); }