-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathSystemTextJsonExtensions.cs
More file actions
1112 lines (989 loc) · 57 KB
/
SystemTextJsonExtensions.cs
File metadata and controls
1112 lines (989 loc) · 57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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;
/// <summary>
/// Extension methods for <see cref="JsonDocument"/>.
/// </summary>
public static class SystemTextJsonExtensions
{
#region Aggregate
/// <summary>
/// Dynamically runs an aggregate function on the <see cref="JsonDocument"/>>.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/>> data source.</param>
/// <param name="function">The name of the function to run. Can be Sum, Average, Min or Max.</param>
/// <param name="member">The name of the property to aggregate over.</param>
/// <returns>The value of the aggregate function run over the specified property.</returns>
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
/// <summary>
/// Determines whether all the elements of a sequence satisfy a condition.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.</returns>
public static bool All(this JsonDocument source, string predicate, params object?[] args)
{
return All(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
/// <summary>
/// Determines whether all the elements of a sequence satisfy a condition.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.</returns>
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
/// <summary>
/// Determines whether a sequence contains any elements.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
public static bool Any(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Any();
}
/// <summary>
/// Determines whether a sequence contains any elements.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
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);
}
/// <summary>
/// Determines whether a sequence contains any elements.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
public static bool Any(this JsonDocument source, string predicate, params object?[] args)
{
return Any(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Any
#region Average
/// <summary>
/// Computes the average of a sequence of numeric values.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <returns>The average of the values in the sequence.</returns>
public static double Average(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Average();
}
/// <summary>
/// Computes the average of a sequence of numeric values.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>The average of the values in the sequence.</returns>
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);
}
/// <summary>
/// Computes the average of a sequence of numeric values.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>The average of the values in the sequence.</returns>
public static double Average(this JsonDocument source, string predicate, params object?[] args)
{
return Average(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Average
#region Cast
/// <summary>
/// Converts the elements of an <see cref="JsonDocument"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be converted.</param>
/// <param name="type">The type to convert the elements of source to.</param>
/// <returns>An <see cref="IQueryable"/> that contains each element of the source sequence converted to the specified type.</returns>
public static IQueryable Cast(this JsonDocument source, Type type)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Cast(type);
}
/// <summary>
/// Converts the elements of an <see cref="JsonDocument"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be converted.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="typeName">The type to convert the elements of source to.</param>
/// <returns>An <see cref="JsonDocument"/> that contains each element of the source sequence converted to the specified type.</returns>
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);
}
/// <summary>
/// Converts the elements of an <see cref="JsonDocument"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be converted.</param>
/// <param name="typeName">The type to convert the elements of source to.</param>
/// <returns>An <see cref="IQueryable"/> that contains each element of the source sequence converted to the specified type.</returns>
public static IQueryable Cast(this JsonDocument source, string typeName)
{
return Cast(source, SystemTextJsonParsingConfig.Default, typeName);
}
#endregion Cast
#region Count
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</param>
/// <returns>The number of elements in the input sequence.</returns>
public static int Count(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Count();
}
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The number of elements in the specified sequence that satisfies a condition.</returns>
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);
}
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> that contains the elements to be counted.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The number of elements in the specified sequence that satisfies a condition.</returns>
public static int Count(this JsonDocument source, string predicate, params object?[] args)
{
return Count(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Count
#region DefaultIfEmpty
/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return a default value for if empty.</param>
/// <returns>An <see cref="JsonDocument"/> that contains default if source is empty; otherwise, source.</returns>
public static JsonDocument DefaultIfEmpty(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonDocumentArray(queryable.DefaultIfEmpty);
}
/// <summary>
/// Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return a default value for if empty.</param>
/// <param name="defaultValue">The value to return if the sequence is empty.</param>
/// <returns>An <see cref="JsonDocument"/> that contains defaultValue if source is empty; otherwise, source.</returns>
public static JsonDocument DefaultIfEmpty(this JsonDocument source, object? defaultValue)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.DefaultIfEmpty(defaultValue));
}
#endregion
#region Distinct
/// <summary>
/// Returns distinct elements from a sequence by using the default equality comparer to compare values.
/// </summary>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <returns>An <see cref="JsonDocument"/> that contains distinct elements from the source sequence.</returns>
public static JsonDocument Distinct(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonDocumentArray(queryable.Distinct);
}
#endregion Distinct
#region First
/// <summary>
/// Returns the first element of a sequence.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the first element of.</param>
/// <returns>The first element in source.</returns>
public static JsonElement First(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.First()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements);
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the first element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
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);
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the first element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement First(this JsonDocument source, string predicate, params object?[] args)
{
return First(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion First
#region FirstOrDefault
/// <summary>
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="source">The <see cref="IQueryable"/> to return the first element of.</param>
/// <returns>default if source is empty; otherwise, the first element in source.</returns>
public static JsonElement? FirstOrDefault(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.FirstOrDefault());
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the first element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>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.</returns>
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));
}
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the first element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>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.</returns>
public static JsonElement? FirstOrDefault(this JsonDocument source, string predicate, params object?[] args)
{
return FirstOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion FirstOrDefault
#region GroupBy
/// <summary>
/// Groups the elements of a sequence according to a specified key string function
/// and creates a result value from each group and its key.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> whose elements to group.</param>
/// <param name="keySelector">A string expression to specify the key for each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> where each element represents a projection over a group and its key.</returns>
[PublicAPI]
public static JsonDocument GroupBy(this JsonDocument source, string keySelector, params object[]? args)
{
return GroupBy(source, SystemTextJsonParsingConfig.Default, keySelector, args);
}
/// <summary>
/// Groups the elements of a sequence according to a specified key string function
/// and creates a result value from each group and its key.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> whose elements to group.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="keySelector">A string expression to specify the key for each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> where each element represents a projection over a group and its key.</returns>
[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
/// <summary>
/// Returns the last element of a sequence.
/// </summary>
/// <param name="source">The <see cref="IQueryable"/> to return the last element of.</param>
/// <returns>The last element in source.</returns>
public static JsonElement Last(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.Last()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements);
}
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
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);
}
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement Last(this JsonDocument source, string predicate, params object?[] args)
{
return Last(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Last
#region LastOrDefault
/// <summary>
/// Returns the last element of a sequence, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <returns>default if source is empty; otherwise, the last element in source.</returns>
public static JsonElement? LastOrDefault(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.LastOrDefault());
}
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
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));
}
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition, or a default value if the sequence contains no elements.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement? LastOrDefault(this JsonDocument source, string predicate, params object?[] args)
{
return LastOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion LastOrDefault
#region Max
/// <summary>
/// Computes the max element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <returns>The max element in the sequence.</returns>
public static object Max(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Max();
}
/// <summary>
/// Computes the max element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
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);
}
/// <summary>
/// Computes the max element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the max for.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The max element in the sequence.</returns>
public static object Max(this JsonDocument source, string predicate, params object?[] args)
{
return Max(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Max
#region Min
/// <summary>
/// Computes the min element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <returns>The min element in the sequence.</returns>
public static object Min(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Min();
}
/// <summary>
/// Computes the min element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
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);
}
/// <summary>
/// Computes the min element of a sequence.
/// </summary>
/// <param name="source">A sequence of values to calculate find the min for.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The min element in the sequence.</returns>
public static object Min(this JsonDocument source, string predicate, params object?[] args)
{
return Min(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Min
#region OrderBy
/// <summary>
/// Sorts the elements of a sequence in ascending or descending order according to a key.
/// </summary>
/// <param name="source">A sequence of values to order.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="ordering">An expression string to indicate values to order by.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
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));
}
/// <summary>
/// Sorts the elements of a sequence in ascending or descending order according to a key.
/// </summary>
/// <param name="source">A sequence of values to order.</param>
/// <param name="ordering">An expression string to indicate values to order by.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
public static JsonDocument OrderBy(this JsonDocument source, string ordering, params object?[] args)
{
return OrderBy(source, SystemTextJsonParsingConfig.Default, ordering, args);
}
/// <summary>
/// Sorts the elements of a sequence in ascending or descending order according to a key.
/// </summary>
/// <param name="source">A sequence of values to order.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="ordering">An expression string to indicate values to order by.</param>
/// <param name="comparer">The comparer to use.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
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));
}
/// <summary>
/// Sorts the elements of a sequence in ascending or descending order according to a key.
/// </summary>
/// <param name="source">A sequence of values to order.</param>
/// <param name="ordering">An expression string to indicate values to order by.</param>
/// <param name="comparer">The comparer to use.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JsonDocument"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
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
/// <summary>
/// Returns the elements as paged.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="page">The page to return.</param>
/// <param name="pageSize">The number of elements per page.</param>
/// <returns>A <see cref="JsonDocument"/> that contains the paged elements.</returns>
public static JsonDocument Page(this JsonDocument source, int page, int pageSize)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.Page(page, pageSize));
}
/// <summary>
/// Returns the elements as paged and include the CurrentPage, PageCount, PageSize and RowCount.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="page">The page to return.</param>
/// <param name="pageSize">The number of elements per page.</param>
/// <param name="rowCount">If this optional parameter has been defined, this value is used as the RowCount instead of executing a Linq `Count()`.</param>
/// <returns>PagedResult</returns>
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
/// <summary>
/// Inverts the order of the elements in a sequence.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <returns>A <see cref="JsonDocument"/> whose elements correspond to those of the input sequence in reverse order.</returns>
public static JsonDocument Reverse(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonDocumentArray(() => queryable.Reverse());
}
#endregion Reverse
#region Select
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. </param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JsonDocument Select(this JsonDocument source, string selector, params object?[] args)
{
return Select(source, SystemTextJsonParsingConfig.Default, selector, args);
}
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
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));
}
/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
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));
}
/// <summary>
/// Projects each element of a sequence into a new class of type TResult.
/// Details see http://solutionizing.net/category/linq/
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="resultType">The result type.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>An <see cref="JsonDocument"/> whose elements are the result of invoking a projection string on each element of source.</returns>
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
/// <summary>
/// Projects each element of a sequence to an <see cref="JsonDocument"/> and combines the resulting sequences into one sequence.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. </param>
/// <returns>A <see cref="JsonDocument"/> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.</returns>
public static JsonDocument SelectMany(this JsonDocument source, string selector, params object?[] args)
{
return SelectMany(source, SystemTextJsonParsingConfig.Default, selector, args);
}
/// <summary>
/// Projects each element of a sequence to an <see cref="JsonDocument"/> and combines the resulting sequences into one sequence.
/// </summary>
/// <param name="source">The source <see cref="JsonDocument"/></param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="selector">A projection string expression to apply to each element.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters.</param>
/// <returns>A <see cref="JsonDocument"/> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.</returns>
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
/// <summary>
/// Returns the only element of a sequence, and throws an exception if there
/// is not exactly one element in the sequence.
/// </summary>
/// <param name="source">A <see cref="IQueryable"/> to return the single element of.</param>
/// <returns>The single element of the input sequence.</returns>
public static JsonElement Single(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.Single()) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
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);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement Single(this JsonDocument source, string predicate, params object?[] args)
{
return Single(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Single
#region SingleOrDefault
/// <summary>
/// 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.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return the single element of.</param>
/// <returns>The single element of the input sequence, or default if the sequence contains no elements.</returns>
public static JsonElement? SingleOrDefault(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJsonElement(queryable.SingleOrDefault());
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
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));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">The <see cref="JsonDocument"/> to return the last element of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The first element in source that passes the test in predicate.</returns>
public static JsonElement? SingleOrDefault(this JsonDocument source, string predicate, params object?[] args)
{
return SingleOrDefault(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion SingleOrDefault
#region Skip
/// <summary>
/// Bypasses a specified number of elements in a sequence and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</param>
/// <param name="count">The number of elements to skip before returning the remaining elements.</param>
/// <returns>A <see cref="JsonDocument"/> that contains elements that occur after the specified index in the input sequence.</returns>
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
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
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));
}
/// <summary>
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
/// </summary>
/// <param name="source">A <see cref="JsonDocument"/> to return elements from.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JsonDocument"/> that contains elements from source starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
public static JsonDocument SkipWhile(this JsonDocument source, string predicate, params object?[] args)
{
return SkipWhile(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion SkipWhile
#region Sum
/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Sum();
}
/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The sum of the values in the sequence.</returns>
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);
}
/// <summary>
/// Computes the sum of a sequence of numeric values.
/// </summary>
/// <param name="source">A sequence of numeric values to calculate the sum of.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
/// <returns>The sum of the values in the sequence.</returns>
public static object Sum(this JsonDocument source, string predicate, params object?[] args)
{
return Sum(source, SystemTextJsonParsingConfig.Default, predicate, args);
}
#endregion Sum
#region Take
/// <summary>
/// Returns a specified number of contiguous elements from the start of a sequence.
/// </summary>
/// <param name="source">The sequence to return elements from.</param>
/// <param name="count">The number of elements to return.</param>
/// <returns>A <see cref="IQueryable"/> that contains the specified number of elements from the start of source.</returns>
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
/// <summary>
/// Returns elements from a sequence as long as a specified condition is true.
/// </summary>
/// <param name="source">The sequence to return elements from.</param>
/// <param name="config">The <see cref="SystemTextJsonParsingConfig"/>.</param>
/// <param name="predicate">A function to test each element for a condition.</param>