Ejemplo n.º 1
0
        /// <summary>
        /// Get relative path from current JsonPath expression to child JsonPath expression
        /// </summary>
        /// <param name="path">JsonPath expression</param>
        /// <param name="childPath">Child JsonPath expression</param>
        /// <returns>Relative path from current JsonPath expression to child JsonPth expression, or null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="childPath"/> is null</exception>
        /// <remarks>
        /// If <paramref name="childPath"/> does not start with <paramref name="path"/>, or is equal to <paramref name="path"/>, null is returned
        /// </remarks>
        public static RelativeJsonPathExpression?GetRelativePathTo(this JsonPathExpression path, JsonPathExpression childPath)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (childPath is null)
            {
                throw new ArgumentNullException(nameof(childPath));
            }

            if (path.Elements.Count >= childPath.Elements.Count)
            {
                return(null);
            }
            if (!childPath.StartsWith(path))
            {
                return(null);
            }

            var elements = childPath.Elements
                           .Skip(path.Elements.Count)
                           .ToList();

            return(new RelativeJsonPathExpression(elements));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check if current JsonPath expression starts with another JsonPath expression but is not equal to it
        /// </summary>
        /// <param name="path">JsonPath expression</param>
        /// <param name="prefix">JsonPath expression that is supposed to be a prefix of <paramref name="path"/></param>
        /// <returns>True if <paramref name="path"/> starts with <paramref name="prefix"/></returns>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="prefix"/> is null</exception>
        public static bool StartsWith(this JsonPathExpression path, JsonPathExpression prefix)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (prefix is null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }

            if (prefix.Elements.Count >= path.Elements.Count)
            {
                return(false);
            }

            for (int i = 0; i < prefix.Elements.Count; ++i)
            {
                if (!path.Elements[i].Equals(prefix.Elements[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Tells if a set of JSON tree elements represented by current JsonPath expression contain JSON tree elements represented by another JsonPath expression
        /// </summary>
        /// <param name="path">JsonPath expression</param>
        /// <param name="other">JsonPath expression to check for matching</param>
        /// <returns>True if a set of JSON tree elements represented by current JsonPath expression contain JSON tree elements represented by <paramref name="other"/></returns>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="other"/> is null</exception>
        /// <remarks>Returns null if it's not possible to check if a set of JSON tree elements represented by the JsonPath expression contain JSON tree elements represented by another JsonPath expression</remarks>
        public static bool?Matches(this JsonPathExpression path, JsonPathExpression other)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (other is null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            return(path.Elements[0].Matches(path.Elements, 0, other.Elements, 0));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Convert JsonPath expression to <see cref="AbsoluteJsonPathExpression"/>
        /// </summary>
        /// <param name="path">JsonPath expression to convert</param>
        /// <returns><see cref="AbsoluteJsonPathExpression"/></returns>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null</exception>
        /// <remarks>
        /// <para>If <paramref name="path"/> is <see cref="AbsoluteJsonPathExpression"/>, <paramref name="path"/> is returned</para>
        /// <para>If <paramref name="path"/> is an absolute path, an absolute JsonPath expression with its elements is returned</para>
        /// <para>If <paramref name="path"/> is a relative path, an absolute JsonPath expression with its elements appended to root element is returned</para>
        /// </remarks>
        public static AbsoluteJsonPathExpression ToAbsolute(this JsonPathExpression path)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (path is AbsoluteJsonPathExpression absolutePath)
            {
                return(absolutePath);
            }

            var elements = path.IsAbsolute
                ? path.Elements
                : CollectionHelper.Concatenate(new JsonPathRootElement(), path.Elements);

            return(new AbsoluteJsonPathExpression(elements));
        }