-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathNewtonsoftJsonExtensions.cs
More file actions
875 lines (785 loc) · 46.5 KB
/
NewtonsoftJsonExtensions.cs
File metadata and controls
875 lines (785 loc) · 46.5 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
using System.Collections;
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
using System.Linq.Dynamic.Core.NewtonsoftJson.Extensions;
using System.Linq.Dynamic.Core.Validation;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
namespace System.Linq.Dynamic.Core.NewtonsoftJson;
/// <summary>
/// Extension methods for <see cref="JArray"/>.
/// </summary>
public static class NewtonsoftJsonExtensions
{
#region Aggregate
/// <summary>
/// Dynamically runs an aggregate function on the <see cref="JArray"/>>.
/// </summary>
/// <param name="source">The <see cref="JArray"/>> 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 JArray 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">A sequence whose elements to test for a condition.</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>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 JArray source, string predicate, params object?[] args)
{
return All(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
/// <summary>
/// Determines whether all the elements of a sequence satisfy a condition.
/// </summary>
/// <param name="source">A sequence whose elements to test for a condition.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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>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 JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotEmpty(predicate);
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="JArray"/></param>
/// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
public static bool Any(this JArray 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="JArray"/></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. Similar to the way String.Format formats strings.</param>
/// <returns>true if the source sequence contains any elements; otherwise, false.</returns>
public static bool Any(this JArray source, NewtonsoftJsonParsingConfig 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="JArray"/></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>true if the source sequence contains any elements; otherwise, false.</returns>
public static bool Any(this JArray source, string predicate, params object?[] args)
{
return Any(source, NewtonsoftJsonParsingConfig.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="JArray"/></param>
/// <returns>The average of the values in the sequence.</returns>
public static double Average(this JArray 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="JArray"/></param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotEmpty(predicate);
var queryable = ToQueryable(source);
return queryable.Average(config, predicate, args);
}
/// <summary>
/// Computes the average of a sequence of numeric values.
/// </summary>
/// <param name="source">The source <see cref="JArray"/></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 JArray source, string predicate, params object?[] args)
{
return Average(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion Average
#region Cast
/// <summary>
/// Converts the elements of an <see cref="JArray"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JArray"/> 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 JArray source, Type type)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return queryable.Cast(type);
}
/// <summary>
/// Converts the elements of an <see cref="JArray"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be converted.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</param>
/// <param name="typeName">The type to convert the elements of source to.</param>
/// <returns>An <see cref="JArray"/> that contains each element of the source sequence converted to the specified type.</returns>
public static IQueryable Cast(this JArray source, NewtonsoftJsonParsingConfig 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="JArray"/> to the specified type.
/// </summary>
/// <param name="source">The <see cref="JArray"/> 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 JArray source, string typeName)
{
return Cast(source, NewtonsoftJsonParsingConfig.Default, typeName);
}
#endregion Cast
#region Count
/// <summary>
/// Returns the number of elements in a sequence.
/// </summary>
/// <param name="source">The <see cref="JArray"/> that contains the elements to be counted.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JArray source, NewtonsoftJsonParsingConfig 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="JArray"/> 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 JArray source, string predicate, params object?[] args)
{
return Count(source, NewtonsoftJsonParsingConfig.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="JArray"/> 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="JArray"/> that contains defaultValue if source is empty; otherwise, source.</returns>
public static JArray DefaultIfEmpty(this JArray source, object? defaultValue)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJArray(() => queryable.DefaultIfEmpty(defaultValue));
}
#endregion
#region First
/// <summary>
/// Returns the first element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return the first element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken First(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(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="JArray"/> 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 JToken First(this JArray source, string predicate, params object?[] args)
{
return First(source, NewtonsoftJsonParsingConfig.Default, predicate, args) ?? throw new InvalidOperationException(Res.SequenceContainsNoElements);
}
#endregion First
#region 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="JArray"/> to return the first element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken? FirstOrDefault(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(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="JArray"/> 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 JToken? FirstOrDefault(this JArray source, string predicate, params object?[] args)
{
return FirstOrDefault(source, NewtonsoftJsonParsingConfig.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="JArray"/> 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="JArray"/> where each element represents a projection over a group and its key.</returns>
[PublicAPI]
public static JArray GroupBy(this JArray source, string keySelector, params object[]? args)
{
return GroupBy(source, NewtonsoftJsonParsingConfig.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="JArray"/> whose elements to group.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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="JArray"/> where each element represents a projection over a group and its key.</returns>
[PublicAPI]
public static JArray GroupBy(this JArray source, NewtonsoftJsonParsingConfig config, string keySelector, params object[]? args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(keySelector);
var queryable = ToQueryable(source, config);
return ToJArray(() => queryable.GroupBy(config, keySelector, args));
}
#endregion
#region Last
/// <summary>
/// Returns the last element of a sequence that satisfies a specified condition.
/// </summary>
/// <param name="source">The <see cref="JArray"/> to return the last element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken Last(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(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="JArray"/> 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 JToken Last(this JArray source, string predicate, params object?[] args)
{
return Last(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion Last
#region 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="JArray"/> to return the last element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken? LastOrDefault(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(queryable.LastOrDefault(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="JArray"/> 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 JToken? LastOrDefault(this JArray source, string predicate, params object?[] args)
{
return LastOrDefault(source, NewtonsoftJsonParsingConfig.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>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JArray source, NewtonsoftJsonParsingConfig 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 JArray source, string predicate, params object?[] args)
{
return Max(source, NewtonsoftJsonParsingConfig.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>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JArray source, NewtonsoftJsonParsingConfig 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 JArray source, string predicate, params object?[] args)
{
return Min(source, NewtonsoftJsonParsingConfig.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="NewtonsoftJsonParsingConfig"/>.</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="JArray"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
public static JArray OrderBy(this JArray source, NewtonsoftJsonParsingConfig config, string ordering, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(source);
Check.NotNullOrEmpty(ordering);
var queryable = ToQueryable(source, config);
return ToJArray(() => 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="JArray"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
public static JArray OrderBy(this JArray source, string ordering, params object?[] args)
{
return OrderBy(source, NewtonsoftJsonParsingConfig.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="NewtonsoftJsonParsingConfig"/>.</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="JArray"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
public static JArray OrderBy(this JArray source, NewtonsoftJsonParsingConfig config, string ordering, IComparer comparer, params object?[] args)
{
var queryable = ToQueryable(source, config);
return ToJArray(() => 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="JArray"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
public static JArray OrderBy(this JArray source, string ordering, IComparer comparer, params object?[] args)
{
return OrderBy(source, NewtonsoftJsonParsingConfig.Default, ordering, comparer, args);
}
#endregion OrderBy
#region Page/PageResult
/// <summary>
/// Returns the elements as paged.
/// </summary>
/// <param name="source">The source <see cref="JArray"/></param>
/// <param name="page">The page to return.</param>
/// <param name="pageSize">The number of elements per page.</param>
/// <returns>A <see cref="JArray"/> that contains the paged elements.</returns>
public static JArray Page(this JArray source, int page, int pageSize)
{
Check.NotNull(source);
var queryable = ToQueryable(source);
return ToJArray(() => 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="JArray"/></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 JArray 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 Select
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">A sequence of values to project.</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. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, string selector, params object?[] args)
{
return Select(source, NewtonsoftJsonParsingConfig.Default, selector, args);
}
/// <summary>
/// Projects each element of a sequence into a new form.
/// </summary>
/// <param name="source">A sequence of values to project.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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. Similar to the way String.Format formats strings.</param>
/// <returns>An <see cref="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, NewtonsoftJsonParsingConfig config, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(selector);
if (source.Count == 0)
{
return [];
}
var queryable = ToQueryable(source, config);
return ToJArray(() => 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="JArray"/></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="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, NewtonsoftJsonParsingConfig config, Type resultType, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJArray(() => 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="JArray"/></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="JArray"/> whose elements are the result of invoking a projection string on each element of source.</returns>
public static JArray Select(this JArray source, Type resultType, string selector, params object?[] args)
{
return Select(source, NewtonsoftJsonParsingConfig.Default, resultType, selector, args);
}
#endregion Select
#region SelectMany
/// <summary>
/// Projects each element of a sequence to an <see cref="JArray"/> and combines the resulting sequences into one sequence.
/// </summary>
/// <param name="source">A sequence of values to project.</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. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JArray"/> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.</returns>
public static JArray SelectMany(this JArray source, string selector, params object?[] args)
{
return SelectMany(source, NewtonsoftJsonParsingConfig.Default, selector, args);
}
/// <summary>
/// Projects each element of a sequence to an <see cref="JArray"/> and combines the resulting sequences into one sequence.
/// </summary>
/// <param name="source">A sequence of values to project.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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. Similar to the way String.Format formats strings.</param>
/// <returns>A <see cref="JArray"/> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.</returns>
public static JArray SelectMany(this JArray source, NewtonsoftJsonParsingConfig config, string selector, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(selector);
if (source.Count == 0)
{
return [];
}
var queryable = ToQueryable(source, config);
return ToJArray(() => queryable.SelectMany(config, selector, args));
}
#endregion SelectMany
#region Single
/// <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="JArray"/> to return the last element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken Single(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(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="JArray"/> 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 JToken Single(this JArray source, string predicate, params object?[] args)
{
return Single(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion Single
#region 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="JArray"/> to return the last element of.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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 JToken? SingleOrDefault(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source, config);
return ToJToken(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="JArray"/> 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 JToken? SingleOrDefault(this JArray source, string predicate, params object?[] args)
{
return SingleOrDefault(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion SingleOrDefault
#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="JArray"/> to return elements from.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</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="JArray"/> 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 JArray SkipWhile(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source);
return ToJArray(() => 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="JArray"/> 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="JArray"/> 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 JArray SkipWhile(this JArray source, string predicate, params object?[] args)
{
return SkipWhile(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion SkipWhile
#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="NewtonsoftJsonParsingConfig"/>.</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="JArray"/> that contains elements from the input sequence occurring before the element at which the test specified by predicate no longer passes.</returns>
public static JArray TakeWhile(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
var queryable = ToQueryable(source);
return ToJArray(() => queryable.TakeWhile(predicate, args));
}
/// <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="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="JArray"/> that contains elements from the input sequence occurring before the element at which the test specified by predicate no longer passes.</returns>
public static JArray TakeWhile(this JArray source, string predicate, params object?[] args)
{
return TakeWhile(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
#endregion TakeWhile
#region Where
/// <summary>
/// Filters a sequence of values based on a predicate.
/// </summary>
/// <param name="source">A <see cref="JArray"/> to filter.</param>
/// <param name="predicate">An expression string 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>A <see cref="JArray"/> that contains elements from the input sequence that satisfy the condition specified by predicate.</returns>
public static JArray Where(this JArray source, string predicate, params object?[] args)
{
return Where(source, NewtonsoftJsonParsingConfig.Default, predicate, args);
}
/// <summary>
/// Filters a sequence of values based on a predicate.
/// </summary>
/// <param name="source">A <see cref="JArray"/> to filter.</param>
/// <param name="config">The <see cref="NewtonsoftJsonParsingConfig"/>.</param>
/// <param name="predicate">An expression string 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>A <see cref="JArray"/> that contains elements from the input sequence that satisfy the condition specified by predicate.</returns>
public static JArray Where(this JArray source, NewtonsoftJsonParsingConfig config, string predicate, params object?[] args)
{
Check.NotNull(source);
Check.NotNull(config);
Check.NotNullOrEmpty(predicate);
if (source.Count == 0)
{
return new JArray();
}
var queryable = ToQueryable(source, config);
return ToJArray(() => queryable.Where(config, predicate, args));
}
#endregion Where
#region Private Methods
private static JToken? ToJToken(object? value)
{
if (value == null)
{
return null;
}
if (value is JToken jToken)
{
return jToken;
}
return JToken.FromObject(value);
}
private static JArray ToJArray(Func<IQueryable> func)
{
var array = new JArray();
foreach (var dynamicElement in func())
{
var element = dynamicElement switch
{
IGrouping<object, object> grouping => new JObject
{
[nameof(grouping.Key)] = JToken.FromObject(grouping.Key),
["Values"] = ToJArray(grouping.AsQueryable)
},
DynamicClass dynamicClass => JObject.FromObject(dynamicClass),
_ => dynamicElement
};
array.Add(element);
}
return array;
}
private static IQueryable ToQueryable(JArray source, NewtonsoftJsonParsingConfig? config = null)
{
return source.ToDynamicJsonClassArray(config?.DynamicJsonClassOptions).AsQueryable();
}
#endregion
}