~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Linq/Extensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
  }
 
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 Newtonsoft.Json.Utilities;
 
29
using System.Globalization;
 
30
#if NET20
 
31
using Newtonsoft.Json.Utilities.LinqBridge;
 
32
#else
 
33
using System.Linq;
 
34
#endif
 
35
 
 
36
namespace Newtonsoft.Json.Linq
 
37
{
 
38
  /// <summary>
 
39
  /// Contains the LINQ to JSON extension methods.
 
40
  /// </summary>
 
41
  public static class Extensions
 
42
  {
 
43
    /// <summary>
 
44
    /// Returns a collection of tokens that contains the ancestors of every token in the source collection.
 
45
    /// </summary>
 
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
 
50
    {
 
51
      ValidationUtils.ArgumentNotNull(source, "source");
 
52
 
 
53
      return source.SelectMany(j => j.Ancestors()).AsJEnumerable();
 
54
    }
 
55
 
 
56
    //TODO
 
57
    //public static IEnumerable<JObject> AncestorsAndSelf<T>(this IEnumerable<T> source) where T : JObject
 
58
    //{
 
59
    //  ValidationUtils.ArgumentNotNull(source, "source");
 
60
 
 
61
    //  return source.SelectMany(j => j.AncestorsAndSelf());
 
62
    //}
 
63
 
 
64
    /// <summary>
 
65
    /// Returns a collection of tokens that contains the descendants of every token in the source collection.
 
66
    /// </summary>
 
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
 
71
    {
 
72
      ValidationUtils.ArgumentNotNull(source, "source");
 
73
 
 
74
      return source.SelectMany(j => j.Descendants()).AsJEnumerable();
 
75
    }
 
76
 
 
77
    //TODO
 
78
    //public static IEnumerable<JObject> DescendantsAndSelf<T>(this IEnumerable<T> source) where T : JContainer
 
79
    //{
 
80
    //  ValidationUtils.ArgumentNotNull(source, "source");
 
81
 
 
82
    //  return source.SelectMany(j => j.DescendantsAndSelf());
 
83
    //}
 
84
 
 
85
    /// <summary>
 
86
    /// Returns a collection of child properties of every object in the source collection.
 
87
    /// </summary>
 
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)
 
91
    {
 
92
      ValidationUtils.ArgumentNotNull(source, "source");
 
93
 
 
94
      return source.SelectMany(d => d.Properties()).AsJEnumerable();
 
95
    }
 
96
 
 
97
    /// <summary>
 
98
    /// Returns a collection of child values of every object in the source collection with the given key.
 
99
    /// </summary>
 
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)
 
104
    {
 
105
      return Values<JToken, JToken>(source, key).AsJEnumerable();
 
106
    }
 
107
 
 
108
    /// <summary>
 
109
    /// Returns a collection of child values of every object in the source collection.
 
110
    /// </summary>
 
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)
 
114
    {
 
115
      return source.Values(null);
 
116
    }
 
117
 
 
118
    /// <summary>
 
119
    /// Returns a collection of converted child values of every object in the source collection with the given key.
 
120
    /// </summary>
 
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)
 
126
    {
 
127
      return Values<JToken, U>(source, key);
 
128
    }
 
129
 
 
130
    /// <summary>
 
131
    /// Returns a collection of converted child values of every object in the source collection.
 
132
    /// </summary>
 
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)
 
137
    {
 
138
      return Values<JToken, U>(source, null);
 
139
    }
 
140
 
 
141
    /// <summary>
 
142
    /// Converts the value.
 
143
    /// </summary>
 
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)
 
148
    {
 
149
      return value.Value<JToken, U>();
 
150
    }
 
151
 
 
152
    /// <summary>
 
153
    /// Converts the value.
 
154
    /// </summary>
 
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
 
160
    {
 
161
      ValidationUtils.ArgumentNotNull(value, "source");
 
162
 
 
163
      JToken token = value as JToken;
 
164
      if (token == null)
 
165
        throw new ArgumentException("Source value must be a JToken.");
 
166
 
 
167
      return token.Convert<JToken, U>();
 
168
    }
 
169
 
 
170
 
 
171
    internal static IEnumerable<U> Values<T, U>(this IEnumerable<T> source, object key) where T : JToken
 
