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

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Linq/JToken.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.IO;
30
 
using Newtonsoft.Json.Utilities;
31
 
using System.Diagnostics;
32
 
using System.Globalization;
33
 
using System.Collections;
34
 
using System.ComponentModel;
35
 
 
36
 
namespace Newtonsoft.Json.Linq
37
 
{
38
 
  /// <summary>
39
 
  /// Represents an abstract JSON token.
40
 
  /// </summary>
41
 
  public abstract class JToken : IJEnumerable<JToken>, IJsonLineInfo
42
 
  {
43
 
    private JContainer _parent;
44
 
    internal JToken _next;
45
 
    private static JTokenEqualityComparer _equalityComparer;
46
 
 
47
 
    private int? _lineNumber;
48
 
    private int? _linePosition;
49
 
 
50
 
    /// <summary>
51
 
    /// Gets a comparer that can compare two tokens for value equality.
52
 
    /// </summary>
53
 
    /// <value>A <see cref="JTokenEqualityComparer"/> that can compare two nodes for value equality.</value>
54
 
    public static JTokenEqualityComparer EqualityComparer
55
 
    {
56
 
      get
57
 
      {
58
 
        if (_equalityComparer == null)
59
 
          _equalityComparer = new JTokenEqualityComparer();
60
 
 
61
 
        return _equalityComparer;
62
 
      }
63
 
    }
64
 
 
65
 
    /// <summary>
66
 
    /// Gets or sets the parent.
67
 
    /// </summary>
68
 
    /// <value>The parent.</value>
69
 
    public JContainer Parent
70
 
    {
71
 
      [DebuggerStepThrough]
72
 
      get { return _parent; }
73
 
      internal set { _parent = value; }
74
 
    }
75
 
 
76
 
    /// <summary>
77
 
    /// Gets the root <see cref="JToken"/> of this <see cref="JToken"/>.
78
 
    /// </summary>
79
 
    /// <value>The root <see cref="JToken"/> of this <see cref="JToken"/>.</value>
80
 
    public JToken Root
81
 
    {
82
 
      get
83
 
      {
84
 
        JContainer parent = Parent;
85
 
        if (parent == null)
86
 
          return this;
87
 
 
88
 
        while (parent.Parent != null)
89
 
        {
90
 
          parent = parent.Parent;
91
 
        }
92
 
 
93
 
        return parent;
94
 
      }
95
 
    }
96
 
 
97
 
    internal abstract JToken CloneToken();
98
 
    internal abstract bool DeepEquals(JToken node);
99
 
 
100
 
    /// <summary>
101
 
    /// Gets the node type for this <see cref="JToken"/>.
102
 
    /// </summary>
103
 
    /// <value>The type.</value>
104
 
    public abstract JTokenType Type { get; }
105
 
 
106
 
    /// <summary>
107
 
    /// Gets a value indicating whether this token has childen tokens.
108
 
    /// </summary>
109
 
    /// <value>
110
 
    ///         <c>true</c> if this token has child values; otherwise, <c>false</c>.
111
 
    /// </value>
112
 
    public abstract bool HasValues { get; }
113
 
 
114
 
    /// <summary>
115
 
    /// Compares the values of two tokens, including the values of all descendant tokens.
116
 
    /// </summary>
117
 
    /// <param name="t1">The first <see cref="JToken"/> to compare.</param>
118
 
    /// <param name="t2">The second <see cref="JToken"/> to compare.</param>
119
 
    /// <returns>true if the tokens are equal; otherwise false.</returns>
120
 
    public static bool DeepEquals(JToken t1, JToken t2)
121
 
    {
122
 
      return (t1 == t2 || (t1 != null && t2 != null && t1.DeepEquals(t2)));
123
 
    }
124
 
 
125
 
    /// <summary>
126
 
    /// Gets the next sibling token of this node.
127
 
    /// </summary>
128
 
    /// <value>The <see cref="JToken"/> that contains the next sibling token.</value>
129
 
    public JToken Next
130
 
    {
131
 
      get
132
 
      {
133
 
        if (_parent != null && _next != _parent.First)
134
 
          return _next;
135
 
 
136
 
        return null;
137
 
      }
138
 
      internal set { _next = value; }
139
 
    }
140
 
 
141
 
    /// <summary>
142
 
    /// Gets the previous sibling token of this node.
143
 
    /// </summary>
144
 
    /// <value>The <see cref="JToken"/> that contains the previous sibling token.</value>
145
 
    public JToken Previous
146
 
    {
147
 
      get
148
 
      {
149
 
        if (_parent == null)
150
 
          return null;
151
 
 
152
 
        JToken parentNext = _parent.Content._next;
153
 
        JToken parentNextBefore = null;
154
 
        while (parentNext != this)
155
 
        {
156
 
          parentNextBefore = parentNext;
157
 
          parentNext = parentNext.Next;
158
 
        }
159
 
        return parentNextBefore;
160
 
      }
161
 
    }
162
 
 
163
 
    internal JToken()
164
 
    {
165
 
    }
166
 
 
167
 
    /// <summary>
168
 
    /// Adds the specified content immediately after this token.
169
 
    /// </summary>
170
 
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added after this token.</param>
171
 
    public void AddAfterSelf(object content)
172
 
    {
173
 
      if (_parent == null)
174
 
        throw new InvalidOperationException("The parent is missing.");
175
 
 
176
 
      _parent.AddInternal((Next == null), this, content);
177
 
    }
178
 
 
179
 
    /// <summary>
180
 
    /// Adds the specified content immediately before this token.
181
 
    /// </summary>
182
 
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added before this token.</param>
183
 
    public void AddBeforeSelf(object content)
184
 
    {
185
 
      if (_parent == null)
186
 
        throw new InvalidOperationException("The parent is missing.");
187
 
 
188
 
      JToken previous = Previous;
189
 
      if (previous == null)
190
 
        previous = _parent.Last;
191
 
 
192
 
      _parent.AddInternal(false, previous, content);
193
 
    }
194
 
 
195
 
    /// <summary>
196
 
    /// Returns a collection of the ancestor tokens of this token.
197
 
    /// </summary>
198
 
    /// <returns>A collection of the ancestor tokens of this token.</returns>
199
 
    public IEnumerable<JToken> Ancestors()
200
 
    {
201
 
      for (JToken parent = Parent; parent != null; parent = parent.Parent)
202
 
      {
203
 
        yield return parent;
204
 
      }
205
 
    }
206
 
 
207
 
    /// <summary>
208
 
    /// Returns a collection of the sibling tokens after this token, in document order.
209
 
    /// </summary>
210
 
    /// <returns>A collection of the sibling tokens after this tokens, in document order.</returns>
211
 
    public IEnumerable<JToken> AfterSelf()
212
 
    {
213
 
      if (Parent == null)
214
 
        yield break;
215
 
 
216
 
      for (JToken o = Next; o != null; o = o.Next)
217
 
        yield return o;
218
 
    }
219
 
 
220
 
    /// <summary>
221
 
    /// Returns a collection of the sibling tokens before this token, in document order.
222
 
    /// </summary>
223
 
    /// <returns>A collection of the sibling tokens before this token, in document order.</returns>
224
 
    public IEnumerable<JToken> BeforeSelf()
225
 
    {
226
 
      for (JToken o = Parent.First; o != this; o = o.Next)
227
 
        yield return o;
228
 
    }
229
 
 
230
 
    /// <summary>
231
 
    /// Gets the <see cref="JToken"/> with the specified key.
232
 
    /// </summary>
233
 
    /// <value>The <see cref="JToken"/> with the specified key.</value>
234
 
    public virtual JToken this[object key]
235
 
    {
236
 
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
237
 
      set { throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
238
 
    }
239
 
 
240
 
    /// <summary>
241
 
    /// Gets the <see cref="JToken"/> with the specified key converted to the specified type.
242
 
    /// </summary>
243
 
    /// <typeparam name="T">The type to convert the token to.</typeparam>
244
 
    /// <param name="key">The token key.</param>
245
 
    /// <returns>The converted token value.</returns>
246
 
    public virtual T Value<T>(object key)
247
 
    {
248
 
      JToken token = this[key];
249
 
 
250
 
      return Extensions.Convert<JToken, T>(token);
251
 
    }
252
 
 
253
 
    /// <summary>
254
 
    /// Get the first child token of this token.
255
 
    /// </summary>
256
 
    /// <value>A <see cref="JToken"/> containing the first child token of the <see cref="JToken"/>.</value>
257
 
    public virtual JToken First
258
 
    {
259
 
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
260
 
    }
261
 
 
262
 
    /// <summary>
263
 
    /// Get the last child token of this token.
264
 
    /// </summary>
265
 
    /// <value>A <see cref="JToken"/> containing the last child token of the <see cref="JToken"/>.</value>
266
 
    public virtual JToken Last
267
 
    {
268
 
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
269
 
    }
270
 
 
271
 
    /// <summary>
272
 
    /// Returns a collection of the child tokens of this token, in document order.
273
 
    /// </summary>
274
 
    /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
275
 
    public virtual JEnumerable<JToken> Children()
276
 
    {
277
 
      throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
278
 
    }
279
 
 
280
 
    /// <summary>
281
 
    /// Returns a collection of the child tokens of this token, in document order, filtered by the specified type.
282
 
    /// </summary>
283
 
    /// <typeparam name="T">The type to filter the child tokens on.</typeparam>
284
 
    /// <returns>A <see cref="JEnumerable{T}"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
285
 
    public JEnumerable<T> Children<T>() where T : JToken
286
 
    {
287
 
      return new JEnumerable<T>(Children().OfType<T>());
288
 
    }
289
 
 
290
 
    /// <summary>
291
 
    /// Returns a collection of the child values of this token, in document order.
292
 
    /// </summary>
293
 
    /// <typeparam name="T">The type to convert the values to.</typeparam>
294
 
    /// <returns>A <see cref="IEnumerable{T}"/> containing the child values of this <see cref="JToken"/>, in document order.</returns>
295
 
    public virtual IEnumerable<T> Values<T>()
296
 
    {
297
 
      throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
298
 
    }
299
 
 
300
 
    /// <summary>
301
 
    /// Removes this token from its parent.
302
 
    /// </summary>
303
 
    public void Remove()
304
 
    {
305
 
      if (_parent == null)
306
 
        throw new InvalidOperationException("The parent is missing.");
307
 
 
308
 
      _parent.RemoveItem(this);
309
 
    }
310
 
 
311
 
    /// <summary>
312
 
    /// Replaces this token with the specified token.
313
 
    /// </summary>
314
 
    /// <param name="value">The value.</param>
315
 
    public void Replace(JToken value)
316
 
    {
317
 
      if (_parent == null)
318
 
        throw new InvalidOperationException("The parent is missing.");
319
 
 
320
 
      _parent.ReplaceItem(this, value);
321
 
    }
322
 
 
323
 
    /// <summary>
324
 
    /// Writes this token to a <see cref="JsonWriter"/>.
325
 
    /// </summary>
326
 
    /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
327
 
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
328
 
    public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
329
 
 
330
 
    /// <summary>
331
 
    /// Returns the indented JSON for this token.
332
 
    /// </summary>
333
 
    /// <returns>
334
 
    /// The indented JSON for this token.
335
 
    /// </returns>
336
 
    public override string ToString()
337
 
    {
338
 
      return ToString(Formatting.Indented);
339
 
    }
340
 
 
341
 
    /// <summary>
342
 
    /// Returns the JSON for this token using the given formatting and converters.
343
 
    /// </summary>
344
 
    /// <param name="formatting">Indicates how the output is formatted.</param>
345
 
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
346
 
    /// <returns>The JSON for this token using the given formatting and converters.</returns>
347
 
    public string ToString(Formatting formatting, params JsonConverter[] converters)
348
 
    {
349
 
      using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
350
 
      {
351
 
        JsonTextWriter jw = new JsonTextWriter(sw);
352
 
        jw.Formatting = formatting;
353
 
 
354
 
        WriteTo(jw, converters);
355
 
 
356
 
        return sw.ToString();
357
 
      }
358
 
    }
359
 
 
360
 
    private static JValue EnsureValue(JToken value)
361
 
    {
362
 
      if (value == null)
363
 
        throw new ArgumentNullException("value");
364
 
 
365
 
      if (value is JProperty)
366
 
        value = ((JProperty)value).Value;
367
 
 
368
 
      JValue v = value as JValue;
369
 
 
370
 
      return v;
371
 
    }
372
 
 
373
 
    private static string GetType(JToken token)
374
 
    {
375
 
      ValidationUtils.ArgumentNotNull(token, "token");
376
 
 
377
 
      if (token is JProperty)
378
 
        token = ((JProperty)token).Value;
379
 
 
380
 
      return token.Type.ToString();
381
 
    }
382
 
 
383
 
    private static bool IsNullable(JToken o)
384
 
    {
385
 
      return (o.Type == JTokenType.Undefined || o.Type == JTokenType.Null);
386
 
    }
387
 
 
388
 
    private static bool ValidateFloat(JToken o, bool nullable)
389
 
    {
390
 
      return (o.Type == JTokenType.Float || o.Type == JTokenType.Integer || (nullable && IsNullable(o)));
391
 
    }
392
 
 
393
 
    private static bool ValidateInteger(JToken o, bool nullable)
394
 
    {
395
 
      return (o.Type == JTokenType.Integer || (nullable && IsNullable(o)));
396
 
    }
397
 
 
398
 
    private static bool ValidateDate(JToken o, bool nullable)
399
 
    {
400
 
      return (o.Type == JTokenType.Date || (nullable && IsNullable(o)));
401
 
    }
402
 
 
403
 
    private static bool ValidateBoolean(JToken o, bool nullable)
404
 
    {
405
 
      return (o.Type == JTokenType.Boolean || (nullable && IsNullable(o)));
406
 
    }
407
 
 
408
 
    private static bool ValidateString(JToken o)
409
 
    {
410
 
      return (o.Type == JTokenType.String || o.Type == JTokenType.Comment || o.Type == JTokenType.Raw || IsNullable(o));
411
 
    }
412
 
 
413
 
    private static bool ValidateBytes(JToken o)
414
 
    {
415
 
      return (o.Type == JTokenType.Bytes || IsNullable(o));
416
 
    }
417
 
 
418
 
    #region Cast from operators
419
 
    /// <summary>
420
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Boolean"/>.
421
 
    /// </summary>
422
 
    /// <param name="value">The value.</param>
423
 
    /// <returns>The result of the conversion.</returns>
424
 
    public static explicit operator bool(JToken value)
425
 
    {
426
 
      JValue v = EnsureValue(value);
427
 
      if (v == null || !ValidateBoolean(v, false))
428
 
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
429
 
 
430
 
      return (bool)v.Value;
431
 
    }
432
 
 
433
 
#if !PocketPC && !NET20
434
 
    /// <summary>
435
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTimeOffset"/>.
436
 
    /// </summary>
437
 
    /// <param name="value">The value.</param>
438
 
    /// <returns>The result of the conversion.</returns>
439
 
    public static explicit operator DateTimeOffset(JToken value)
440
 
    {
441
 
      JValue v = EnsureValue(value);
442
 
      if (v == null || !ValidateDate(v, false))
443
 
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
444
 
 
445
 
      return (DateTimeOffset)v.Value;
446
 
    }
447
 
#endif
448
 
 
449
 
    /// <summary>
450
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Boolean}"/>.
451
 
    /// </summary>
452
 
    /// <param name="value">The value.</param>
453
 
    /// <returns>The result of the conversion.</returns>
454
 
    public static explicit operator bool?(JToken value)
455
 
    {
456
 
      if (value == null)
457
 
        return null;
458
 
 
459
 
      JValue v = EnsureValue(value);
460
 
      if (v == null || !ValidateBoolean(v, true))
461
 
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
462
 
 
463
 
      return (bool?)v.Value;
464
 
    }
465
 
 
466
 
    /// <summary>
467
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int64"/>.
468
 
    /// </summary>
469
 
    /// <param name="value">The value.</param>
470
 
    /// <returns>The result of the conversion.</returns>
471
 
    public static explicit operator long(JToken value)
472
 
    {
473
 
      JValue v = EnsureValue(value);
474
 
      if (v == null || !ValidateInteger(v, false))
475
 
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
476
 
 
477
 
      return (long)v.Value;
478
 
    }
479
 
 
480
 
    /// <summary>
481
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTime}"/>.
482
 
    /// </summary>
483
 
    /// <param name="value">The value.</param>
484
 
    /// <returns>The result of the conversion.</returns>
485
 
    public static explicit operator DateTime?(JToken value)
486
 
    {
487
 
      if (value == null)
488
 
        return null;
489
 
 
490
 
      JValue v = EnsureValue(value);
491
 
      if (v == null || !ValidateDate(v, true))
492
 
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
493
 
 
494
 
      return (DateTime?)v.Value;
495
 
    }
496
 
 
497
 
#if !PocketPC && !NET20
498
 
    /// <summary>
499
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTimeOffset}"/>.
500
 
    /// </summary>
