using System.Collections; using System.Collections.Generic; using System.Linq.Dynamic.Core.SystemTextJson.Config; using System.Linq.Dynamic.Core.SystemTextJson.Extensions; using System.Linq.Dynamic.Core.SystemTextJson.Utils; using System.Linq.Dynamic.Core.Validation; using System.Text.Json; using JetBrains.Annotations; namespace System.Linq.Dynamic.Core.SystemTextJson; /// /// Extension methods for . /// public static class SystemTextJsonExtensions { #region Aggregate /// /// Dynamically runs an aggregate function on the >. /// /// The > data source. /// The name of the function to run. Can be Sum, Average, Min or Max. /// The name of the property to aggregate over. /// The value of the aggregate function run over the specified property. public static object Aggregate(this JsonDocument source, string function, string member) { Check.NotNull(source); Check.NotEmpty(function); Check.NotEmpty(member); var queryable = ToQueryable(source); return queryable.Aggregate(function, member); } #endregion Aggregate #region All /// /// Determines whether all the elements of a sequence satisfy a condition. /// /// The source /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false. public static bool All(this JsonDocument source, string predicate, params object?[] args) { return All(source, SystemTextJsonParsingConfig.Default, predicate, args); } /// /// Determines whether all the elements of a sequence satisfy a condition. /// /// The source /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false. public static bool All(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.All(config, predicate, args); } #endregion All #region Any /// /// Determines whether a sequence contains any elements. /// /// The source /// true if the source sequence contains any elements; otherwise, false. public static bool Any(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Any(); } /// /// Determines whether a sequence contains any elements. /// /// The source /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// true if the source sequence contains any elements; otherwise, false. public static bool Any(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Any(config, predicate, args); } /// /// Determines whether a sequence contains any elements. /// /// The source /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// true if the source sequence contains any elements; otherwise, false. public static bool Any(this JsonDocument source, string predicate, params object?[] args) { return Any(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Any #region Average /// /// Computes the average of a sequence of numeric values. /// /// The source /// The average of the values in the sequence. public static double Average(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Average(); } /// /// Computes the average of a sequence of numeric values. /// /// The source /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// The average of the values in the sequence. public static double Average(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); Check.NotEmpty(predicate); var queryable = ToQueryable(source, config); return queryable.Average(config, predicate, args); } /// /// Computes the average of a sequence of numeric values. /// /// The source /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// The average of the values in the sequence. public static double Average(this JsonDocument source, string predicate, params object?[] args) { return Average(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Average #region Cast /// /// Converts the elements of an to the specified type. /// /// The that contains the elements to be converted. /// The type to convert the elements of source to. /// An that contains each element of the source sequence converted to the specified type. public static IQueryable Cast(this JsonDocument source, Type type) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Cast(type); } /// /// Converts the elements of an to the specified type. /// /// The that contains the elements to be converted. /// The . /// The type to convert the elements of source to. /// An that contains each element of the source sequence converted to the specified type. public static IQueryable Cast(this JsonDocument source, SystemTextJsonParsingConfig config, string typeName) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Cast(typeName); } /// /// Converts the elements of an to the specified type. /// /// The that contains the elements to be converted. /// The type to convert the elements of source to. /// An that contains each element of the source sequence converted to the specified type. public static IQueryable Cast(this JsonDocument source, string typeName) { return Cast(source, SystemTextJsonParsingConfig.Default, typeName); } #endregion Cast #region Count /// /// Returns the number of elements in a sequence. /// /// The that contains the elements to be counted. /// The number of elements in the input sequence. public static int Count(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Count(); } /// /// Returns the number of elements in a sequence. /// /// The that contains the elements to be counted. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The number of elements in the specified sequence that satisfies a condition. public static int Count(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Count(config, predicate, args); } /// /// Returns the number of elements in a sequence. /// /// The that contains the elements to be counted. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The number of elements in the specified sequence that satisfies a condition. public static int Count(this JsonDocument source, string predicate, params object?[] args) { return Count(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Count #region DefaultIfEmpty /// /// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty. /// /// The to return a default value for if empty. /// An that contains default if source is empty; otherwise, source. public static JsonDocument DefaultIfEmpty(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(queryable.DefaultIfEmpty); } /// /// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty. /// /// The to return a default value for if empty. /// The value to return if the sequence is empty. /// An that contains defaultValue if source is empty; otherwise, source. public static JsonDocument DefaultIfEmpty(this JsonDocument source, object? defaultValue) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.DefaultIfEmpty(defaultValue)); } #endregion #region Distinct /// /// Returns distinct elements from a sequence by using the default equality comparer to compare values. /// /// The sequence to remove duplicate elements from. /// An that contains distinct elements from the source sequence. public static JsonDocument Distinct(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(queryable.Distinct); } #endregion Distinct #region First /// /// Returns the first element of a sequence. /// /// The to return the first element of. /// The first element in source. public static JsonElement First(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.First()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the first element of a sequence that satisfies a specified condition. /// /// The to return the first element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement First(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.First(config, predicate, args)) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the first element of a sequence that satisfies a specified condition. /// /// The to return the first element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement First(this JsonDocument source, string predicate, params object?[] args) { return First(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion First #region FirstOrDefault /// /// Returns the first element of a sequence, or a default value if the sequence contains no elements. /// /// The to return the first element of. /// default if source is empty; otherwise, the first element in source. public static JsonElement? FirstOrDefault(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.FirstOrDefault()); } /// /// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found. /// /// The to return the first element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// default if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. public static JsonElement? FirstOrDefault(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.FirstOrDefault(predicate, args)); } /// /// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found. /// /// The to return the first element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// default if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. public static JsonElement? FirstOrDefault(this JsonDocument source, string predicate, params object?[] args) { return FirstOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion FirstOrDefault #region GroupBy /// /// Groups the elements of a sequence according to a specified key string function /// and creates a result value from each group and its key. /// /// A whose elements to group. /// A string expression to specify the key for each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A where each element represents a projection over a group and its key. [PublicAPI] public static JsonDocument GroupBy(this JsonDocument source, string keySelector, params object[]? args) { return GroupBy(source, SystemTextJsonParsingConfig.Default, keySelector, args); } /// /// Groups the elements of a sequence according to a specified key string function /// and creates a result value from each group and its key. /// /// A whose elements to group. /// The . /// A string expression to specify the key for each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A where each element represents a projection over a group and its key. [PublicAPI] public static JsonDocument GroupBy(this JsonDocument source, SystemTextJsonParsingConfig config, string keySelector, params object[]? args) { Check.NotNull(source); Check.NotNull(config); Check.NotNullOrEmpty(keySelector); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.GroupBy(config, keySelector, args)); } #endregion #region Last /// /// Returns the last element of a sequence. /// /// The to return the last element of. /// The last element in source. public static JsonElement Last(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.Last()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the last element of a sequence that satisfies a specified condition. /// /// The to return the last element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement Last(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.Last(predicate, args)) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the last element of a sequence that satisfies a specified condition. /// /// The to return the last element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement Last(this JsonDocument source, string predicate, params object?[] args) { return Last(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Last #region LastOrDefault /// /// Returns the last element of a sequence, or a default value if the sequence contains no elements. /// /// The to return the last element of. /// default if source is empty; otherwise, the last element in source. public static JsonElement? LastOrDefault(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.LastOrDefault()); } /// /// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements. /// /// The to return the last element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement? LastOrDefault(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.LastOrDefault(config, predicate, args)); } /// /// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements. /// /// The to return the last element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement? LastOrDefault(this JsonDocument source, string predicate, params object?[] args) { return LastOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion LastOrDefault #region Max /// /// Computes the max element of a sequence. /// /// A sequence of values to calculate find the max for. /// The max element in the sequence. public static object Max(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Max(); } /// /// Computes the max element of a sequence. /// /// A sequence of values to calculate find the max for. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. public static object Max(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Max(config, predicate, args); } /// /// Computes the max element of a sequence. /// /// A sequence of values to calculate find the max for. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The max element in the sequence. public static object Max(this JsonDocument source, string predicate, params object?[] args) { return Max(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Max #region Min /// /// Computes the min element of a sequence. /// /// A sequence of values to calculate find the min for. /// The min element in the sequence. public static object Min(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Min(); } /// /// Computes the min element of a sequence. /// /// A sequence of values to calculate find the min for. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. public static object Min(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Min(config, predicate, args); } /// /// Computes the min element of a sequence. /// /// A sequence of values to calculate find the min for. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The min element in the sequence. public static object Min(this JsonDocument source, string predicate, params object?[] args) { return Min(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Min #region OrderBy /// /// Sorts the elements of a sequence in ascending or descending order according to a key. /// /// A sequence of values to order. /// The . /// An expression string to indicate values to order by. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A whose elements are sorted according to the specified . public static JsonDocument OrderBy(this JsonDocument source, SystemTextJsonParsingConfig config, string ordering, params object?[] args) { Check.NotNull(source); Check.NotNull(source); Check.NotNullOrEmpty(ordering); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.OrderBy(config, ordering, args)); } /// /// Sorts the elements of a sequence in ascending or descending order according to a key. /// /// A sequence of values to order. /// An expression string to indicate values to order by. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A whose elements are sorted according to the specified . public static JsonDocument OrderBy(this JsonDocument source, string ordering, params object?[] args) { return OrderBy(source, SystemTextJsonParsingConfig.Default, ordering, args); } /// /// Sorts the elements of a sequence in ascending or descending order according to a key. /// /// A sequence of values to order. /// The . /// An expression string to indicate values to order by. /// The comparer to use. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A whose elements are sorted according to the specified . public static JsonDocument OrderBy(this JsonDocument source, SystemTextJsonParsingConfig config, string ordering, IComparer comparer, params object?[] args) { var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.OrderBy(config, ordering, comparer, args)); } /// /// Sorts the elements of a sequence in ascending or descending order according to a key. /// /// A sequence of values to order. /// An expression string to indicate values to order by. /// The comparer to use. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// A whose elements are sorted according to the specified . public static JsonDocument OrderBy(this JsonDocument source, string ordering, IComparer comparer, params object?[] args) { return OrderBy(source, SystemTextJsonParsingConfig.Default, ordering, comparer, args); } #endregion OrderBy #region Page/PageResult /// /// Returns the elements as paged. /// /// The source /// The page to return. /// The number of elements per page. /// A that contains the paged elements. public static JsonDocument Page(this JsonDocument source, int page, int pageSize) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.Page(page, pageSize)); } /// /// Returns the elements as paged and include the CurrentPage, PageCount, PageSize and RowCount. /// /// The source /// The page to return. /// The number of elements per page. /// If this optional parameter has been defined, this value is used as the RowCount instead of executing a Linq `Count()`. /// PagedResult public static PagedResult PageResult(this JsonDocument source, int page, int pageSize, int? rowCount = null) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.PageResult(page, pageSize, rowCount); } #endregion Page/PageResult #region Reverse /// /// Inverts the order of the elements in a sequence. /// /// The source /// A whose elements correspond to those of the input sequence in reverse order. public static JsonDocument Reverse(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.Reverse()); } #endregion Reverse #region Select /// /// Projects each element of a sequence into a new form. /// /// The source /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// An whose elements are the result of invoking a projection string on each element of source. public static JsonDocument Select(this JsonDocument source, string selector, params object?[] args) { return Select(source, SystemTextJsonParsingConfig.Default, selector, args); } /// /// Projects each element of a sequence into a new form. /// /// The source /// The . /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// An whose elements are the result of invoking a projection string on each element of source. public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args) { Check.NotNull(source); Check.NotNull(config); Check.NotNullOrEmpty(selector); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.Select(config, selector, args)); } /// /// Projects each element of a sequence into a new class of type TResult. /// Details see http://solutionizing.net/category/linq/ /// /// The source /// The . /// The result type. /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// An whose elements are the result of invoking a projection string on each element of source. public static JsonDocument Select(this JsonDocument source, SystemTextJsonParsingConfig config, Type resultType, string selector, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.Select(config, resultType, selector, args)); } /// /// Projects each element of a sequence into a new class of type TResult. /// Details see http://solutionizing.net/category/linq/ /// /// The source /// The result type. /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// An whose elements are the result of invoking a projection string on each element of source. public static JsonDocument Select(this JsonDocument source, Type resultType, string selector, params object?[] args) { return Select(source, SystemTextJsonParsingConfig.Default, resultType, selector, args); } #endregion Select #region SelectMany /// /// Projects each element of a sequence to an and combines the resulting sequences into one sequence. /// /// The source /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// A whose elements are the result of invoking a one-to-many projection function on each element of the input sequence. public static JsonDocument SelectMany(this JsonDocument source, string selector, params object?[] args) { return SelectMany(source, SystemTextJsonParsingConfig.Default, selector, args); } /// /// Projects each element of a sequence to an and combines the resulting sequences into one sequence. /// /// The source /// The . /// A projection string expression to apply to each element. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// A whose elements are the result of invoking a one-to-many projection function on each element of the input sequence. public static JsonDocument SelectMany(this JsonDocument source, SystemTextJsonParsingConfig config, string selector, params object?[] args) { Check.NotNull(source); Check.NotNull(config); Check.NotNullOrEmpty(selector); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.SelectMany(config, selector, args)); } #endregion SelectMany #region Single /// /// Returns the only element of a sequence, and throws an exception if there /// is not exactly one element in the sequence. /// /// A to return the single element of. /// The single element of the input sequence. public static JsonElement Single(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.Single()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if there /// is not exactly one element in the sequence. /// /// The to return the last element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement Single(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.Single(predicate, args)) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements); } /// /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if there /// is not exactly one element in the sequence. /// /// The to return the last element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement Single(this JsonDocument source, string predicate, params object?[] args) { return Single(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Single #region SingleOrDefault /// /// Returns the only element of a sequence, or a default value if the sequence /// is empty; this method throws an exception if there is more than one element /// in the sequence. /// /// A to return the single element of. /// The single element of the input sequence, or default if the sequence contains no elements. public static JsonElement? SingleOrDefault(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonElement(queryable.SingleOrDefault()); } /// /// Returns the only element of a sequence that satisfies a specified condition or a default value if the sequence /// is empty; and throws an exception if there is not exactly one element in the sequence. /// /// The to return the last element of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement? SingleOrDefault(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonElement(queryable.SingleOrDefault(predicate, args)); } /// /// Returns the only element of a sequence that satisfies a specified condition or a default value if the sequence /// is empty; and throws an exception if there is not exactly one element in the sequence. /// /// The to return the last element of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The first element in source that passes the test in predicate. public static JsonElement? SingleOrDefault(this JsonDocument source, string predicate, params object?[] args) { return SingleOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion SingleOrDefault #region Skip /// /// Bypasses a specified number of elements in a sequence and then returns the remaining elements. /// /// A to return elements from. /// The number of elements to skip before returning the remaining elements. /// A that contains elements that occur after the specified index in the input sequence. public static JsonDocument Skip(this JsonDocument source, int count) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.Skip(count)); } #endregion Skip #region SkipWhile /// /// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. /// /// A to return elements from. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// An that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate. public static JsonDocument SkipWhile(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.SkipWhile(predicate, args)); } /// /// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. /// /// A to return elements from. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// An that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate. public static JsonDocument SkipWhile(this JsonDocument source, string predicate, params object?[] args) { return SkipWhile(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion SkipWhile #region Sum /// /// Computes the sum of a sequence of numeric values. /// /// A sequence of numeric values to calculate the sum of. /// The sum of the values in the sequence. public static object Sum(this JsonDocument source) { Check.NotNull(source); var queryable = ToQueryable(source); return queryable.Sum(); } /// /// Computes the sum of a sequence of numeric values. /// /// A sequence of numeric values to calculate the sum of. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The sum of the values in the sequence. public static object Sum(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return queryable.Sum(predicate, args); } /// /// Computes the sum of a sequence of numeric values. /// /// A sequence of numeric values to calculate the sum of. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// The sum of the values in the sequence. public static object Sum(this JsonDocument source, string predicate, params object?[] args) { return Sum(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion Sum #region Take /// /// Returns a specified number of contiguous elements from the start of a sequence. /// /// The sequence to return elements from. /// The number of elements to return. /// A that contains the specified number of elements from the start of source. public static JsonDocument Take(this JsonDocument source, int count) { Check.NotNull(source); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.Take(count)); } #endregion Take #region TakeWhile /// /// Returns elements from a sequence as long as a specified condition is true. /// /// The sequence to return elements from. /// The . /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// An that contains elements from the input sequence occurring before the element at which the test specified by predicate no longer passes. public static JsonDocument TakeWhile(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source); return ToJsonDocumentArray(() => queryable.TakeWhile(predicate, args)); } /// /// Returns elements from a sequence as long as a specified condition is true. /// /// The sequence to return elements from. /// A function to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings. /// An that contains elements from the input sequence occurring before the element at which the test specified by predicate no longer passes. public static JsonDocument TakeWhile(this JsonDocument source, string predicate, params object?[] args) { return TakeWhile(source, SystemTextJsonParsingConfig.Default, predicate, args); } #endregion TakeWhile #region Where /// /// Filters a sequence of values based on a predicate. /// /// The source /// An expression string to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// A that contains elements from the input sequence that satisfy the condition specified by predicate. public static JsonDocument Where(this JsonDocument source, string predicate, params object?[] args) { return Where(source, SystemTextJsonParsingConfig.Default, predicate, args); } /// /// Filters a sequence of values based on a predicate. /// /// The source /// The . /// An expression string to test each element for a condition. /// An object array that contains zero or more objects to insert into the predicate as parameters. /// A that contains elements from the input sequence that satisfy the condition specified by predicate. public static JsonDocument Where(this JsonDocument source, SystemTextJsonParsingConfig config, string predicate, params object?[] args) { Check.NotNull(source); Check.NotNull(config); var queryable = ToQueryable(source, config); return ToJsonDocumentArray(() => queryable.Where(config, predicate, args)); } #endregion Where #region Private Methods private static JsonElement? ToJsonElement(object? value) { if (value == null) { return null; } if (value is JsonElement jsonElement) { return jsonElement; } return JsonElementUtils.FromObject(value); } private static JsonDocument ToJsonDocumentArray(Func func) { var array = new List(); foreach (var dynamicElement in func()) { var element = dynamicElement switch { IGrouping grouping => ToJsonElement(new { Key = ToJsonElement(grouping.Key), Values = ToJsonDocumentArray(grouping.AsQueryable).RootElement }), _ => ToJsonElement(dynamicElement) }; array.Add(element); } return JsonDocumentUtils.FromObject(array); } // ReSharper disable once UnusedParameter.Local private static IQueryable ToQueryable(JsonDocument source, SystemTextJsonParsingConfig? config = null) { var array = source.RootElement; if (array.ValueKind != JsonValueKind.Array) { throw new NotSupportedException("The source is not a JSON array."); } return JsonDocumentExtensions.ToDynamicJsonClassArray(array).AsQueryable(); } #endregion }