172
    {
 
173
      ValidationUtils.ArgumentNotNull(source, "source");
 
174
 
 
175
      foreach (JToken token in source)
 
176
      {
 
177
        if (key == null)
 
178
        {
 
179
          if (token is JValue)
 
180
          {
 
181
            yield return Convert<JValue, U>((JValue)token);
 
182
          }
 
183
          else
 
184
          {
 
185
            foreach (JToken t in token.Children())
 
186
            {
 
187
              yield return t.Convert<JToken, U>();
 
188
            }
 
189
          }
 
190
        }
 
191
        else
 
192
        {
 
193
          JToken value = token[key];
 
194
          if (value != null)
 
195
            yield return value.Convert<JToken, U>();
 
196
        }
 
197
      }
 
198
 
 
199
      yield break;
 
200
    }
 
201
 
 
202
    //TODO
 
203
    //public static IEnumerable<T> InDocumentOrder<T>(this IEnumerable<T> source) where T : JObject;
 
204
 
 
205
    //public static IEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
 
206
    //{
 
207
    //  ValidationUtils.ArgumentNotNull(source, "source");
 
208
 
 
209
    //  return source.SelectMany(c => c.Children());
 
210
    //}
 
211
 
 
212
    /// <summary>
 
213
    /// Returns a collection of child tokens of every array in the source collection.
 
214
    /// </summary>
 
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
 
219
    {
 
220
      return Children<T, JToken>(source).AsJEnumerable();
 
221
    }
 
222
 
 
223
    /// <summary>
 
224
    /// Returns a collection of converted child tokens of every array in the source collection.
 
225
    /// </summary>
 
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
 
231
    {
 
232
      ValidationUtils.ArgumentNotNull(source, "source");
 
233
 
 
234
      return source.SelectMany(c => c.Children()).Convert<JToken, U>();
 
235
    }
 
236
 
 
237
    internal static IEnumerable<U> Convert<T, U>(this IEnumerable<T> source) where T : JToken
 
238
    {
 
239
      ValidationUtils.ArgumentNotNull(source, "source");
 
240
 
 
241
      foreach (T token in source)
 
242
      {
 
243
        yield return Convert<JToken, U>(token);
 
244
      }
 
245
    }
 
246
 
 
247
    internal static U Convert<T, U>(this T token) where T : JToken
 
248
    {
 
249
      if (token == null)
 
250
        return default(U);
 
251
 
 
252
      if (token is U
 
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))
 
255
      {
 
256
        // HACK
 
257
        return (U)(object)token;
 
258
      }
 
259
      else
 
260
      {
 
261
        JValue value = token as JValue;
 
262
        if (value == null)
 
263
          throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
 
264
 
 
265
        if (value.Value is U)
 
266
          return (U)value.Value;
 
267
 
 
268
        Type targetType = typeof(U);
 
269
 
 
270
        if (ReflectionUtils.IsNullableType(targetType))
 
271
        {
 
272
          if (value.Value == null)
 
273
            return default(U);
 
274
 
 
275
          targetType = Nullable.GetUnderlyingType(targetType);
 
276
        }
 
277
 
 
278
        return (U)System.Convert.ChangeType(value.Value, targetType, CultureInfo.InvariantCulture);
 
279
      }
 
280
    }
 
281
 
 
282
    //TODO
 
283
    //public static void Remove<T>(this IEnumerable<T> source) where T : JContainer;
 
284
 
 
285
    /// <summary>
 
286
    /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
 
287
    /// </summary>
 
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)
 
291
    {
 
292
      return source.AsJEnumerable<JToken>();
 
293
    }
 
294
 
 
295
    /// <summary>
 
296
    /// Returns the input typed as <see cref="IJEnumerable{T}"/>.
 
297
    /// </summary>
 
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
 
302
    {
 
303
      if (source == null)
 
304
        return null;
 
305
      else if (source is IJEnumerable<T>)
 
306
        return (IJEnumerable<T>)source;
 
307
      else
 
308
        return new JEnumerable<T>(source);
 
309
    }
 
310
  }
316
311
}
 
 
b'\\ No newline at end of file'