コード例 #1
0
        /// <summary>
        /// Selects a <see cref="JsonElement"/> using a JSONPath expression. Selects the token that matches the object path.
        /// </summary>
        /// <param name="path">
        /// A <see cref="String"/> that contains a JSONPath expression.
        /// </param>
        /// <param name="errorWhenNoMatch">A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression.</param>
        /// <returns>A <see cref="JsonDocument"/>.</returns>
        public static JsonElement?SelectElement(this JsonDocument src, string path, bool errorWhenNoMatch)
        {
            var         p  = new JsonDocumentPath(path);
            JsonElement?el = null;

            foreach (JsonElement t in p.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch))
            {
                if (el != null)
                {
                    throw new JsonException("Path returned multiple tokens.");
                }
                el = t;
            }
            return(el);
        }
コード例 #2
0
        private IEnumerable <JsonElement?> GetResult(JsonElement root, JsonElement t, object?o)
        {
            if (o is JsonElement resultToken)
            {
                return(new JsonElement?[1] {
                    resultToken
                });
            }

            if (o is List <PathFilter> pathFilters)
            {
                return(JsonDocumentPath.Evaluate(pathFilters, root, t, false));
            }

            return(Enumerable.Empty <JsonElement?>());
        }
コード例 #3
0
        /// <summary>
        /// Selects a collection of elements using a JSONPath expression.
        /// </summary>
        /// <param name="path">
        /// A <see cref="String"/> that contains a JSONPath expression.
        /// </param>
        /// <param name="errorWhenNoMatch">A flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JsonDocument"/> that contains the selected elements.</returns>
        public static IEnumerable <JsonElement?> SelectElements(this JsonDocument src, string path, bool errorWhenNoMatch)
        {
            var parser = new JsonDocumentPath(path);

            return(parser.Evaluate(src.RootElement, src.RootElement, errorWhenNoMatch));
        }