501
 
    /// <param name="value">The value.</param>
502
 
    /// <returns>The result of the conversion.</returns>
503
 
    public static explicit operator DateTimeOffset?(JToken value)
504
 
    {
505
 
      if (value == null)
506
 
        return null;
507
 
 
508
 
      JValue v = EnsureValue(value);
509
 
      if (v == null || !ValidateDate(v, true))
510
 
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
511
 
 
512
 
      return (DateTimeOffset?)v.Value;
513
 
    }
514
 
#endif
515
 
 
516
 
    /// <summary>
517
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Decimal}"/>.
518
 
    /// </summary>
519
 
    /// <param name="value">The value.</param>
520
 
    /// <returns>The result of the conversion.</returns>
521
 
    public static explicit operator decimal?(JToken value)
522
 
    {
523
 
      if (value == null)
524
 
        return null;
525
 
 
526
 
      JValue v = EnsureValue(value);
527
 
      if (v == null || !ValidateFloat(v, true))
528
 
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
529
 
 
530
 
      return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;
531
 
    }
532
 
 
533
 
    /// <summary>
534
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Double}"/>.
535
 
    /// </summary>
536
 
    /// <param name="value">The value.</param>
537
 
    /// <returns>The result of the conversion.</returns>
538
 
    public static explicit operator double?(JToken value)
539
 
    {
540
 
      if (value == null)
541
 
        return null;
542
 
 
543
 
      JValue v = EnsureValue(value);
544
 
      if (v == null || !ValidateFloat(v, true))
545
 
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
546
 
 
547
 
      return (double?)v.Value;
548
 
    }
549
 
 
550
 
    /// <summary>
551
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int32"/>.
552
 
    /// </summary>
553
 
    /// <param name="value">The value.</param>
554
 
    /// <returns>The result of the conversion.</returns>
555
 
    public static explicit operator int(JToken value)
556
 
    {
557
 
      JValue v = EnsureValue(value);
558
 
      if (v == null || !ValidateInteger(v, false))
559
 
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
560
 
 
561
 
      return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
562
 
    }
563
 
 
564
 
    /// <summary>
565
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int32}"/>.
566
 
    /// </summary>
567
 
    /// <param name="value">The value.</param>
568
 
    /// <returns>The result of the conversion.</returns>
569
 
    public static explicit operator int?(JToken value)
570
 
    {
571
 
      if (value == null)
572
 
        return null;
573
 
 
574
 
      JValue v = EnsureValue(value);
575
 
      if (v == null || !ValidateInteger(v, true))
576
 
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
577
 
 
578
 
      return (v.Value != null) ? (int?)Convert.ToInt32(v.Value, CultureInfo.InvariantCulture) : null;
579
 
    }
580
 
 
581
 
    /// <summary>
582
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTime"/>.
583
 
    /// </summary>
584
 
    /// <param name="value">The value.</param>
585
 
    /// <returns>The result of the conversion.</returns>
586
 
    public static explicit operator DateTime(JToken value)
587
 
    {
588
 
      JValue v = EnsureValue(value);
589
 
      if (v == null || !ValidateDate(v, false))
590
 
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
591
 
 
592
 
      return (DateTime)v.Value;
593
 
    }
594
 
 
595
 
    /// <summary>
596
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int64}"/>.
597
 
    /// </summary>
598
 
    /// <param name="value">The value.</param>
599
 
    /// <returns>The result of the conversion.</returns>
600
 
    public static explicit operator long?(JToken value)
601
 
    {
602
 
      if (value == null)
603
 
        return null;
604
 
 
605
 
      JValue v = EnsureValue(value);
606
 
      if (v == null || !ValidateInteger(v, true))
607
 
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
608
 
 
609
 
      return (long?)v.Value;
610
 
    }
611
 
 
612
 
    /// <summary>
613
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Single}"/>.
614
 
    /// </summary>
615
 
    /// <param name="value">The value.</param>
616
 
    /// <returns>The result of the conversion.</returns>
617
 
    public static explicit operator float?(JToken value)
618
 
    {
619
 
      if (value == null)
620
 
        return null;
621
 
 
622
 
      JValue v = EnsureValue(value);
623
 
      if (v == null || !ValidateFloat(v, true))
624
 
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
625
 
 
626
 
      return (v.Value != null) ? (float?)Convert.ToSingle(v.Value, CultureInfo.InvariantCulture) : null;
627
 
    }
628
 
 
629
 
    /// <summary>
630
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Decimal"/>.
631
 
    /// </summary>
632
 
    /// <param name="value">The value.</param>
633
 
    /// <returns>The result of the conversion.</returns>
634
 
    public static explicit operator decimal(JToken value)
635
 
    {
636
 
      JValue v = EnsureValue(value);
637
 
      if (v == null || !ValidateFloat(v, false))
638
 
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
639
 
 
640
 
      return Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture);
641
 
    }
642
 
 
643
 
    /// <summary>
644
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt32}"/>.
645
 
    /// </summary>
646
 
    /// <param name="value">The value.</param>
647
 
    /// <returns>The result of the conversion.</returns>
648
 
    [CLSCompliant(false)]
649
 
    public static explicit operator uint?(JToken value)
