2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
30
using Newtonsoft.Json.Utilities;
31
using System.Collections;
32
using System.Globalization;
34
namespace Newtonsoft.Json.Linq
37
/// Contains the LINQ to JSON extension methods.
39
public static class Extensions
42
/// Returns a collection of tokens that contains the ancestors of every token in the source collection.
44
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JToken"/>.</typeparam>
45
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
46
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the ancestors of every node in the source collection.</returns>
47
public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
49
ValidationUtils.ArgumentNotNull(source, "source");
51
return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
55
//public static IEnumerable<JObject> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JObject
57
// ValidationUtils.ArgumentNotNull(source, "source");
59
// return source.SelectMany(j => j.AncestorsAndSelf());
63
/// Returns a collection of tokens that contains the descendants of every token in the source collection.
65
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JContainer"/>.</typeparam>
66
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
67
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the descendants of every node in the source collection.</returns>
68
public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
70
ValidationUtils.ArgumentNotNull(source, "source");
72
return source.SelectMany(j => j.Descendants()).AsJEnumerable();
76
//public static IEnumerable<JObject> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
78
// ValidationUtils.ArgumentNotNull(source, "source");
80
// return source.SelectMany(j => j.DescendantsAndSelf());
84
/// Returns a collection of child properties of every object in the source collection.
86
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JObject"/> that contains the source collection.</param>
87
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JProperty"/> that contains the properties of every object in the source collection.</returns>
88
public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
90
ValidationUtils.ArgumentNotNull(source, "source");
92
return source.SelectMany(d => d.Properties()).AsJEnumerable();
96
/// Returns a collection of child values of every object in the source collection with the given key.
98
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
99
/// <param name="key">The token key.</param>
100
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection with the given key.</returns>
101
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
103
return Values<JToken, JToken>(source, key).AsJEnumerable();
107
/// Returns a collection of child values of every object in the source collection.
109
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
110
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
111
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
113
return source.Values(null);
117
/// Returns a collection of converted child values of every object in the source collection with the given key.
119
/// <typeparam name="U">The type to convert the values to.</typeparam>
120
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
121
/// <param name="key">The token key.</param>
122
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection with the given key.</returns>
123
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
125
return Values<JToken, U>(source, key);
129
/// Returns a collection of converted child values of every object in the source collection.
131
/// <typeparam name="U">The type to convert the values to.</typeparam>
132
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
133
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection.</returns>
134
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source)
136
return Values<JToken, U>(source, null);
140
/// Converts the value.
142
/// <typeparam name="U">The type to convert the value to.</typeparam>
143
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
144
/// <returns>A converted value.</returns>
145
public static U Value<U>(this IEnumerable<JToken> value)
147
return value.Value<JToken, U>();
151
/// Converts the value.
153
/// <typeparam name="T">The source collection type.</typeparam>
154
/// <typeparam name="U">The type to convert the value to.</typeparam>
155
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
156
/// <returns>A converted value.</returns>
157
public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
159
ValidationUtils.ArgumentNotNull(value, "source");
161
JToken token = value as JToken;
163
throw new ArgumentException("Source value must be a JToken.");
165
return token.Convert<JToken, U>();
169
internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
171
ValidationUtils.ArgumentNotNull(source, "source");
173
foreach (JToken token in source)
179
yield return Convert<JValue, U>((JValue)token);
183
foreach (JToken t in token.Children())
185
yield return t.Convert<JToken, U>(); ;
191
JToken value = token[key];
193
yield return value.Convert<JToken, U>();
201
//public static IEnumerable<T> InDocumentOrder<T>(this IEnumerable<T> source) where T : JObject;
203
//public static IEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
205
// ValidationUtils.ArgumentNotNull(source, "source");
207
// return source.SelectMany(c => c.Children());
211
/// Returns a collection of child tokens of every array in the source collection.
213
/// <typeparam name="T">The source collection type.</typeparam>
214
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
215
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
216
public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
218
return Children<T, JToken>(source).AsJEnumerable();
222
/// Returns a collection of converted child tokens of every array in the source collection.
224
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
225
/// <typeparam name="U">The type to convert the values to.</typeparam>
226
/// <typeparam name="T">The source collection type.</typeparam>
227
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection.</returns>
228
public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
230
ValidationUtils.ArgumentNotNull(source, "source");
232
return source.SelectMany(c => c.Children()).Convert<JToken, U>();
235
internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
237
ValidationUtils.ArgumentNotNull(source, "source");
239
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
241
foreach (JToken token in source)
243
yield return Convert<JToken, U>(token, cast);
247
internal static U Convert<T, U>(this T token) where T : JToken
249
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
251
return Convert<T, U>(token, cast);
254
internal static U Convert<T, U>(this T token, bool cast) where T : JToken
259
return (U)(object)token;
266
JValue value = token as JValue;
268
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
270
if (value.Value is U)
271
return (U)value.Value;
273
Type targetType = typeof(U);
275
if (ReflectionUtils.IsNullableType(targetType))
277
if (value.Value == null)
280
targetType = Nullable.GetUnderlyingType(targetType);
283
return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
288
//public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
291
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
293
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
294
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
295
public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
297
return source.AsJEnumerable<JToken>();
301
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
303
/// <typeparam name="T">The source collection type.</typeparam>
304
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
305
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
306
public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
310
else if (source is IJEnumerable<T>)
311
return (IJEnumerable<T>)source;
313
return new JEnumerable<T>(source);
2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
28
using Newtonsoft.Json.Utilities;
29
using System.Globalization;
31
using Newtonsoft.Json.Utilities.LinqBridge;
36
namespace Newtonsoft.Json.Linq
39
/// Contains the LINQ to JSON extension methods.
41
public static class Extensions
44
/// Returns a collection of tokens that contains the ancestors of every token in the source collection.
46
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JToken"/>.</typeparam>
47
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
48
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the ancestors of every node in the source collection.</returns>
49
public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
51
ValidationUtils.ArgumentNotNull(source, "source");
53
return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
57
//public static IEnumerable<JObject> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JObject
59
// ValidationUtils.ArgumentNotNull(source, "source");
61
// return source.SelectMany(j => j.AncestorsAndSelf());
65
/// Returns a collection of tokens that contains the descendants of every token in the source collection.
67
/// <typeparam name="T">The type of the objects in source, constrained to <see cref="JContainer"/>.</typeparam>
68
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
69
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the descendants of every node in the source collection.</returns>
70
public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
72
ValidationUtils.ArgumentNotNull(source, "source");
74
return source.SelectMany(j => j.Descendants()).AsJEnumerable();
78
//public static IEnumerable<JObject> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
80
// ValidationUtils.ArgumentNotNull(source, "source");
82
// return source.SelectMany(j => j.DescendantsAndSelf());
86
/// Returns a collection of child properties of every object in the source collection.
88
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JObject"/> that contains the source collection.</param>
89
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JProperty"/> that contains the properties of every object in the source collection.</returns>
90
public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
92
ValidationUtils.ArgumentNotNull(source, "source");
94
return source.SelectMany(d => d.Properties()).AsJEnumerable();
98
/// Returns a collection of child values of every object in the source collection with the given key.
100
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
101
/// <param name="key">The token key.</param>
102
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection with the given key.</returns>
103
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
105
return Values<JToken, JToken>(source, key).AsJEnumerable();
109
/// Returns a collection of child values of every object in the source collection.
111
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
112
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
113
public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
115
return source.Values(null);
119
/// Returns a collection of converted child values of every object in the source collection with the given key.
121
/// <typeparam name="U">The type to convert the values to.</typeparam>
122
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
123
/// <param name="key">The token key.</param>
124
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection with the given key.</returns>
125
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
127
return Values<JToken, U>(source, key);
131
/// Returns a collection of converted child values of every object in the source collection.
133
/// <typeparam name="U">The type to convert the values to.</typeparam>
134
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
135
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection.</returns>
136
public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source)
138
return Values<JToken, U>(source, null);
142
/// Converts the value.
144
/// <typeparam name="U">The type to convert the value to.</typeparam>
145
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
146
/// <returns>A converted value.</returns>
147
public static U Value<U>(this IEnumerable<JToken> value)
149
return value.Value<JToken, U>();
153
/// Converts the value.
155
/// <typeparam name="T">The source collection type.</typeparam>
156
/// <typeparam name="U">The type to convert the value to.</typeparam>
157
/// <param name="value">A <see cref="JToken"/> cast as a <see cref="IEnumerable{T}"/> of <see cref="JToken"/>.</param>
158
/// <returns>A converted value.</returns>
159
public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
161
ValidationUtils.ArgumentNotNull(value, "source");
163
JToken token = value as JToken;
165
throw new ArgumentException("Source value must be a JToken.");
167
return token.Convert<JToken, U>();
171
internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
173
ValidationUtils.ArgumentNotNull(source, "source");
175
foreach (JToken token in source)
181
yield return Convert<JValue, U>((JValue)token);
185
foreach (JToken t in token.Children())
187
yield return t.Convert<JToken, U>();
193
JToken value = token[key];
195
yield return value.Convert<JToken, U>();
203
//public static IEnumerable<T> InDocumentOrder<T>(this IEnumerable<T> source) where T : JObject;
205
//public static IEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
207
// ValidationUtils.ArgumentNotNull(source, "source");
209
// return source.SelectMany(c => c.Children());
213
/// Returns a collection of child tokens of every array in the source collection.
215
/// <typeparam name="T">The source collection type.</typeparam>
216
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
217
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the values of every node in the source collection.</returns>
218
public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
220
return Children<T, JToken>(source).AsJEnumerable();
224
/// Returns a collection of converted child tokens of every array in the source collection.
226
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
227
/// <typeparam name="U">The type to convert the values to.</typeparam>
228
/// <typeparam name="T">The source collection type.</typeparam>
229
/// <returns>An <see cref="IEnumerable{T}"/> that contains the converted values of every node in the source collection.</returns>
230
public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
232
ValidationUtils.ArgumentNotNull(source, "source");
234
return source.SelectMany(c => c.Children()).Convert<JToken, U>();
237
internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
239
ValidationUtils.ArgumentNotNull(source, "source");
241
foreach (T token in source)
243
yield return Convert<JToken, U>(token);
247
internal static U Convert<T, U>(this T token) where T : JToken
253
// don't want to cast JValue to its interfaces, want to get the internal value
254
&& typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
257
return (U)(object)token;
261
JValue value = token as JValue;
263
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
265
if (value.Value is U)
266
return (U)value.Value;
268
Type targetType = typeof(U);
270
if (ReflectionUtils.IsNullableType(targetType))
272
if (value.Value == null)
275
targetType = Nullable.GetUnderlyingType(targetType);
278
return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
283
//public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
286
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
288
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
289
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
290
public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
292
return source.AsJEnumerable<JToken>();
296
/// Returns the input typed as <see cref="IJEnumerable{T}"/>.
298
/// <typeparam name="T">The source collection type.</typeparam>
299
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> that contains the source collection.</param>
300
/// <returns>The input typed as <see cref="IJEnumerable{T}"/>.</returns>
301
public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
305
else if (source is IJEnumerable<T>)
306
return (IJEnumerable<T>)source;
308
return new JEnumerable<T>(source);
b'\\ No newline at end of file'