All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json / Linq / Extensions.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
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
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
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.
24 #endregion
25
26 using System;
27 using System.Collections.Generic;
28 using System.Linq;
29 using System.Text;
30 using Newtonsoft.Json.Utilities;
31 using System.Collections;
32 using System.Globalization;
33
34 namespace Newtonsoft.Json.Linq
35 {
36   /// <summary>
37   /// Contains the LINQ to JSON extension methods.
38   /// </summary>
39   public static class Extensions
40   {
41     /// <summary>
42     /// Returns a collection of tokens that contains the ancestors of every token in the source collection.
43     /// </summary>
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
48     {
49       ValidationUtils.ArgumentNotNull(source, "source");
50
51       return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
52     }
53
54     //TODO
55     //public static IEnumerable<JObject> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JObject
56     //{
57     //  ValidationUtils.ArgumentNotNull(source, "source");
58
59     //  return source.SelectMany(j => j.AncestorsAndSelf());
60     //}
61
62     /// <summary>
63     /// Returns a collection of tokens that contains the descendants of every token in the source collection.
64     /// </summary>
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
69     {
70       ValidationUtils.ArgumentNotNull(source, "source");
71
72       return source.SelectMany(j => j.Descendants()).AsJEnumerable();
73     }
74
75     //TODO
76     //public static IEnumerable<JObject> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
77     //{
78     //  ValidationUtils.ArgumentNotNull(source, "source");
79
80     //  return source.SelectMany(j => j.DescendantsAndSelf());
81     //}
82
83     /// <summary>
84     /// Returns a collection of child properties of every object in the source collection.
85     /// </summary>
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)
89     {
90       ValidationUtils.ArgumentNotNull(source, "source");
91
92       return source.SelectMany(d => d.Properties()).AsJEnumerable();
93     }
94
95     /// <summary>
96     /// Returns a collection of child values of every object in the source collection with the given key.
97     /// </summary>
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)
102     {
103       return Values<JToken, JToken>(source, key).AsJEnumerable();
104     }
105
106     /// <summary>
107     /// Returns a collection of child values of every object in the source collection.
108     /// </summary>
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)
112     {
113       return source.Values(null);
114     }
115
116     /// <summary>
117     /// Returns a collection of converted child values of every object in the source collection with the given key.
118     /// </summary>
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)
124     {
125       return Values<JToken, U>(source, key);
126     }
127
128     /// <summary>
129     /// Returns a collection of converted child values of every object in the source collection.
130     /// </summary>
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)
135     {
136       return Values<JToken, U>(source, null);
137     }
138
139     /// <summary>
140     /// Converts the value.
141     /// </summary>
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)
146     {
147       return value.Value<JToken, U>();
148     }
149
150     /// <summary>
151     /// Converts the value.
152     /// </summary>
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
158     {
159       ValidationUtils.ArgumentNotNull(value, "source");
160
161       JToken token = value as JToken;
162       if (token == null)
163         throw new ArgumentException("Source value must be a JToken.");
164
165       return token.Convert<JToken, U>();
166     }
167
168
169     internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
170     {
171       ValidationUtils.ArgumentNotNull(source, "source");
172
173       foreach (JToken token in source)
174       {
175         if (key == null)
176         {
177           if (token is JValue)
178           {
179             yield return Convert<JValue, U>((JValue)token);
180           }
181           else
182           {
183             foreach (JToken t in token.Children())
184             {
185               yield return t.Convert<JToken, U>(); ;
186             }
187           }
188         }
189         else
190         {
191           JToken value = token[key];
192           if (value != null)
193             yield return value.Convert<JToken, U>();
194         }
195       }
196
197       yield break;
198     }
199
200     //TODO
201     //public static IEnumerable<T> InDocumentOrder<T>(this IEnumerable<T> source) where T : JObject;
202
203     //public static IEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
204     //{
205     //  ValidationUtils.ArgumentNotNull(source, "source");
206
207     //  return source.SelectMany(c => c.Children());
208     //}
209
210     /// <summary>
211     /// Returns a collection of child tokens of every array in the source collection.
212     /// </summary>
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
217     {
218       return Children<T, JToken>(source).AsJEnumerable();
219     }
220
221     /// <summary>
222     /// Returns a collection of converted child tokens of every array in the source collection.
223     /// </summary>
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
229     {
230       ValidationUtils.ArgumentNotNull(source, "source");
231
232       return source.SelectMany(c => c.Children()).Convert<JToken, U>();
233     }
234
235     internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
236     {
237       ValidationUtils.ArgumentNotNull(source, "source");
238
239       bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
240
241       foreach (JToken token in source)
242       {
243         yield return Convert<JToken, U>(token, cast);
244       }
245     }
246
247     internal static U Convert<T, U>(this T token) where T : JToken
248     {
249       bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
250
251       return Convert<T, U>(token, cast);
252     }
253
254     internal static U Convert<T, U>(this T token, bool cast) where T : JToken
255     {
256       if (cast)
257       {
258         // HACK
259         return (U)(object)token;
260       }
261       else
262       {
263         if (token == null)
264           return default(U);
265
266         JValue value = token as JValue;
267         if (value == null)
268           throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
269
270         if (value.Value is U)
271           return (U)value.Value;
272
273         Type targetType = typeof(U);
274
275         if (ReflectionUtils.IsNullableType(targetType))
276         {
277           if (value.Value == null)
278             return default(U);
279
280           targetType = Nullable.GetUnderlyingType(targetType);
281         }
282
283         return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
284       }
285     }
286
287     //TODO
288     //public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
289
290     /// <summary>
291     /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
292     /// </summary>
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)
296     {
297       return source.AsJEnumerable<JToken>();
298     }
299
300     /// <summary>
301     /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
302     /// </summary>
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
307     {
308       if (source == null)
309         return null;
310       else if (source is IJEnumerable<T>)
311         return (IJEnumerable<T>)source;
312       else
313         return new JEnumerable<T>(source);
314     }
315   }
316 }