650
 
    {
651
 
      if (value == null)
652
 
        return null;
653
 
 
654
 
      JValue v = EnsureValue(value);
655
 
      if (v == null || !ValidateInteger(v, true))
656
 
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
657
 
 
658
 
      return (uint?)v.Value;
659
 
    }
660
 
 
661
 
    /// <summary>
662
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt64}"/>.
663
 
    /// </summary>
664
 
    /// <param name="value">The value.</param>
665
 
    /// <returns>The result of the conversion.</returns>
666
 
    [CLSCompliant(false)]
667
 
    public static explicit operator ulong?(JToken value)
668
 
    {
669
 
      if (value == null)
670
 
        return null;
671
 
 
672
 
      JValue v = EnsureValue(value);
673
 
      if (v == null || !ValidateInteger(v, true))
674
 
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
675
 
 
676
 
      return (ulong?)v.Value;
677
 
    }
678
 
 
679
 
    /// <summary>
680
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Double"/>.
681
 
    /// </summary>
682
 
    /// <param name="value">The value.</param>
683
 
    /// <returns>The result of the conversion.</returns>
684
 
    public static explicit operator double(JToken value)
685
 
    {
686
 
      JValue v = EnsureValue(value);
687
 
      if (v == null || !ValidateFloat(v, false))
688
 
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
689
 
 
690
 
      return (double)v.Value;
691
 
    }
692
 
 
693
 
    /// <summary>
694
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Single"/>.
695
 
    /// </summary>
696
 
    /// <param name="value">The value.</param>
697
 
    /// <returns>The result of the conversion.</returns>
698
 
    public static explicit operator float(JToken value)
699
 
    {
700
 
      JValue v = EnsureValue(value);
701
 
      if (v == null || !ValidateFloat(v, false))
702
 
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
703
 
 
704
 
      return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
705
 
    }
706
 
 
707
 
    /// <summary>
708
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.String"/>.
709
 
    /// </summary>
710
 
    /// <param name="value">The value.</param>
711
 
    /// <returns>The result of the conversion.</returns>
712
 
    public static explicit operator string(JToken value)
713
 
    {
714
 
      if (value == null)
715
 
        return null;
716
 
 
717
 
      JValue v = EnsureValue(value);
718
 
      if (v == null || !ValidateString(v))
719
 
        throw new ArgumentException("Can not convert {0} to String.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
720
 
 
721
 
      return (string)v.Value;
722
 
    }
723
 
 
724
 
    /// <summary>
725
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt32"/>.
726
 
    /// </summary>
727
 
    /// <param name="value">The value.</param>
728
 
    /// <returns>The result of the conversion.</returns>
729
 
    [CLSCompliant(false)]
730
 
    public static explicit operator uint(JToken value)
731
 
    {
732
 
      JValue v = EnsureValue(value);
733
 
      if (v == null || !ValidateInteger(v, false))
734
 
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
735
 
 
736
 
      return Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture);
737
 
    }
738
 
 
739
 
    /// <summary>
740
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt64"/>.
741
 
    /// </summary>
742
 
    /// <param name="value">The value.</param>
743
 
    /// <returns>The result of the conversion.</returns>
744
 
    [CLSCompliant(false)]
745
 
    public static explicit operator ulong(JToken value)
746
 
    {
747
 
      JValue v = EnsureValue(value);
748
 
      if (v == null || !ValidateInteger(v, false))
749
 
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
750
 
 
751
 
      return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
752
 
    }
753
 
 
754
 
    /// <summary>
755
 
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="T:System.Byte[]"/>.
756
 
    /// </summary>
757
 
    /// <param name="value">The value.</param>
758
 
    /// <returns>The result of the conversion.</returns>
759
 
    public static explicit operator byte[](JToken value)
760
 
    {
761
 
      JValue v = EnsureValue(value);
762
 
      if (v == null || !ValidateBytes(v))
763
 
        throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
764
 
 
765
 
      return (byte[])v.Value;
766
 
    }
767
 
    #endregion
768
 
 
769
 
    #region Cast to operators
770
 
    /// <summary>
771
 
    /// Performs an implicit conversion from <see cref="Boolean"/> to <see cref="JToken"/>.
772
 
    /// </summary>
773
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
774
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
775
 
    public static implicit operator JToken(bool value)
776
 
    {
777
 
      return new JValue(value);
778
 
    }
779
 
 
780
 
#if !PocketPC && !NET20
781
 
    /// <summary>
782
 
    /// Performs an implicit conversion from <see cref="DateTimeOffset"/> to <see cref="JToken"/>.
783
 
    /// </summary>
784
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
785
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
786
 
    public static implicit operator JToken(DateTimeOffset value)
787
 
    {
788
 
      return new JValue(value);
789
 
    }
790
 
#endif
791
 
 
792
 
    /// <summary>
793
 
    /// Performs an implicit conversion from <see cref="Nullable{Boolean}"/> to <see cref="JToken"/>.
794
 
    /// </summary>
795
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
796
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
797
 
    public static implicit operator JToken(bool? value)
798
 
    {
799
 
      return new JValue(value);
800
 
    }
801
 
 
802
 
    /// <summary>
803
 
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
804
 
    /// </summary>
805
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
806
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
807
 
    public static implicit operator JToken(long value)
808
 
    {
809
 
      return new JValue(value);
810
 
    }
811
 
 
812
 
    /// <summary>
813
 
    /// Performs an implicit conversion from <see cref="Nullable{DateTime}"/> to <see cref="JToken"/>.
814
 
    /// </summary>
815
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
816
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
817
 
    public static implicit operator JToken(DateTime? value)
818
 
    {
819
 
      return new JValue(value);
820
 
    }
821
 
 
822
 
#if !PocketPC && !NET20
823
 
    /// <summary>
824
 
    /// Performs an implicit conversion from <see cref="Nullable{DateTimeOffset}"/> to <see cref="JToken"/>.
825
 
    /// </summary>
826
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
827
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
828
 
    public static implicit operator JToken(DateTimeOffset? value)
829
 
    {
830
 
      return new JValue(value);
831
 
    }
832
 
#endif
833
 
 
834
 
    /// <summary>
835
 
    /// Performs an implicit conversion from <see cref="Nullable{Decimal}"/> to <see cref="JToken"/>.
836
 
    /// </summary>
837
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
838
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
839
 
    public static implicit operator JToken(decimal? value)
840
 
    {
841
 
      return new JValue(value);
842
 
    }
843
 
 
844
 
    /// <summary>
845
 
    /// Performs an implicit conversion from <see cref="Nullable{Double}"/> to <see cref="JToken"/>.
846
 
    /// </summary>
847
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
848
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
849
 
    public static implicit operator JToken(double? value)
850
 
    {
851
 
      return new JValue(value);
852
 
    }
853
 
 
854
 
    /// <summary>
855
 
    /// Performs an implicit conversion from <see cref="UInt16"/> to <see cref="JToken"/>.
856
 
    /// </summary>
857
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
858
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
859
 
    [CLSCompliant(false)]
860
 
    public static implicit operator JToken(ushort value)
861
 
    {
862
 
      return new JValue(value);
863
 
    }
864
 
 
865
 
    /// <summary>
866
 
    /// Performs an implicit conversion from <see cref="Int32"/> to <see cref="JToken"/>.
867
 
    /// </summary>
868
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
869
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
870
 
    public static implicit operator JToken(int value)
871
 
    {
872
 
      return new JValue(value);
873
 
    }
874
 
 
875
 
    /// <summary>
876
 
    /// Performs an implicit conversion from <see cref="Nullable{Int32}"/> to <see cref="JToken"/>.
877
 
    /// </summary>
878
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
879
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
880
 
    public static implicit operator JToken(int? value)
881
 
    {
882
 
      return new JValue(value);
883
 
    }
884
 
 
885
 
    /// <summary>
886
 
    /// Performs an implicit conversion from <see cref="DateTime"/> to <see cref="JToken"/>.
887
 
    /// </summary>
888
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
889
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
890
 
    public static implicit operator JToken(DateTime value)
891
 
    {
892
 
      return new JValue(value);
893
 
    }
894
 
 
895
 
    /// <summary>
896
 
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
897
 
    /// </summary>
898
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
899
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
900
 
    public static implicit operator JToken(long? value)
901
 
    {
902
 
      return new JValue(value);
903
 
    }
904
 
 
905
 
    /// <summary>
906
 
    /// Performs an implicit conversion from <see cref="Nullable{Single}"/> to <see cref="JToken"/>.
907
 
    /// </summary>
908
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
909
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
910
 
    public static implicit operator JToken(float? value)
911
 
    {
912
 
      return new JValue(value);
913
 
    }
914
 
 
915
 
    /// <summary>
916
 
    /// Performs an implicit conversion from <see cref="Decimal"/> to <see cref="JToken"/>.
917
 
    /// </summary>
918
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
919
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
920
 
    public static implicit operator JToken(decimal value)
921
 
    {
922
 
      return new JValue(value);
923
 
    }
924
 
 
925
 
    /// <summary>
926
 
    /// Performs an implicit conversion from <see cref="Nullable{UInt16}"/> to <see cref="JToken"/>.
927
 
    /// </summary>
928
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
929
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
930
 
    [CLSCompliant(false)]
931
 
    public static implicit operator JToken(ushort? value)
932
 
    {
933
 
      return new JValue(value);
934
 
    }
935
 
 
936
 
    /// <summary>
937
 
    /// Performs an implicit conversion from <see cref="Nullable{UInt32}"/> to <see cref="JToken"/>.
938
 
    /// </summary>
939
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
940
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
941
 
    [CLSCompliant(false)]
942
 
    public static implicit operator JToken(uint? value)
943
 
    {
944
 
      return new JValue(value);
945
 
    }
946
 
 
947
 
    /// <summary>
948
 
    /// Performs an implicit conversion from <see cref="Nullable{UInt64}"/> to <see cref="JToken"/>.
949
 
    /// </summary>
950
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
951
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
952
 
    [CLSCompliant(false)]
953
 
    public static implicit operator JToken(ulong? value)
954
 
    {
955
 
      return new JValue(value);
956
 
    }
957
 
 
958
 
    /// <summary>
959
 
    /// Performs an implicit conversion from <see cref="Double"/> to <see cref="JToken"/>.
960
 
    /// </summary>
961
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
962
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
963
 
    public static implicit operator JToken(double value)
964
 
    {
965
 
      return new JValue(value);
966
 
    }
967
 
 
968
 
    /// <summary>
969
 
    /// Performs an implicit conversion from <see cref="Single"/> to <see cref="JToken"/>.
970
 
    /// </summary>
971
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
972
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
973
 
    public static implicit operator JToken(float value)
974
 
    {
975
 
      return new JValue(value);
976
 
    }
977
 
 
978
 
    /// <summary>
979
 
    /// Performs an implicit conversion from <see cref="String"/> to <see cref="JToken"/>.
980
 
    /// </summary>
981
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
982
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
983
 
    public static implicit operator JToken(string value)
984
 
    {
985
 
      return new JValue(value);
986
 
    }
987
 
 
988
 
    /// <summary>
989
 
    /// Performs an implicit conversion from <see cref="UInt32"/> to <see cref="JToken"/>.
990
 
    /// </summary>
991
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
992
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
993
 
    [CLSCompliant(false)]
