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);