/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathArrayIndexElement arrayIndexElement: return(null); case JsonPathArrayIndexListElement arrayIndexListElement: return(arrayIndexListElement.Indexes.Count == 1 ? default(bool?) : false); case JsonPathArraySliceElement arraySliceElement: return(arraySliceElement.IndexCount == 1 ? default(bool?) : false); case JsonPathExpressionElement expressionElement: return(Equals(expressionElement)); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathArrayIndexElement arrayIndexElement: return(ContainsIndex(arrayIndexElement.Index)); case JsonPathAnyArrayIndexElement anyArrayIndexElement: return(ContainsAllIndexes); case JsonPathArrayIndexListElement arrayIndexListElement: return(ContainsIndexes(arrayIndexListElement.Indexes)); case JsonPathArraySliceElement arraySliceElement: return(Matches(arraySliceElement)); case JsonPathExpressionElement expressionElement: return(null); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathArrayIndexElement arrayIndexElement: return(_indexes.Contains(arrayIndexElement.Index)); case JsonPathArrayIndexListElement arrayIndexListElement: return(arrayIndexListElement.Indexes.All(x => _indexes.Contains(x))); case JsonPathArraySliceElement arraySliceElement: if (arraySliceElement.ContainsAllIndexes) { return(Indexes.Count == int.MaxValue); } return(arraySliceElement.GetIndexes()?.All(x => _indexes.Contains(x))); default: return(false); } }
/// <summary> /// Check if JsonPath element has type in a passed range or element is a recursive descent applied to JsonPath element with type in a passed range /// </summary> /// <param name="element">JsonPath element</param> /// <param name="firstType">First JsonPath element type in the range</param> /// <param name="lastType">Last JsonPath element type in the range</param> /// <returns>True if JsonPath element has type in a passed range or element is a recursive descent applied to element with JsonPath type in a passed range</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="lastType"/> is less than <paramref name="firstType"/></exception> public static bool IsOfTypeInRange(this JsonPathElement element, JsonPathElementType firstType, JsonPathElementType lastType) { if (element is null) { throw new ArgumentNullException(nameof(element)); } if (lastType < firstType) { throw new ArgumentOutOfRangeException(nameof(lastType), lastType, "Last type is less than first type"); } while (true) { if (element.Type >= firstType && element.Type <= lastType) { return(true); } if (!(element is JsonPathRecursiveDescentElement recursiveDescentElement)) { return(false); } element = recursiveDescentElement.AppliedToElement; } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathArrayIndexElement arrayIndexElement: case JsonPathAnyArrayIndexElement anyArrayIndexElement: case JsonPathArrayIndexListElement arrayIndexListElement: case JsonPathArraySliceElement arraySliceElement: case JsonPathExpressionElement expressionElement: return(null); case JsonPathFilterExpressionElement filterExpressionElement: if (Equals(filterExpressionElement)) { return(true); } return(null); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } return(Equals(element)); }
/// <summary> /// Get non-recursive-descent JsonPath element for passed JsonPath element /// </summary> /// <param name="element">JsonPath element</param> /// <returns>Non-recursive-descent JsonPath element</returns> /// <remarks> /// If <paramref name="element"/> is <see cref="JsonPathRecursiveDescentElement"/> <see cref="JsonPathRecursiveDescentElement.AppliedToElement"/> is returned /// </remarks> public static JsonPathElement GetUnderlyingElement(this JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } return(element.Type == JsonPathElementType.RecursiveDescent ? ((JsonPathRecursiveDescentElement)element).AppliedToElement : element); }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } if (element is JsonPathRecursiveDescentElement recursiveDescentElement) { return(AppliedToElement.Matches(recursiveDescentElement.AppliedToElement)); } return(null); }
/// <summary> /// Create <see cref="JsonPathRecursiveDescentElement"/> instance /// </summary> /// <param name="appliedToElement">JsonPath element to which recursive descent is applied</param> /// <exception cref="ArgumentException"><paramref name="appliedToElement"/> is <see cref="JsonPathRootElement"/> or <see cref="JsonPathRecursiveDescentElement"/></exception> public JsonPathRecursiveDescentElement(JsonPathElement appliedToElement) { if (appliedToElement is null) { throw new ArgumentNullException(nameof(appliedToElement)); } if (appliedToElement.Type == JsonPathElementType.Root) { throw new ArgumentException("Recursive descent must not be applied to root element"); } if (appliedToElement.Type == JsonPathElementType.RecursiveDescent) { throw new ArgumentException("Recursive descent must not be applied to recursive descent"); } AppliedToElement = appliedToElement; }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathPropertyElement propertyElement: return(Equals(propertyElement)); case JsonPathPropertyListElement propertyListElement: return(propertyListElement.Names.Count == 1 && propertyListElement.Names.First() == Name); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathPropertyElement propertyElement: return(_names.Contains(propertyElement.Name)); case JsonPathPropertyListElement propertyListElement: return(propertyListElement.Names.All(x => _names.Contains(x))); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element.Type) { case JsonPathElementType.Property: case JsonPathElementType.AnyProperty: case JsonPathElementType.PropertyList: case JsonPathElementType.FilterExpression: return(true); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element.Type) { case JsonPathElementType.AnyArrayIndex: case JsonPathElementType.ArrayIndex: case JsonPathElementType.ArrayIndexList: case JsonPathElementType.ArraySlice: case JsonPathElementType.Expression: return(true); default: return(false); } }
/// <inheritdoc /> public override bool?Matches(JsonPathElement element) { if (element is null) { throw new ArgumentNullException(nameof(element)); } switch (element) { case JsonPathArrayIndexElement arrayIndexElement: return(arrayIndexElement.Index == Index); case JsonPathArrayIndexListElement arrayIndexListElement: return(arrayIndexListElement.Indexes.Count == 1 && arrayIndexListElement.Indexes.First() == Index); case JsonPathArraySliceElement arraySliceElement: return(arraySliceElement.GetIndexes()?.All(x => x == Index)); } return(Equals(element)); }
/// <summary> /// Check if JsonPath element has same type as passed or element is a recursive descent applied to JsonPath element with same type as passed /// </summary> /// <param name="element">JsonPath element</param> /// <param name="type">JsonPath element type to check</param> /// <returns>True if JsonPath element has same type as passed or element is a recursive descent applied to JsonPath element with same type as passed</returns> public static bool IsOfType(this JsonPathElement element, JsonPathElementType type) { if (element is null) { throw new ArgumentNullException(nameof(element)); } while (true) { if (element.Type == type) { return(true); } if (!(element is JsonPathRecursiveDescentElement recursiveDescentElement)) { return(false); } element = recursiveDescentElement.AppliedToElement; } }
/// <inheritdoc /> public override bool Equals(JsonPathElement other) { return(Equals((object)other)); }