994
 
    public static implicit operator JToken(uint value)
995
 
    {
996
 
      return new JValue(value);
997
 
    }
998
 
 
999
 
    /// <summary>
1000
 
    /// Performs an implicit conversion from <see cref="UInt64"/> to <see cref="JToken"/>.
1001
 
    /// </summary>
1002
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
1003
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
1004
 
    [CLSCompliant(false)]
1005
 
    public static implicit operator JToken(ulong value)
1006
 
    {
1007
 
      return new JValue(value);
1008
 
    }
1009
 
 
1010
 
    /// <summary>
1011
 
    /// Performs an implicit conversion from <see cref="T:System.Byte[]"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
1012
 
    /// </summary>
1013
 
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
1014
 
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
1015
 
    public static implicit operator JToken(byte[] value)
1016
 
    {
1017
 
      return new JValue(value);
1018
 
    }
1019
 
    #endregion
1020
 
 
1021
 
    IEnumerator IEnumerable.GetEnumerator()
1022
 
    {
1023
 
      return ((IEnumerable<JToken>)this).GetEnumerator();
1024
 
    }
1025
 
 
1026
 
    IEnumerator<JToken> IEnumerable<JToken>.GetEnumerator()
1027
 
    {
1028
 
      return Children().GetEnumerator();
1029
 
    }
1030
 
 
1031
 
    internal abstract int GetDeepHashCode();
1032
 
 
1033
 
    IJEnumerable<JToken> IJEnumerable<JToken>.this[object key]
1034
 
    {
1035
 
      get { return this[key]; }
1036
 
    }
1037
 
 
1038
 
    /// <summary>
1039
 
    /// Creates an <see cref="JsonReader"/> for this token.
1040
 
    /// </summary>
1041
 
    /// <returns>An <see cref="JsonReader"/> that can be used to read this token and its descendants.</returns>
1042
 
    public JsonReader CreateReader()
1043
 
    {
1044
 
      return new JTokenReader(this);
1045
 
    }
1046
 
 
1047
 
    internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer)
1048
 
    {
1049
 
      ValidationUtils.ArgumentNotNull(o, "o");
1050
 
      ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
1051
 
 
1052
 
      JToken token;
1053
 
      using (JTokenWriter jsonWriter = new JTokenWriter())
1054
 
      {
1055
 
        jsonSerializer.Serialize(jsonWriter, o);
1056
 
        token = jsonWriter.Token;
1057
 
      }
1058
 
 
1059
 
      return token;
1060
 
    }
1061
 
 
1062
 
    /// <summary>
1063
 
    /// Creates a <see cref="JToken"/> from an object.
1064
 
    /// </summary>
1065
 
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
1066
 
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
1067
 
    public static JToken FromObject(object o)
1068
 
    {
1069
 
      return FromObjectInternal(o, new JsonSerializer());
1070
 
    }
1071
 
 
1072
 
    /// <summary>
1073
 
    /// Creates a <see cref="JToken"/> from an object using the specified <see cref="JsonSerializer"/>.
1074
 
    /// </summary>
1075
 
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
1076
 
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when reading the object.</param>
1077
 
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
1078
 
    public static JToken FromObject(object o, JsonSerializer jsonSerializer)
1079
 
    {
1080
 
      return FromObjectInternal(o, jsonSerializer);
1081
 
    }
1082
 
 
1083
 
    /// <summary>
1084
 
    /// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
1085
 
    /// </summary>
1086
 
    /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
1087
 
    /// <returns>
1088
 
    /// An <see cref="JToken"/> that contains the token and its descendant tokens
1089
 
    /// that were read from the reader. The runtime type of the token is determined
1090
 
    /// by the token type of the first token encountered in the reader.
1091
 
    /// </returns>
1092
 
    public static JToken ReadFrom(JsonReader reader)
1093
 
    {
1094
 
      ValidationUtils.ArgumentNotNull(reader, "reader");
1095
 
 
1096
 
      if (reader.TokenType == JsonToken.None)
1097
 
      {
1098
 
        if (!reader.Read())
1099
 
          throw new Exception("Error reading JToken from JsonReader.");
1100
 
      }
1101
 
 
1102
 
      if (reader.TokenType == JsonToken.StartObject)
1103
 
        return JObject.Load(reader);
1104
 
 
1105
 
      if (reader.TokenType == JsonToken.StartArray)
1106
 
        return JArray.Load(reader);
1107
 
 
1108
 
      if (reader.TokenType == JsonToken.PropertyName)
1109
 
        return JProperty.Load(reader);
1110
 
 
1111
 
      if (reader.TokenType == JsonToken.StartConstructor)
1112
 
        return JConstructor.Load(reader);
1113
 
 
1114
 
      // hack. change to look at TokenType rather than using value
1115
 
      if (!JsonReader.IsStartToken(reader.TokenType))
1116
 
        return new JValue(reader.Value);
1117
 
 
1118
 
      // TODO: loading constructor and parameters?
1119
 
      throw new Exception("Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
1120
 
    }
1121
 
 
1122
 
    internal void SetLineInfo(IJsonLineInfo lineInfo)
1123
 
    {
1124
 
      if (lineInfo == null || !lineInfo.HasLineInfo())
1125
 
        return;
1126
 
 
1127
 
      SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
1128
 
    }
1129
 
 
1130
 
    internal void SetLineInfo(int lineNumber, int linePosition)
1131
 
    {
1132
 
      _lineNumber = lineNumber;
1133
 
      _linePosition = linePosition;
1134
 
    }
1135
 
 
1136
 
    bool IJsonLineInfo.HasLineInfo()
1137
 
    {
1138
 
      return (_lineNumber != null && _linePosition != null);
1139
 
    }
1140
 
 
1141
 
    int IJsonLineInfo.LineNumber
1142
 
    {
1143
 
      get { return _lineNumber ?? 0; }
1144
 
    }
1145
 
 
1146
 
    int IJsonLineInfo.LinePosition
1147
 
    {
1148
 
      get { return _linePosition ?? 0; }
1149
 
    }
1150
 
 
1151
 
    /// <summary>
1152
 
    /// Selects the token that matches the object path.
1153
 
    /// </summary>
1154
 
    /// <param name="path">
1155
 
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
1156
 
    /// to be returned. This must be a string of property names or array indexes separated
1157
 
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
1158
 
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
1159
 
    /// </param>
1160
 
    /// <returns>The <see cref="JToken"/> that matches the object path or a null reference if no matching token is found.</returns>
1161
 
    public JToken SelectToken(string path)
1162
 
    {
1163
 
      return SelectToken(path, false);
1164
 
    }
1165
 
 
1166
 
    /// <summary>
1167
 
    /// Selects the token that matches the object path.
1168
 
    /// </summary>
1169
 
    /// <param name="path">
1170
 
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
1171
 
    /// to be returned. This must be a string of property names or array indexes separated
1172
 
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
1173
 
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
1174
 
    /// </param>
1175
 
    /// <param name="errorWhenNoMatch">A flag to indicate whether an error should be thrown if no token is found.</param>
1176
 
    /// <returns>The <see cref="JToken"/> that matches the object path.</returns>
1177
 
    public JToken SelectToken(string path, bool errorWhenNoMatch)
1178
 
    {
1179
 
      JPath p = new JPath(path);
1180
 
      return p.Evaluate(this, errorWhenNoMatch);
1181
 
    }
1182
 
  }
 
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
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
29
using System.Dynamic;
 
30
using System.Linq.Expressions;
 
31
#endif
 
32
using System.IO;
 
33
using Newtonsoft.Json.Utilities;
 
34
using System.Diagnostics;
 
35
using System.Globalization;
 
36
using System.Collections;
 
37
#if NET20
 
38
using Newtonsoft.Json.Utilities.LinqBridge;
 
39
#else
 
40
using System.Linq;
 
41
#endif
 
42
 
 
43
namespace Newtonsoft.Json.Linq
 
44
{
 
45
  /// <summary>
 
46
  /// Represents an abstract JSON token.
 
47
  /// </summary>
 
48
  public abstract class JToken : IJEnumerable<JToken>, IJsonLineInfo
 
49
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
50
, ICloneable
 
51
#endif
 
52
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
53
, IDynamicMetaObjectProvider
 
54
#endif
 
55
  {
 
56
    private JContainer _parent;
 
57
    private JToken _previous;
 
58
    private JToken _next;
 
59
    private static JTokenEqualityComparer _equalityComparer;
 
60
 
 
61
    private int? _lineNumber;
 
62
    private int? _linePosition;
 
63
 
 
64
    /// <summary>
 
65
    /// Gets a comparer that can compare two tokens for value equality.
 
66
    /// </summary>
 
67
    /// <value>A <see cref="JTokenEqualityComparer"/> that can compare two nodes for value equality.</value>
 
68
    public static JTokenEqualityComparer EqualityComparer
 
69
    {
 
70
      get
 
71
      {
 
72
        if (_equalityComparer == null)
 
73
          _equalityComparer = new JTokenEqualityComparer();
 
74
 
 
75
        return _equalityComparer;
 
76
      }
 
77
    }
 
78
 
 
79
    /// <summary>
 
80
    /// Gets or sets the parent.
 
81
    /// </summary>
 
82
    /// <value>The parent.</value>
 
83
    public JContainer Parent
 
84
    {
 
85
      [DebuggerStepThrough]
 
86
      get { return _parent; }
 
87
      internal set { _parent = value; }
 
88
    }
 
89
 
 
90
    /// <summary>
 
91
    /// Gets the root <see cref="JToken"/> of this <see cref="JToken"/>.
 
92
    /// </summary>
 
93
    /// <value>The root <see cref="JToken"/> of this <see cref="JToken"/>.</value>
 
94
    public JToken Root
 
95
    {
 
96
      get
 
97
      {
 
98
        JContainer parent = Parent;
 
99
        if (parent == null)
 
100
          return this;
 
101
 
 
102
        while (parent.Parent != null)
 
103
        {
 
104
          parent = parent.Parent;
 
105
        }
 
106
 
 
107
        return parent;
 
108
      }
 
109
    }
 
110
 
 
111
    internal abstract JToken CloneToken();
 
112
    internal abstract bool DeepEquals(JToken node);
 
113
 
 
114
    /// <summary>
 
115
    /// Gets the node type for this <see cref="JToken"/>.
 
116
    /// </summary>
 
117
    /// <value>The type.</value>
 
118
    public abstract JTokenType Type { get; }
 
119
 
 
120
    /// <summary>
 
121
    /// Gets a value indicating whether this token has childen tokens.
 
122
    /// </summary>
 
123
    /// <value>
 
124
    ///         <c>true</c> if this token has child values; otherwise, <c>false</c>.
 
125
    /// </value>
 
126
    public abstract bool HasValues { get; }
 
127
 
 
128
    /// <summary>
 
129
    /// Compares the values of two tokens, including the values of all descendant tokens.
 
130
    /// </summary>
 
131
    /// <param name="t1">The first <see cref="JToken"/> to compare.</param>
 
132
    /// <param name="t2">The second <see cref="JToken"/> to compare.</param>
 
133
    /// <returns>true if the tokens are equal; otherwise false.</returns>
 
134
    public static bool DeepEquals(JToken t1, JToken t2)
 
135
    {
 
136
      return (t1 == t2 || (t1 != null && t2 != null && t1.DeepEquals(t2)));
 
137
    }
 
138
 
 
139
    /// <summary>
 
140
    /// Gets the next sibling token of this node.
 
141
    /// </summary>
 
142
    /// <value>The <see cref="JToken"/> that contains the next sibling token.</value>
 
143
    public JToken Next
 
144
    {
 
145
      get { return _next; }
 
146
      internal set { _next = value; }
 
147
    }
 
148
 
 
149
    /// <summary>
 
150
    /// Gets the previous sibling token of this node.
 
151
    /// </summary>
 
152
    /// <value>The <see cref="JToken"/> that contains the previous sibling token.</value>
 
153
    public JToken Previous
 
154
    {
 
155
      get { return _previous; }
 
156
      internal set { _previous = value; }
 
157
    }
 
158
 
 
159
    internal JToken()
 
160
    {
 
161
    }
 
162
 
 
163
    /// <summary>
 
164
    /// Adds the specified content immediately after this token.
 
165
    /// </summary>
 
166
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added after this token.</param>
 
167
    public void AddAfterSelf(object content)
 
168
    {
 
169
      if (_parent == null)
 
170
        throw new InvalidOperationException("The parent is missing.");
 
171
 
 
172
      int index = _parent.IndexOfItem(this);
 
173
      _parent.AddInternal(index + 1, content, false);
 
174
    }
 
175
 
 
176
    /// <summary>
 
177
    /// Adds the specified content immediately before this token.
 
178
    /// </summary>
 
179
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added before this token.</param>
 
180
    public void AddBeforeSelf(object content)
 
181
    {
 
182
      if (_parent == null)
 
183
        throw new InvalidOperationException("The parent is missing.");
 
184
 
 
185
      int index = _parent.IndexOfItem(this);
 
186
      _parent.AddInternal(index, content, false);
 
187
    }
 
188
 
 
189
    /// <summary>
 
190
    /// Returns a collection of the ancestor tokens of this token.
 
191
    /// </summary>
 
192
    /// <returns>A collection of the ancestor tokens of this token.</returns>
 
193
    public IEnumerable<JToken> Ancestors()
 
194
    {
 
195
      for (JToken parent = Parent; parent != null; parent = parent.Parent)
 
196
      {
 
197
        yield return parent;
 
198
      }
 
199
    }
 
200
 
 
201
    /// <summary>
 
202
    /// Returns a collection of the sibling tokens after this token, in document order.
 
203
    /// </summary>
 
204
    /// <returns>A collection of the sibling tokens after this tokens, in document order.</returns>
 
205
    public IEnumerable<JToken> AfterSelf()
 
206
    {
 
207
      if (Parent == null)
 
208
        yield break;
 
209
 
 
210
      for (JToken o = Next; o != null; o = o.Next)
 
211
      {
 
212
        yield return o;
 
213
      }
 
214
    }
 
215
 
 
216
    /// <summary>
 
217
    /// Returns a collection of the sibling tokens before this token, in document order.
 
218
    /// </summary>
 
219
    /// <returns>A collection of the sibling tokens before this token, in document order.</returns>
 
220
    public IEnumerable<JToken> BeforeSelf()
 
221
    {
 
222
      for (JToken o = Parent.First; o != this; o = o.Next)
 
223
      {
 
224
        yield return o;
 
225
      }
 
226
    }
 
227
 
 
228
    /// <summary>
 
229
    /// Gets the <see cref="JToken"/> with the specified key.
 
230
    /// </summary>
 
231
    /// <value>The <see cref="JToken"/> with the specified key.</value>
 
232
    public virtual JToken this[object key]
 
233
    {
 
234
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
235
      set { throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
236
    }
 
237
 
 
238
    /// <summary>
 
239
    /// Gets the <see cref="JToken"/> with the specified key converted to the specified type.
 
240
    /// </summary>
 
241
    /// <typeparam name="T">The type to convert the token to.</typeparam>
 
242
    /// <param name="key">The token key.</param>
 
243
    /// <returns>The converted token value.</returns>
 
244
    public virtual T Value<T>(object key)
 
245
    {
 
246
      JToken token = this[key];
 
247
 
 
248
      return Extensions.Convert<JToken, T>(token);
 
249
    }
 
250
 
 
251
    /// <summary>
 
252
    /// Get the first child token of this token.
 
253
    /// </summary>
 
254
    /// <value>A <see cref="JToken"/> containing the first child token of the <see cref="JToken"/>.</value>
 
255
    public virtual JToken First
 
256
    {
 
257
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
258
    }
 
259
 
 
260
    /// <summary>
 
261
    /// Get the last child token of this token.
 
262
    /// </summary>
 
263
    /// <value>A <see cref="JToken"/> containing the last child token of the <see cref="JToken"/>.</value>
 
264
    public virtual JToken Last
 
265
    {
 
266
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
267
    }
 
268
 
 
269
    /// <summary>
 
270
    /// Returns a collection of the child tokens of this token, in document order.
 
271
    /// </summary>
 
272
    /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
 
273
    public virtual JEnumerable<JToken> Children()
 
274
    {
 
275
      return JEnumerable<JToken>.Empty;
 
276
    }
 
277
 
 
278
    /// <summary>
 
279
    /// Returns a collection of the child tokens of this token, in document order, filtered by the specified type.
 
280
    /// </summary>
 
281
    /// <typeparam name="T">The type to filter the child tokens on.</typeparam>
 
282
    /// <returns>A <see cref="JEnumerable{T}"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
 
283
    public JEnumerable<T> Children<T>() where T : JToken
 
284
    {
 
285
      return new JEnumerable<T>(Children().OfType<T>());
 
286
    }
 
287
 
 
288
    /// <summary>
 
289
    /// Returns a collection of the child values of this token, in document order.
 
290
    /// </summary>
 
291
    /// <typeparam name="T">The type to convert the values to.</typeparam>
 
292
    /// <returns>A <see cref="IEnumerable{T}"/> containing the child values of this <see cref="JToken"/>, in document order.</returns>
 
293
    public virtual IEnumerable<T> Values<T>()
 
294
    {
 
295
      throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
 
296
    }
 
297
 
 
298
    /// <summary>
 
299
    /// Removes this token from its parent.
 
300
    /// </summary>
 
301
    public void Remove()
 
302
    {
 
303
      if (_parent == null)
 
304
        throw new InvalidOperationException("The parent is missing.");
 
305
 
 
306
      _parent.RemoveItem(this);
 
307
    }
 
308
 
 
309
    /// <summary>
 
310
    /// Replaces this token with the specified token.
 
311
    /// </summary>
 
312
    /// <param name="value">The value.</param>
 
313
    public void Replace(JToken value)
 
314
    {
 
315
      if (_parent == null)
 
316
        throw new InvalidOperationException("The parent is missing.");
 
317
 
 
318
      _parent.ReplaceItem(this, value);
 
319
    }
 
320
 
 
321
    /// <summary>
 
322
    /// Writes this token to a <see cref="JsonWriter"/>.
 
323
    /// </summary>
 
324
    /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
 
325
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
 
326
    public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
 
327
 
 
328
    /// <summary>
 
329
    /// Returns the indented JSON for this token.
 
330
    /// </summary>
 
331
    /// <returns>
 
332
    /// The indented JSON for this token.
 
333
    /// </returns>
 
334
    public override string ToString()
 
335
    {
 
336
      return ToString(Formatting.Indented);
 
337
    }
 
338
 
 
339
    /// <summary>
 
340
    /// Returns the JSON for this token using the given formatting and converters.
 
341
    /// </summary>
 
342
    /// <param name="formatting">Indicates how the output is formatted.</param>
 
343
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
 
344
    /// <returns>The JSON for this token using the given formatting and converters.</returns>
 
345
    public string ToString(Formatting formatting, params JsonConverter[] converters)
 
346
    {
 
347
      using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
 
348
      {
 
349
        JsonTextWriter jw = new JsonTextWriter(sw);
 
350
        jw.Formatting = formatting;
 
351
 
 
352
        WriteTo(jw, converters);
 
353
 
 
354
        return sw.ToString();
 
355
      }
 
356
    }
 
357
 
 
358
    private static JValue EnsureValue(JToken value)
 
359
    {
 
360
      if (value == null)
 
361
        throw new ArgumentNullException("value");
 
362
 
 
363
      if (value is JProperty)
 
364
        value = ((JProperty)value).Value;
 
365
 
 
366
      JValue v = value as JValue;
 
367
 
 
368
      return v;
 
369
    }
 
370
 
 
371
    private static string GetType(JToken token)
 
372
    {
 
373
      ValidationUtils.ArgumentNotNull(token, "token");
 
374
 
 
375
      if (token is JProperty)
 
376
        token = ((JProperty)token).Value;
 
377
 
 
378
      return token.Type.ToString();
 
379
    }
 
380
 
 
381
    private static bool IsNullable(JToken o)
 
382
    {
 
383
      return (o.Type == JTokenType.Undefined || o.Type == JTokenType.Null);
 
384
    }
 
385
 
 
386
    private static bool ValidateFloat(JToken o, bool nullable)
 
387
    {
 
388
      return (o.Type == JTokenType.Float || o.Type == JTokenType.Integer || (nullable && IsNullable(o)));
 
389
    }
 
390
 
 
391
    private static bool ValidateInteger(JToken o, bool nullable)
 
392
    {
 
393
      return (o.Type == JTokenType.Integer || (nullable && IsNullable(o)));
 
394
    }
 
395
 
 
396
    private static bool ValidateDate(JToken o, bool nullable)
 
397
    {
 
398
      return (o.Type == JTokenType.Date || (nullable && IsNullable(o)));
 
399
    }
 
400
 
 
401
    private static bool ValidateBoolean(JToken o, bool nullable)
 
402
    {
 
403
      return (o.Type == JTokenType.Boolean || (nullable && IsNullable(o)));
 
404
    }
 
405
 
 
406
    private static bool ValidateString(JToken o)
 
407
    {
 
408
      return (o.Type == JTokenType.String || o.Type == JTokenType.Comment || o.Type == JTokenType.Raw || IsNullable(o));
 
409
    }
 
410
 
 
411
    private static bool ValidateBytes(JToken o)
 
412
    {
 
413
      return (o.Type == JTokenType.Bytes || IsNullable(o));
 
414
    }
 
415
 
 
416
    #region Cast from operators
 
417
    /// <summary>
 
418
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Boolean"/>.
 
419
    /// </summary>
 
420
    /// <param name="value">The value.</param>
 
421
    /// <returns>The result of the conversion.</returns>
 
422
    public static explicit operator bool(JToken value)
 
423
    {
 
424
      JValue v = EnsureValue(value);
 
425
      if (v == null || !ValidateBoolean(v, false))
 
426
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
427
 
 
428
      return Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture);
 
429
    }
 
430
 
 
431
#if !PocketPC && !NET20
 
432
    /// <summary>
 
433
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTimeOffset"/>.
 
434
    /// </summary>
 
435
    /// <param name="value">The value.</param>
 
436
    /// <returns>The result of the conversion.</returns>
 
437
    public static explicit operator DateTimeOffset(JToken value)
 
438
    {
 
439
      JValue v = EnsureValue(value);
 
440
      if (v == null || !ValidateDate(v, false))
 
441
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
442
 
 
443
      return (DateTimeOffset)v.Value;
 
444
    }
 
445
#endif
 
446
 
 
447
    /// <summary>
 
448
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Boolean}"/>.
 
449
    /// </summary>
 
450
    /// <param name="value">The value.</param>
 
451
    /// <returns>The result of the conversion.</returns>
 
452
    public static explicit operator bool?(JToken value)
 
453
    {
 
454
      if (value == null)
 
455
        return null;
 
456
 
 
457
      JValue v = EnsureValue(value);
 
458
      if (v == null || !ValidateBoolean(v, true))
 
459
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
460
 
 
461
      return (v.Value != null) ? (bool?)Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture) : null;
 
462
    }
 
463
 
 
464
    /// <summary>
 
465
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int64"/>.
 
466
    /// </summary>
 
467
    /// <param name="value">The value.</param>
 
468
    /// <returns>The result of the conversion.</returns>
 
469
    public static explicit operator long(JToken value)
 
470
    {
 
471
      JValue v = EnsureValue(value);
 
472
      if (v == null || !ValidateInteger(v, false))
 
473
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
474
 
 
475
      return Convert.ToInt64(v.Value, CultureInfo.InvariantCulture);
 
476
    }
 
477
 
 
478
    /// <summary>
 
479
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTime}"/>.
 
480
    /// </summary>
 
481
    /// <param name="value">The value.</param>
 
482
    /// <returns>The result of the conversion.</returns>
 
483
    public static explicit operator DateTime?(JToken value)
 
484
    {
 
485
      if (value == null)
 
486
        return null;
 
487
 
 
488
      JValue v = EnsureValue(value);
 
489
      if (v == null || !ValidateDate(v, true))
 
490
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
491
 
 
492
      return (v.Value != null) ? (DateTime?)Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture) : null;
 
493
    }
 
494
 
 
495
#if !PocketPC && !NET20
 
496
    /// <summary>
 
497
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTimeOffset}"/>.
 
498
    /// </summary>
 
499
    /// <param name="value">The value.</param>
 
500
    /// <returns>The result of the conversion.</returns>
 
501
    public static explicit operator DateTimeOffset?(JToken value)
 
502
    {
 
503
      if (value == null)
 
504
        return null;
 
505
 
 
506
      JValue v = EnsureValue(value);
 
507
      if (v == null || !ValidateDate(v, true))
 
508
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
509
 
 
510
      return (DateTimeOffset?)v.Value;
 
511
    }
 
512
#endif
 
513
 
 
514
    /// <summary>
 
515
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Decimal}"/>.
 
516
    /// </summary>
 
517
    /// <param name="value">The value.</param>
 
518
    /// <returns>The result of the conversion.</returns>
 
519
    public static explicit operator decimal?(JToken value)
 
520
    {
 
521
      if (value == null)
 
522
        return null;
 
523
 
 
524
      JValue v = EnsureValue(value);
 
525
      if (v == null || !ValidateFloat(v, true))
 
526
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
527
 
 
528
      return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;
 
529
    }
 
530
 
 
531
    /// <summary>
 
532
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Double}"/>.
 
533
    /// </summary>
 
534
    /// <param name="value">The value.</param>
 
535
    /// <returns>The result of the conversion.</returns>
 
536
    public static explicit operator double?(JToken value)
 
537
    {
 
538
      if (value == null)
 
539
        return null;
 
540
 
 
541
      JValue v = EnsureValue(value);
 
542
      if (v == null || !ValidateFloat(v, true))
 
543
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
544
 
 
545
      return (double?)v.Value;
 
546
    }
 
547
 
 
548
    /// <summary>
 
549
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int32"/>.
 
550
    /// </summary>
 
551
    /// <param name="value">The value.</param>
 
552
    /// <returns>The result of the conversion.</returns>
 
553
    public static explicit operator int(JToken value)
 
554
    {
 
555
      JValue v = EnsureValue(value);
 
556
      if (v == null || !ValidateInteger(v, false))
 
557
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
558
 
 
559
      return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
 
560
    }
 
561
 
 
562
    /// <summary>
 
563
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int16"/>.
 
564
    /// </summary>
 
565
    /// <param name="value">The value.</param>
 
566
    /// <returns>The result of the conversion.</returns>
 
567
    public static explicit operator short(JToken value)
 
568
    {
 
569
      JValue v = EnsureValue(value);
 
570
      if (v == null || !ValidateInteger(v, false))
 
571
        throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
572
 
 
573
      return Convert.ToInt16(v.Value, CultureInfo.InvariantCulture);
 
574
    }
 
575
 
 
576
    /// <summary>
 
577
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt16"/>.
 
578
    /// </summary>
 
579
    /// <param name="value">The value.</param>
 
580
    /// <returns>The result of the conversion.</returns>
 
581
    [CLSCompliant(false)]
 
582
    public static explicit operator ushort(JToken value)
 
583
    {
 
584
      JValue v = EnsureValue(value);
 
585
      if (v == null || !ValidateInteger(v, false))
 
586
        throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
587
 
 
588
      return Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture);
 
589
    }
 
590
 
 
591
    /// <summary>
 
592
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int32}"/>.
 
593
    /// </summary>
 
594
    /// <param name="value">The value.</param>
 
595
    /// <returns>The result of the conversion.</returns>
 
596
    public static explicit operator int?(JToken value)
 
597
    {
 
598
      if (value == null)
 
599
        return null;
 
600
 
 
601
      JValue v = EnsureValue(value);
 
602
      if (v == null || !ValidateInteger(v, true))
 
603
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
604
 
 
605
      return (v.Value != null) ? (int?)Convert.ToInt32(v.Value, CultureInfo.InvariantCulture) : null;
 
606
    }
 
607
 
 
608
    /// <summary>
 
609
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int16}"/>.
 
610
    /// </summary>
 
611
    /// <param name="value">The value.</param>
 
612
    /// <returns>The result of the conversion.</returns>
 
613
    public static explicit operator short?(JToken value)
 
614
    {
 
615
      if (value == null)
 
616
        return null;
 
617
 
 
618
      JValue v = EnsureValue(value);
 
619
      if (v == null || !ValidateInteger(v, true))
 
620
        throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
621
 
 
622
      return (v.Value != null) ? (short?)Convert.ToInt16(v.Value, CultureInfo.InvariantCulture) : null;
 
623
    }
 
624
 
 
625
    /// <summary>
 
626
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt16}"/>.
 
627
    /// </summary>
 
628
    /// <param name="value">The value.</param>
 
629
    /// <returns>The result of the conversion.</returns>
 
630
    [CLSCompliant(false)]
 
631
    public static explicit operator ushort?(JToken value)
 
632
    {
 
633
      if (value == null)
 
634
        return null;
 
635
 
 
636
      JValue v = EnsureValue(value);
 
637
      if (v == null || !ValidateInteger(v, true))
 
638
        throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
639
 
 
640
      return (v.Value != null) ? (ushort?)Convert.ToInt16(v.Value, CultureInfo.InvariantCulture) : null;
 
641
    }
 
642
 
 
643
    /// <summary>
 
644
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTime"/>.
 
645
    /// </summary>
 
646
    /// <param name="value">The value.</param>
 
647
    /// <returns>The result of the conversion.</returns>
 
648
    public static explicit operator DateTime(JToken value)
 
649
    {
 
650
      JValue v = EnsureValue(value);
 
651
      if (v == null || !ValidateDate(v, false))
 
652
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
653
 
 
654
      return Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture);
 
655
    }
 
656
 
 
657
    /// <summary>
 
658
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int64}"/>.
 
659
    /// </summary>
 
660
    /// <param name="value">The value.</param>
 
661
    /// <returns>The result of the conversion.</returns>
 
662
    public static explicit operator long?(JToken value)
 
663
    {
 
664
      if (value == null)
 
665
        return null;
 
666
 
 
667
      JValue v = EnsureValue(value);
 
668
      if (v == null || !ValidateInteger(v, true))
 
669
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
670
 
 
671
      return (v.Value != null) ? (long?)Convert.ToInt64(v.Value, CultureInfo.InvariantCulture) : null;
 
672
    }
 
673
 
 
674
    /// <summary>
 
675
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Single}"/>.
 
676
    /// </summary>
 
677
    /// <param name="value">The value.</param>
 
678
    /// <returns>The result of the conversion.</returns>
 
679
    public static explicit operator float?(JToken value)
 
680
    {
 
681
      if (value == null)
 
682
        return null;
 
683
 
 
684
      JValue v = EnsureValue(value);
 
685
      if (v == null || !ValidateFloat(v, true))
 
686
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
687
 
 
688
      return (v.Value != null) ? (float?)Convert.ToSingle(v.Value, CultureInfo.InvariantCulture) : null;
 
689
    }
 
690
 
 
691
    /// <summary>
 
692
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Decimal"/>.
 
693
    /// </summary>
 
694
    /// <param name="value">The value.</param>
 
695
    /// <returns>The result of the conversion.</returns>
 
696
    public static explicit operator decimal(JToken value)
 
697
    {
 
698
      JValue v = EnsureValue(value);
 
699
      if (v == null || !ValidateFloat(v, false))
 
700
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
701
 
 
702
      return Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture);
 
703
    }
 
704
 
 
705
    /// <summary>
 
706
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt32}"/>.
 
707
    /// </summary>
 
708
    /// <param name="value">The value.</param>
 
709
    /// <returns>The result of the conversion.</returns>
 
710
    [CLSCompliant(false)]
 
711
    public static explicit operator uint?(JToken value)
 
712
    {
 
713
      if (value == null)
 
714
        return null;
 
715
 
 
716
      JValue v = EnsureValue(value);
 
717
      if (v == null || !ValidateInteger(v, true))
 
718
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
719
 
 
720
      return (v.Value != null) ? (uint?)Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture) : null;
 
721
    }
 
722
 
 
723
    /// <summary>
 
724
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt64}"/>.
 
725
    /// </summary>
 
726
    /// <param name="value">The value.</param>
 
727
    /// <returns>The result of the conversion.</returns>
 
728
    [CLSCompliant(false)]
 
729
    public static explicit operator ulong?(JToken value)
 
730
    {
 
731
      if (value == null)
 
732
        return null;
 
733
 
 
734
      JValue v = EnsureValue(value);
 
735
      if (v == null || !ValidateInteger(v, true))
 
736
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
737
 
 
738
      return (v.Value != null) ? (ulong?)Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture) : null;
 
739
    }
 
740
 
 
741
    /// <summary>
 
742
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Double"/>.
 
743
    /// </summary>
 
744
    /// <param name="value">The value.</param>
 
745
    /// <returns>The result of the conversion.</returns>
 
746
    public static explicit operator double(JToken value)
 
747
    {
 
748
      JValue v = EnsureValue(value);
 
749
      if (v == null || !ValidateFloat(v, false))
 
750
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
751
 
 
752
      return Convert.ToDouble(v.Value, CultureInfo.InvariantCulture);
 
753
    }
 
754
 
 
755
    /// <summary>
 
756
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Single"/>.
 
757
    /// </summary>
 
758
    /// <param name="value">The value.</param>
 
759
    /// <returns>The result of the conversion.</returns>
 
760
    public static explicit operator float(JToken value)
 
761
    {
 
762
      JValue v = EnsureValue(value);
 
763
      if (v == null || !ValidateFloat(v, false))
 
764
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
765
 
 
766
      return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
 
767
    }
 
768
 
 
769
    /// <summary>
 
770
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.String"/>.
 
771
    /// </summary>
 
772
    /// <param name="value">The value.</param>
 
773
    /// <returns>The result of the conversion.</returns>
 
774
    public static explicit operator string(JToken value)
 
775
    {
 
776
      if (value == null)
 
777
        return null;
 
778
 
 
779
      JValue v = EnsureValue(value);
 
780
      if (v == null || !ValidateString(v))
 
781
        throw new ArgumentException("Can not convert {0} to String.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
782
 
 
783
      return (v.Value != null) ? Convert.ToString(v.Value, CultureInfo.InvariantCulture) : null;
 
784
    }
 
785
 
 
786
    /// <summary>
 
787
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt32"/>.
 
788
    /// </summary>
 
789
    /// <param name="value">The value.</param>
 
790
    /// <returns>The result of the conversion.</returns>
 
791
    [CLSCompliant(false)]
 
792
    public static explicit operator uint(JToken value)
 
793
    {
 
794
      JValue v = EnsureValue(value);
 
795
      if (v == null || !ValidateInteger(v, false))
 
796
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
797
 
 
798
      return Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture);
 
799
    }
 
800
 
 
801
    /// <summary>
 
802
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt64"/>.
 
803
    /// </summary>
 
804
    /// <param name="value">The value.</param>
 
805
    /// <returns>The result of the conversion.</returns>
 
806
    [CLSCompliant(false)]
 
807
    public static explicit operator ulong(JToken value)
 
808
    {
 
809
      JValue v = EnsureValue(value);
 
810
      if (v == null || !ValidateInteger(v, false))
 
811
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
812
 
 
813
      return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
 
814
    }
 
815
 
 
816
    /// <summary>
 
817
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="T:System.Byte[]"/>.
 
818
    /// </summary>
 
819
    /// <param name="value">The value.</param>
 
820
    /// <returns>The result of the conversion.</returns>
 
821
    public static explicit operator byte[](JToken value)
 
822
    {
 
823
      JValue v = EnsureValue(value);
 
824
      if (v == null || !ValidateBytes(v))
 
825
        throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
826
 
 
827
      return (byte[])v.Value;
 
828
    }
 
829
    #endregion
 
830
 
 
831
    #region Cast to operators
 
832
    /// <summary>
 
833
    /// Performs an implicit conversion from <see cref="Boolean"/> to <see cref="JToken"/>.
 
834
    /// </summary>
 
835
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
836
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
837
    public static implicit operator JToken(bool value)
 
838
    {
 
839
      return new JValue(value);
 
840
    }
 
841
 
 
842
#if !PocketPC && !NET20
 
843
    /// <summary>
 
844
    /// Performs an implicit conversion from <see cref="DateTimeOffset"/> to <see cref="JToken"/>.
 
845
    /// </summary>
 
846
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
847
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
848
    public static implicit operator JToken(DateTimeOffset value)
 
849
    {
 
850
      return new JValue(value);
 
851
    }
 
852
#endif
 
853
 
 
854
    /// <summary>
 
855
    /// Performs an implicit conversion from <see cref="Nullable{Boolean}"/> to <see cref="JToken"/>.
 
856
    /// </summary>
 
857
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
858
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
859
    public static implicit operator JToken(bool? value)
 
860
    {
 
861
      return new JValue(value);
 
862
    }
 
863
 
 
864
    /// <summary>
 
865
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
 
866
    /// </summary>
 
867
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
868
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
869
    public static implicit operator JToken(long value)
 
870
    {
 
871
      return new JValue(value);
 
872
    }
 
873
 
 
874
    /// <summary>
 
875
    /// Performs an implicit conversion from <see cref="Nullable{DateTime}"/> to <see cref="JToken"/>.
 
876
    /// </summary>
 
877
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
878
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
879
    public static implicit operator JToken(DateTime? value)
 
880
    {
 
881
      return new JValue(value);
 
882
    }
 
883
 
 
884
#if !PocketPC && !NET20
 
885
    /// <summary>
 
886
    /// Performs an implicit conversion from <see cref="Nullable{DateTimeOffset}"/> to <see cref="JToken"/>.
 
887
    /// </summary>
 
888
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
889
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
890
    public static implicit operator JToken(DateTimeOffset? value)
 
891
    {
 
892
      return new JValue(value);
 
893
    }
 
894
#endif
 
895
 
 
896
    /// <summary>
 
897
    /// Performs an implicit conversion from <see cref="Nullable{Decimal}"/> to <see cref="JToken"/>.
 
898
    /// </summary>
 
899
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
900
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
901
    public static implicit operator JToken(decimal? value)
 
902
    {
 
903
      return new JValue(value);
 
904
    }
 
905
 
 
906
    /// <summary>
 
907
    /// Performs an implicit conversion from <see cref="Nullable{Double}"/> to <see cref="JToken"/>.
 
908
    /// </summary>
 
909
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
910
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
911
    public static implicit operator JToken(double? value)
 
912
    {
 
913
      return new JValue(value);
 
914
    }
 
915
 
 
916
    /// <summary>
 
917
    /// Performs an implicit conversion from <see cref="Int16"/> to <see cref="JToken"/>.
 
918
    /// </summary>
 
919
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
920
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
921
    [CLSCompliant(false)]
 
922
    public static implicit operator JToken(short value)
 
923
    {
 
924
      return new JValue(value);
 
925
    }
 
926
 
 
927
    /// <summary>
 
928
    /// Performs an implicit conversion from <see cref="UInt16"/> to <see cref="JToken"/>.
 
929
    /// </summary>
 
930
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
931
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
932
    [CLSCompliant(false)]
 
933
    public static implicit operator JToken(ushort value)
 
934
    {
 
935
      return new JValue(value);
 
936
    }
 
937
 
 
938
    /// <summary>
 
939
    /// Performs an implicit conversion from <see cref="Int32"/> to <see cref="JToken"/>.
 
940
    /// </summary>
 
941
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
942
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
943
    public static implicit operator JToken(int value)
 
944
    {
 
945
      return new JValue(value);
 
946
    }
 
947
 
 
948
    /// <summary>
 
949
    /// Performs an implicit conversion from <see cref="Nullable{Int32}"/> to <see cref="JToken"/>.
 
950
    /// </summary>
 
951
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
952
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
953
    public static implicit operator JToken(int? value)
 
954
    {
 
955
      return new JValue(value);
 
956
    }
 
957
 
 
958
    /// <summary>
 
959
    /// Performs an implicit conversion from <see cref="DateTime"/> to <see cref="JToken"/>.
 
960
    /// </summary>
 
961
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
962
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
963
    public static implicit operator JToken(DateTime value)
 
964
    {
 
965
      return new JValue(value);
 
966
    }
 
967
 
 
968
    /// <summary>
 
969
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
 
970
    /// </summary>
 
971
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
972
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
973
    public static implicit operator JToken(long? value)
 
974
    {
 
975
      return new JValue(value);
 
976
    }
 
977
 
 
978
    /// <summary>
 
979
    /// Performs an implicit conversion from <see cref="Nullable{Single}"/> to <see cref="JToken"/>.
 
980
    /// </summary>
 
981
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
982
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
983
    public static implicit operator JToken(float? value)
 
984
    {
 
985
      return new JValue(value);
 
986
    }
 
987
 
 
988
    /// <summary>
 
989
    /// Performs an implicit conversion from <see cref="Decimal"/> to <see cref="JToken"/>.
 
990
    /// </summary>
 
991
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
992
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
993
    public static implicit operator JToken(decimal value)
 
994
    {
 
995
      return new JValue(value);
 
996
    }
 
997
 
 
998
    /// <summary>
 
999
    /// Performs an implicit conversion from <see cref="Nullable{Int16}"/> to <see cref="JToken"/>.
 
1000
    /// </summary>
 
1001
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1002
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1003
    [CLSCompliant(false)]
 
1004
    public static implicit operator JToken(short? value)
 
1005
    {
 
1006
      return new JValue(value);
 
1007
    }
 
1008
 
 
1009
    /// <summary>
 
1010
    /// Performs an implicit conversion from <see cref="Nullable{UInt16}"/> to <see cref="JToken"/>.
 
1011
    /// </summary>
 
1012
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1013
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1014
    [CLSCompliant(false)]
 
1015
    public static implicit operator JToken(ushort? value)
 
1016
    {
 
1017
      return new JValue(value);
 
1018
    }
 
1019
 
 
1020
    /// <summary>
 
1021
    /// Performs an implicit conversion from <see cref="Nullable{UInt32}"/> to <see cref="JToken"/>.
 
1022
    /// </summary>
 
1023
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1024
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1025
    [CLSCompliant(false)]
 
1026
    public static implicit operator JToken(uint? value)
 
1027
    {
 
1028
      return new JValue(value);
 
1029
    }
 
1030
 
 
1031
    /// <summary>
 
1032
    /// Performs an implicit conversion from <see cref="Nullable{UInt64}"/> to <see cref="JToken"/>.
 
1033
    /// </summary>
 
1034
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1035
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1036
    [CLSCompliant(false)]
 
1037
    public static implicit operator JToken(ulong? value)
 
1038
    {
 
1039
      return new JValue(value);
 
1040
    }
 
1041
 
 
1042
    /// <summary>
 
1043
    /// Performs an implicit conversion from <see cref="Double"/> to <see cref="JToken"/>.
 
1044
    /// </summary>
 
1045
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1046
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1047
    public static implicit operator JToken(double value)
 
1048
    {
 
1049
      return new JValue(value);
 
1050
    }
 
1051
 
 
1052
    /// <summary>
 
1053
    /// Performs an implicit conversion from <see cref="Single"/> to <see cref="JToken"/>.
 
1054
    /// </summary>
 
1055
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1056
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1057
    public static implicit operator JToken(float value)
 
1058
    {
 
1059
      return new JValue(value);
 
1060
    }
 
1061
 
 
1062
    /// <summary>
 
1063
    /// Performs an implicit conversion from <see cref="String"/> to <see cref="JToken"/>.
 
1064
    /// </summary>
 
1065
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1066
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1067
    public static implicit operator JToken(string value)
 
1068
    {
 
1069
      return new JValue(value);
 
1070
    }
 
1071
 
 
1072
    /// <summary>
 
1073
    /// Performs an implicit conversion from <see cref="UInt32"/> to <see cref="JToken"/>.
 
1074
    /// </summary>
 
1075
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1076
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1077
    [CLSCompliant(false)]
 
1078
    public static implicit operator JToken(uint value)
 
1079
    {
 
1080
      return new JValue(value);
 
1081
    }
 
1082
 
 
1083
    /// <summary>
 
1084
    /// Performs an implicit conversion from <see cref="UInt64"/> to <see cref="JToken"/>.
 
1085
    /// </summary>
 
1086
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1087
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1088
    [CLSCompliant(false)]
 
1089
    public static implicit operator JToken(ulong value)
 
1090
    {
 
1091
      return new JValue(value);
 
1092
    }
 
1093
 
 
1094
    /// <summary>
 
1095
    /// Performs an implicit conversion from <see cref="T:System.Byte[]"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1096
    /// </summary>
 
1097
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1098
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1099
    public static implicit operator JToken(byte[] value)
 
1100
    {
 
1101
      return new JValue(value);
 
1102
    }
 
1103
    #endregion
 
1104
 
 
1105
    IEnumerator IEnumerable.GetEnumerator()
 
1106
    {
 
1107
      return ((IEnumerable<JToken>)this).GetEnumerator();
 
1108
    }
 
1109
 
 
1110
    IEnumerator<JToken> IEnumerable<JToken>.GetEnumerator()
 
1111
    {
 
1112
      return Children().GetEnumerator();
 
1113
    }
 
1114
 
 
1115
    internal abstract int GetDeepHashCode();
 
1116
 
 
1117
    IJEnumerable<JToken> IJEnumerable<JToken>.this[object key]
 
1118
    {
 
1119
      get { return this[key]; }
 
1120
    }
 
1121
 
 
1122
    /// <summary>
 
1123
    /// Creates an <see cref="JsonReader"/> for this token.
 
1124
    /// </summary>
 
1125
    /// <returns>An <see cref="JsonReader"/> that can be used to read this token and its descendants.</returns>
 
1126
    public JsonReader CreateReader()
 
1127
    {
 
1128
      return new JTokenReader(this);
 
1129
    }
 
1130
 
 
1131
    internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer)
 
1132
    {
 
1133
      ValidationUtils.ArgumentNotNull(o, "o");
 
1134
      ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
 
1135
 
 
1136
      JToken token;
 
1137
      using (JTokenWriter jsonWriter = new JTokenWriter())
 
1138
      {
 
1139
        jsonSerializer.Serialize(jsonWriter, o);
 
1140
        token = jsonWriter.Token;
 
1141
      }
 
1142
 
 
1143
      return token;
 
1144
    }
 
1145
 
 
1146
    /// <summary>
 
1147
    /// Creates a <see cref="JToken"/> from an object.
 
1148
    /// </summary>
 
1149
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
 
1150
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
 
1151
    public static JToken FromObject(object o)
 
1152
    {
 
1153
      return FromObjectInternal(o, new JsonSerializer());
 
1154
    }
 
1155
 
 
1156
    /// <summary>
 
1157
    /// Creates a <see cref="JToken"/> from an object using the specified <see cref="JsonSerializer"/>.
 
1158
    /// </summary>
 
1159
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
 
1160
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when reading the object.</param>
 
1161
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
 
1162
    public static JToken FromObject(object o, JsonSerializer jsonSerializer)
 
1163
    {
 
1164
      return FromObjectInternal(o, jsonSerializer);
 
1165
    }
 
1166
 
 
1167
    /// <summary>
 
1168
    /// Creates the specified .NET type from the <see cref="JToken"/>.
 
1169
    /// </summary>
 
1170
    /// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
 
1171
    /// <returns>The new object created from the JSON value.</returns>
 
1172
    public T ToObject<T>()
 
1173
    {
 
1174
      return ToObject<T>(new JsonSerializer());
 
1175
    }
 
1176
 
 
1177
    /// <summary>
 
1178
    /// Creates the specified .NET type from the <see cref="JToken"/> using the specified <see cref="JsonSerializer"/>.
 
1179
    /// </summary>
 
1180
    /// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
 
1181
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when creating the object.</param>
 
1182
    /// <returns>The new object created from the JSON value.</returns>
 
1183
    public T ToObject<T>(JsonSerializer jsonSerializer)
 
1184
    {
 
1185
      ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
 
1186
 
 
1187
      using (JTokenReader jsonReader = new JTokenReader(this))
 
1188
      {
 
1189
        return jsonSerializer.Deserialize<T>(jsonReader);
 
1190
      }
 
1191
    }
 
1192
 
 
1193
    /// <summary>
 
1194
    /// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
 
1195
    /// </summary>
 
1196
    /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
 
1197
    /// <returns>
 
1198
    /// An <see cref="JToken"/> that contains the token and its descendant tokens
 
1199
    /// that were read from the reader. The runtime type of the token is determined
 
1200
    /// by the token type of the first token encountered in the reader.
 
1201
    /// </returns>
 
1202
    public static JToken ReadFrom(JsonReader reader)
 
1203
    {
 
1204
      ValidationUtils.ArgumentNotNull(reader, "reader");
 
1205
 
 
1206
      if (reader.TokenType == JsonToken.None)
 
1207
      {
 
1208
        if (!reader.Read())
 
1209
          throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
 
1210
      }
 
1211
 
 
1212
      if (reader.TokenType == JsonToken.StartObject)
 
1213
        return JObject.Load(reader);
 
1214
 
 
1215
      if (reader.TokenType == JsonToken.StartArray)
 
1216
        return JArray.Load(reader);
 
1217
 
 
1218
      if (reader.TokenType == JsonToken.PropertyName)
 
1219
        return JProperty.Load(reader);
 
1220
 
 
1221
      if (reader.TokenType == JsonToken.StartConstructor)
 
1222
        return JConstructor.Load(reader);
 
1223
 
 
1224
      if (!JsonReader.IsStartToken(reader.TokenType))
 
1225
        return new JValue(reader.Value);
 
1226
 
 
1227
      throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
 
1228
    }
 
1229
 
 
1230
    /// <summary>
 
1231
    /// Load a <see cref="JToken"/> from a string that contains JSON.
 
1232
    /// </summary>
 
1233
    /// <param name="json">A <see cref="String"/> that contains JSON.</param>
 
1234
    /// <returns>A <see cref="JToken"/> populated from the string that contains JSON.</returns>
 
1235
    public static JToken Parse(string json)
 
1236
    {
 
1237
      JsonReader reader = new JsonTextReader(new StringReader(json));
 
1238
 
 
1239
      JToken t = Load(reader);
 
1240
 
 
1241
      if (reader.Read() && reader.TokenType != JsonToken.Comment)
 
1242
        throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
 
1243
 
 
1244
      return t;
 
1245
    }
 
1246
 
 
1247
    /// <summary>
 
1248
    /// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
 
1249
    /// </summary>
 
1250
    /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
 
1251
    /// <returns>
 
1252
    /// An <see cref="JToken"/> that contains the token and its descendant tokens
 
1253
    /// that were read from the reader. The runtime type of the token is determined
 
1254
    /// by the token type of the first token encountered in the reader.
 
1255
    /// </returns>
 
1256
    public static JToken Load(JsonReader reader)
 
1257
    {
 
1258
      return ReadFrom(reader);
 
1259
    }
 
1260
 
 
1261
    internal void SetLineInfo(IJsonLineInfo lineInfo)
 
1262
    {
 
1263
      if (lineInfo == null || !lineInfo.HasLineInfo())
 
1264
        return;
 
1265
 
 
1266
      SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
 
1267
    }
 
1268
 
 
1269
    internal void SetLineInfo(int lineNumber, int linePosition)
 
1270
    {
 
1271
      _lineNumber = lineNumber;
 
1272
      _linePosition = linePosition;
 
1273
    }
 
1274
 
 
1275
    bool IJsonLineInfo.HasLineInfo()
 
1276
    {
 
1277
      return (_lineNumber != null && _linePosition != null);
 
1278
    }
 
1279
 
 
1280
    int IJsonLineInfo.LineNumber
 
1281
    {
 
1282
      get { return _lineNumber ?? 0; }
 
1283
    }
 
1284
 
 
1285
    int IJsonLineInfo.LinePosition
 
1286
    {
 
1287
      get { return _linePosition ?? 0; }
 
1288
    }
 
1289
 
 
1290
    /// <summary>
 
1291
    /// Selects the token that matches the object path.
 
1292
    /// </summary>
 
1293
    /// <param name="path">
 
1294
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
 
1295
    /// to be returned. This must be a string of property names or array indexes separated
 
1296
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
 
1297
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
 
1298
    /// </param>
 
1299
    /// <returns>The <see cref="JToken"/> that matches the object path or a null reference if no matching token is found.</returns>
 
1300
    public JToken SelectToken(string path)
 
1301
    {
 
1302
      return SelectToken(path, false);
 
1303
    }
 
1304
 
 
1305
    /// <summary>
 
1306
    /// Selects the token that matches the object path.
 
1307
    /// </summary>
 
1308
    /// <param name="path">
 
1309
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
 
1310
    /// to be returned. This must be a string of property names or array indexes separated
 
1311
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
 
1312
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
 
1313
    /// </param>
 
1314
    /// <param name="errorWhenNoMatch">A flag to indicate whether an error should be thrown if no token is found.</param>
 
1315
    /// <returns>The <see cref="JToken"/> that matches the object path.</returns>
 
1316
    public JToken SelectToken(string path, bool errorWhenNoMatch)
 
1317
    {
 
1318
      JPath p = new JPath(path);
 
1319
      return p.Evaluate(this, errorWhenNoMatch);
 
1320
    }
 
1321
 
 
1322
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
1323
    /// <summary>
 
1324
    /// Returns the <see cref="T:System.Dynamic.DynamicMetaObject"/> responsible for binding operations performed on this object.
 
1325
    /// </summary>
 
1326
    /// <param name="parameter">The expression tree representation of the runtime value.</param>
 
1327
    /// <returns>
 
1328
    /// The <see cref="T:System.Dynamic.DynamicMetaObject"/> to bind this object.
 
1329
    /// </returns>
 
1330
    protected virtual DynamicMetaObject GetMetaObject(Expression parameter)
 
1331
    {
 
1332
      return new DynamicProxyMetaObject<JToken>(parameter, this, new DynamicProxy<JToken>(), true);
 
1333
    }
 
1334
 
 
1335
    /// <summary>
 
1336
    /// Returns the <see cref="T:System.Dynamic.DynamicMetaObject"/> responsible for binding operations performed on this object.
 
1337
    /// </summary>
 
1338
    /// <param name="parameter">The expression tree representation of the runtime value.</param>
 
1339
    /// <returns>
 
1340
    /// The <see cref="T:System.Dynamic.DynamicMetaObject"/> to bind this object.
 
1341
    /// </returns>
 
1342
    DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
 
1343
    {
 
1344
      return GetMetaObject(parameter);
 
1345
    }
 
1346
#endif
 
1347
 
 
1348
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
1349
    object ICloneable.Clone()
 
1350
    {
 
1351
      return DeepClone();
 
1352
    }
 
1353
#endif
 
1354
 
 
1355
    /// <summary>
 
1356
    /// Creates a new instance of the <see cref="JToken"/>. All child tokens are recursively cloned.
 
1357
    /// </summary>
 
1358
    /// <returns>A new instance of the <see cref="JToken"/>.</returns>
 
1359
    public JToken DeepClone()
 
1360
    {
 
1361
      return CloneToken();
 
1362
    }
 
1363
  }
1183
1364
}
 
 
b'\\ No newline at end of file'