~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Linq/JToken.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

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
#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
    private static readonly JTokenType[] BooleanTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
 
65
    private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean };
 
66
    private static readonly JTokenType[] StringTypes = new[] { JTokenType.Date, JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean, JTokenType.Bytes, JTokenType.Guid, JTokenType.TimeSpan, JTokenType.Uri };
 
67
    private static readonly JTokenType[] GuidTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Guid };
 
68
    private static readonly JTokenType[] TimeSpanTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.TimeSpan };
 
69
    private static readonly JTokenType[] UriTypes = new[] { JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Uri };
 
70
    private static readonly JTokenType[] CharTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
 
71
    private static readonly JTokenType[] DateTimeTypes = new[] { JTokenType.Date, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
 
72
    private static readonly JTokenType[] BytesTypes = new[] { JTokenType.Bytes, JTokenType.String, JTokenType.Comment, JTokenType.Raw };
 
73
 
 
74
    /// <summary>
 
75
    /// Gets a comparer that can compare two tokens for value equality.
 
76
    /// </summary>
 
77
    /// <value>A <see cref="JTokenEqualityComparer"/> that can compare two nodes for value equality.</value>
 
78
    public static JTokenEqualityComparer EqualityComparer
 
79
    {
 
80
      get
 
81
      {
 
82
        if (_equalityComparer == null)
 
83
          _equalityComparer = new JTokenEqualityComparer();
 
84
 
 
85
        return _equalityComparer;
 
86
      }
 
87
    }
 
88
 
 
89
    /// <summary>
 
90
    /// Gets or sets the parent.
 
91
    /// </summary>
 
92
    /// <value>The parent.</value>
 
93
    public JContainer Parent
 
94
    {
 
95
      [DebuggerStepThrough]
 
96
      get { return _parent; }
 
97
      internal set { _parent = value; }
 
98
    }
 
99
 
 
100
    /// <summary>
 
101
    /// Gets the root <see cref="JToken"/> of this <see cref="JToken"/>.
 
102
    /// </summary>
 
103
    /// <value>The root <see cref="JToken"/> of this <see cref="JToken"/>.</value>
 
104
    public JToken Root
 
105
    {
 
106
      get
 
107
      {
 
108
        JContainer parent = Parent;
 
109
        if (parent == null)
 
110
          return this;
 
111
 
 
112
        while (parent.Parent != null)
 
113
        {
 
114
          parent = parent.Parent;
 
115
        }
 
116
 
 
117
        return parent;
 
118
      }
 
119
    }
 
120
 
 
121
    internal abstract JToken CloneToken();
 
122
    internal abstract bool DeepEquals(JToken node);
 
123
 
 
124
    /// <summary>
 
125
    /// Gets the node type for this <see cref="JToken"/>.
 
126
    /// </summary>
 
127
    /// <value>The type.</value>
 
128
    public abstract JTokenType Type { get; }
 
129
 
 
130
    /// <summary>
 
131
    /// Gets a value indicating whether this token has childen tokens.
 
132
    /// </summary>
 
133
    /// <value>
 
134
    ///         <c>true</c> if this token has child values; otherwise, <c>false</c>.
 
135
    /// </value>
 
136
    public abstract bool HasValues { get; }
 
137
 
 
138
    /// <summary>
 
139
    /// Compares the values of two tokens, including the values of all descendant tokens.
 
140
    /// </summary>
 
141
    /// <param name="t1">The first <see cref="JToken"/> to compare.</param>
 
142
    /// <param name="t2">The second <see cref="JToken"/> to compare.</param>
 
143
    /// <returns>true if the tokens are equal; otherwise false.</returns>
 
144
    public static bool DeepEquals(JToken t1, JToken t2)
 
145
    {
 
146
      return (t1 == t2 || (t1 != null && t2 != null && t1.DeepEquals(t2)));
 
147
    }
 
148
 
 
149
    /// <summary>
 
150
    /// Gets the next sibling token of this node.
 
151
    /// </summary>
 
152
    /// <value>The <see cref="JToken"/> that contains the next sibling token.</value>
 
153
    public JToken Next
 
154
    {
 
155
      get { return _next; }
 
156
      internal set { _next = value; }
 
157
    }
 
158
 
 
159
    /// <summary>
 
160
    /// Gets the previous sibling token of this node.
 
161
    /// </summary>
 
162
    /// <value>The <see cref="JToken"/> that contains the previous sibling token.</value>
 
163
    public JToken Previous
 
164
    {
 
165
      get { return _previous; }
 
166
      internal set { _previous = value; }
 
167
    }
 
168
 
 
169
    internal JToken()
 
170
    {
 
171
    }
 
172
 
 
173
    /// <summary>
 
174
    /// Adds the specified content immediately after this token.
 
175
    /// </summary>
 
176
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added after this token.</param>
 
177
    public void AddAfterSelf(object content)
 
178
    {
 
179
      if (_parent == null)
 
180
        throw new InvalidOperationException("The parent is missing.");
 
181
 
 
182
      int index = _parent.IndexOfItem(this);
 
183
      _parent.AddInternal(index + 1, content, false);
 
184
    }
 
185
 
 
186
    /// <summary>
 
187
    /// Adds the specified content immediately before this token.
 
188
    /// </summary>
 
189
    /// <param name="content">A content object that contains simple content or a collection of content objects to be added before this token.</param>
 
190
    public void AddBeforeSelf(object content)
 
191
    {
 
192
      if (_parent == null)
 
193
        throw new InvalidOperationException("The parent is missing.");
 
194
 
 
195
      int index = _parent.IndexOfItem(this);
 
196
      _parent.AddInternal(index, content, false);
 
197
    }
 
198
 
 
199
    /// <summary>
 
200
    /// Returns a collection of the ancestor tokens of this token.
 
201
    /// </summary>
 
202
    /// <returns>A collection of the ancestor tokens of this token.</returns>
 
203
    public IEnumerable<JToken> Ancestors()
 
204
    {
 
205
      for (JToken parent = Parent; parent != null; parent = parent.Parent)
 
206
      {
 
207
        yield return parent;
 
208
      }
 
209
    }
 
210
 
 
211
    /// <summary>
 
212
    /// Returns a collection of the sibling tokens after this token, in document order.
 
213
    /// </summary>
 
214
    /// <returns>A collection of the sibling tokens after this tokens, in document order.</returns>
 
215
    public IEnumerable<JToken> AfterSelf()
 
216
    {
 
217
      if (Parent == null)
 
218
        yield break;
 
219
 
 
220
      for (JToken o = Next; o != null; o = o.Next)
 
221
      {
 
222
        yield return o;
 
223
      }
 
224
    }
 
225
 
 
226
    /// <summary>
 
227
    /// Returns a collection of the sibling tokens before this token, in document order.
 
228
    /// </summary>
 
229
    /// <returns>A collection of the sibling tokens before this token, in document order.</returns>
 
230
    public IEnumerable<JToken> BeforeSelf()
 
231
    {
 
232
      for (JToken o = Parent.First; o != this; o = o.Next)
 
233
      {
 
234
        yield return o;
 
235
      }
 
236
    }
 
237
 
 
238
    /// <summary>
 
239
    /// Gets the <see cref="JToken"/> with the specified key.
 
240
    /// </summary>
 
241
    /// <value>The <see cref="JToken"/> with the specified key.</value>
 
242
    public virtual JToken this[object key]
 
243
    {
 
244
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
245
      set { throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
246
    }
 
247
 
 
248
    /// <summary>
 
249
    /// Gets the <see cref="JToken"/> with the specified key converted to the specified type.
 
250
    /// </summary>
 
251
    /// <typeparam name="T">The type to convert the token to.</typeparam>
 
252
    /// <param name="key">The token key.</param>
 
253
    /// <returns>The converted token value.</returns>
 
254
    public virtual T Value<T>(object key)
 
255
    {
 
256
      JToken token = this[key];
 
257
 
 
258
      return Extensions.Convert<JToken, T>(token);
 
259
    }
 
260
 
 
261
    /// <summary>
 
262
    /// Get the first child token of this token.
 
263
    /// </summary>
 
264
    /// <value>A <see cref="JToken"/> containing the first child token of the <see cref="JToken"/>.</value>
 
265
    public virtual JToken First
 
266
    {
 
267
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
268
    }
 
269
 
 
270
    /// <summary>
 
271
    /// Get the last child token of this token.
 
272
    /// </summary>
 
273
    /// <value>A <see cref="JToken"/> containing the last child token of the <see cref="JToken"/>.</value>
 
274
    public virtual JToken Last
 
275
    {
 
276
      get { throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType())); }
 
277
    }
 
278
 
 
279
    /// <summary>
 
280
    /// Returns a collection of the child tokens of this token, in document order.
 
281
    /// </summary>
 
282
    /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="JToken"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
 
283
    public virtual JEnumerable<JToken> Children()
 
284
    {
 
285
      return JEnumerable<JToken>.Empty;
 
286
    }
 
287
 
 
288
    /// <summary>
 
289
    /// Returns a collection of the child tokens of this token, in document order, filtered by the specified type.
 
290
    /// </summary>
 
291
    /// <typeparam name="T">The type to filter the child tokens on.</typeparam>
 
292
    /// <returns>A <see cref="JEnumerable{T}"/> containing the child tokens of this <see cref="JToken"/>, in document order.</returns>
 
293
    public JEnumerable<T> Children<T>() where T : JToken
 
294
    {
 
295
      return new JEnumerable<T>(Children().OfType<T>());
 
296
    }
 
297
 
 
298
    /// <summary>
 
299
    /// Returns a collection of the child values of this token, in document order.
 
300
    /// </summary>
 
301
    /// <typeparam name="T">The type to convert the values to.</typeparam>
 
302
    /// <returns>A <see cref="IEnumerable{T}"/> containing the child values of this <see cref="JToken"/>, in document order.</returns>
 
303
    public virtual IEnumerable<T> Values<T>()
 
304
    {
 
305
      throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
 
306
    }
 
307
 
 
308
    /// <summary>
 
309
    /// Removes this token from its parent.
 
310
    /// </summary>
 
311
    public void Remove()
 
312
    {
 
313
      if (_parent == null)
 
314
        throw new InvalidOperationException("The parent is missing.");
 
315
 
 
316
      _parent.RemoveItem(this);
 
317
    }
 
318
 
 
319
    /// <summary>
 
320
    /// Replaces this token with the specified token.
 
321
    /// </summary>
 
322
    /// <param name="value">The value.</param>
 
323
    public void Replace(JToken value)
 
324
    {
 
325
      if (_parent == null)
 
326
        throw new InvalidOperationException("The parent is missing.");
 
327
 
 
328
      _parent.ReplaceItem(this, value);
 
329
    }
 
330
 
 
331
    /// <summary>
 
332
    /// Writes this token to a <see cref="JsonWriter"/>.
 
333
    /// </summary>
 
334
    /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
 
335
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
 
336
    public abstract void WriteTo(JsonWriter writer, params JsonConverter[] converters);
 
337
 
 
338
    /// <summary>
 
339
    /// Returns the indented JSON for this token.
 
340
    /// </summary>
 
341
    /// <returns>
 
342
    /// The indented JSON for this token.
 
343
    /// </returns>
 
344
    public override string ToString()
 
345
    {
 
346
      return ToString(Formatting.Indented);
 
347
    }
 
348
 
 
349
    /// <summary>
 
350
    /// Returns the JSON for this token using the given formatting and converters.
 
351
    /// </summary>
 
352
    /// <param name="formatting">Indicates how the output is formatted.</param>
 
353
    /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
 
354
    /// <returns>The JSON for this token using the given formatting and converters.</returns>
 
355
    public string ToString(Formatting formatting, params JsonConverter[] converters)
 
356
    {
 
357
      using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
 
358
      {
 
359
        JsonTextWriter jw = new JsonTextWriter(sw);
 
360
        jw.Formatting = formatting;
 
361
 
 
362
        WriteTo(jw, converters);
 
363
 
 
364
        return sw.ToString();
 
365
      }
 
366
    }
 
367
 
 
368
    private static JValue EnsureValue(JToken value)
 
369
    {
 
370
      if (value == null)
 
371
        throw new ArgumentNullException("value");
 
372
 
 
373
      if (value is JProperty)
 
374
        value = ((JProperty)value).Value;
 
375
 
 
376
      JValue v = value as JValue;
 
377
 
 
378
      return v;
 
379
    }
 
380
 
 
381
    private static string GetType(JToken token)
 
382
    {
 
383
      ValidationUtils.ArgumentNotNull(token, "token");
 
384
 
 
385
      if (token is JProperty)
 
386
        token = ((JProperty)token).Value;
 
387
 
 
388
      return token.Type.ToString();
 
389
    }
 
390
 
 
391
    private static bool ValidateToken(JToken o, JTokenType[] validTypes, bool nullable)
 
392
    {
 
393
      return (Array.IndexOf(validTypes, o.Type) != -1) || (nullable && (o.Type == JTokenType.Null || o.Type == JTokenType.Undefined));
 
394
    }
 
395
 
 
396
    #region Cast from operators
 
397
    /// <summary>
 
398
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Boolean"/>.
 
399
    /// </summary>
 
400
    /// <param name="value">The value.</param>
 
401
    /// <returns>The result of the conversion.</returns>
 
402
    public static explicit operator bool(JToken value)
 
403
    {
 
404
      JValue v = EnsureValue(value);
 
405
      if (v == null || !ValidateToken(v, BooleanTypes, false))
 
406
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
407
 
 
408
      return Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture);
 
409
    }
 
410
 
 
411
#if !PocketPC && !NET20
 
412
    /// <summary>
 
413
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTimeOffset"/>.
 
414
    /// </summary>
 
415
    /// <param name="value">The value.</param>
 
416
    /// <returns>The result of the conversion.</returns>
 
417
    public static explicit operator DateTimeOffset(JToken value)
 
418
    {
 
419
      JValue v = EnsureValue(value);
 
420
      if (v == null || !ValidateToken(v, DateTimeTypes, false))
 
421
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
422
 
 
423
      if (v.Value is DateTimeOffset)
 
424
        return (DateTimeOffset)v.Value;
 
425
      if (v.Value is string)
 
426
        return DateTimeOffset.Parse((string)v.Value);
 
427
      return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture));
 
428
    }
 
429
#endif
 
430
 
 
431
    /// <summary>
 
432
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Boolean}"/>.
 
433
    /// </summary>
 
434
    /// <param name="value">The value.</param>
 
435
    /// <returns>The result of the conversion.</returns>
 
436
    public static explicit operator bool?(JToken value)
 
437
    {
 
438
      if (value == null)
 
439
        return null;
 
440
 
 
441
      JValue v = EnsureValue(value);
 
442
      if (v == null || !ValidateToken(v, BooleanTypes, true))
 
443
        throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
444
 
 
445
      return (v.Value != null) ? (bool?)Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture) : null;
 
446
    }
 
447
 
 
448
    /// <summary>
 
449
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int64"/>.
 
450
    /// </summary>
 
451
    /// <param name="value">The value.</param>
 
452
    /// <returns>The result of the conversion.</returns>
 
453
    public static explicit operator long(JToken value)
 
454
    {
 
455
      JValue v = EnsureValue(value);
 
456
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
457
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
458
 
 
459
      return Convert.ToInt64(v.Value, CultureInfo.InvariantCulture);
 
460
    }
 
461
 
 
462
    /// <summary>
 
463
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTime}"/>.
 
464
    /// </summary>
 
465
    /// <param name="value">The value.</param>
 
466
    /// <returns>The result of the conversion.</returns>
 
467
    public static explicit operator DateTime?(JToken value)
 
468
    {
 
469
      if (value == null)
 
470
        return null;
 
471
 
 
472
      JValue v = EnsureValue(value);
 
473
      if (v == null || !ValidateToken(v, DateTimeTypes, true))
 
474
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
475
 
 
476
      return (v.Value != null) ? (DateTime?)Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture) : null;
 
477
    }
 
478
 
 
479
#if !PocketPC && !NET20
 
480
    /// <summary>
 
481
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{DateTimeOffset}"/>.
 
482
    /// </summary>
 
483
    /// <param name="value">The value.</param>
 
484
    /// <returns>The result of the conversion.</returns>
 
485
    public static explicit operator DateTimeOffset?(JToken value)
 
486
    {
 
487
      if (value == null)
 
488
        return null;
 
489
 
 
490
      JValue v = EnsureValue(value);
 
491
      if (v == null || !ValidateToken(v, DateTimeTypes, true))
 
492
        throw new ArgumentException("Can not convert {0} to DateTimeOffset.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
493
 
 
494
      if (v.Value == null)
 
495
        return null;
 
496
      if (v.Value is DateTimeOffset)
 
497
        return (DateTimeOffset?)v.Value;
 
498
      if (v.Value is string)
 
499
        return DateTimeOffset.Parse((string)v.Value);
 
500
      return new DateTimeOffset(Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture));
 
501
    }
 
502
#endif
 
503
 
 
504
    /// <summary>
 
505
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Decimal}"/>.
 
506
    /// </summary>
 
507
    /// <param name="value">The value.</param>
 
508
    /// <returns>The result of the conversion.</returns>
 
509
    public static explicit operator decimal?(JToken value)
 
510
    {
 
511
      if (value == null)
 
512
        return null;
 
513
 
 
514
      JValue v = EnsureValue(value);
 
515
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
516
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
517
 
 
518
      return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;
 
519
    }
 
520
 
 
521
    /// <summary>
 
522
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Double}"/>.
 
523
    /// </summary>
 
524
    /// <param name="value">The value.</param>
 
525
    /// <returns>The result of the conversion.</returns>
 
526
    public static explicit operator double?(JToken value)
 
527
    {
 
528
      if (value == null)
 
529
        return null;
 
530
 
 
531
      JValue v = EnsureValue(value);
 
532
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
533
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
534
 
 
535
      return (v.Value != null) ? (double?)Convert.ToDouble(v.Value, CultureInfo.InvariantCulture) : null;
 
536
    }
 
537
 
 
538
    /// <summary>
 
539
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Char}"/>.
 
540
    /// </summary>
 
541
    /// <param name="value">The value.</param>
 
542
    /// <returns>The result of the conversion.</returns>
 
543
    public static explicit operator char?(JToken value)
 
544
    {
 
545
      if (value == null)
 
546
        return null;
 
547
 
 
548
      JValue v = EnsureValue(value);
 
549
      if (v == null || !ValidateToken(v, CharTypes, true))
 
550
        throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
551
 
 
552
      return (v.Value != null) ? (char?)Convert.ToChar(v.Value, CultureInfo.InvariantCulture) : null;
 
553
    }
 
554
 
 
555
    /// <summary>
 
556
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int32"/>.
 
557
    /// </summary>
 
558
    /// <param name="value">The value.</param>
 
559
    /// <returns>The result of the conversion.</returns>
 
560
    public static explicit operator int(JToken value)
 
561
    {
 
562
      JValue v = EnsureValue(value);
 
563
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
564
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
565
 
 
566
      return Convert.ToInt32(v.Value, CultureInfo.InvariantCulture);
 
567
    }
 
568
 
 
569
    /// <summary>
 
570
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Int16"/>.
 
571
    /// </summary>
 
572
    /// <param name="value">The value.</param>
 
573
    /// <returns>The result of the conversion.</returns>
 
574
    public static explicit operator short(JToken value)
 
575
    {
 
576
      JValue v = EnsureValue(value);
 
577
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
578
        throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
579
 
 
580
      return Convert.ToInt16(v.Value, CultureInfo.InvariantCulture);
 
581
    }
 
582
 
 
583
    /// <summary>
 
584
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt16"/>.
 
585
    /// </summary>
 
586
    /// <param name="value">The value.</param>
 
587
    /// <returns>The result of the conversion.</returns>
 
588
    [CLSCompliant(false)]
 
589
    public static explicit operator ushort(JToken value)
 
590
    {
 
591
      JValue v = EnsureValue(value);
 
592
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
593
        throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
594
 
 
595
      return Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture);
 
596
    }
 
597
 
 
598
    /// <summary>
 
599
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Char"/>.
 
600
    /// </summary>
 
601
    /// <param name="value">The value.</param>
 
602
    /// <returns>The result of the conversion.</returns>
 
603
    [CLSCompliant(false)]
 
604
    public static explicit operator char(JToken value)
 
605
    {
 
606
      JValue v = EnsureValue(value);
 
607
      if (v == null || !ValidateToken(v, CharTypes, false))
 
608
        throw new ArgumentException("Can not convert {0} to Char.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
609
 
 
610
      return Convert.ToChar(v.Value, CultureInfo.InvariantCulture);
 
611
    }
 
612
 
 
613
    /// <summary>
 
614
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Byte"/>.
 
615
    /// </summary>
 
616
    /// <param name="value">The value.</param>
 
617
    /// <returns>The result of the conversion.</returns>
 
618
    public static explicit operator byte(JToken value)
 
619
    {
 
620
      JValue v = EnsureValue(value);
 
621
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
622
        throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
623
 
 
624
      return Convert.ToByte(v.Value, CultureInfo.InvariantCulture);
 
625
    }
 
626
 
 
627
    /// <summary>
 
628
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int32}"/>.
 
629
    /// </summary>
 
630
    /// <param name="value">The value.</param>
 
631
    /// <returns>The result of the conversion.</returns>
 
632
    public static explicit operator int?(JToken value)
 
633
    {
 
634
      if (value == null)
 
635
        return null;
 
636
 
 
637
      JValue v = EnsureValue(value);
 
638
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
639
        throw new ArgumentException("Can not convert {0} to Int32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
640
 
 
641
      return (v.Value != null) ? (int?)Convert.ToInt32(v.Value, CultureInfo.InvariantCulture) : null;
 
642
    }
 
643
 
 
644
    /// <summary>
 
645
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int16}"/>.
 
646
    /// </summary>
 
647
    /// <param name="value">The value.</param>
 
648
    /// <returns>The result of the conversion.</returns>
 
649
    public static explicit operator short?(JToken value)
 
650
    {
 
651
      if (value == null)
 
652
        return null;
 
653
 
 
654
      JValue v = EnsureValue(value);
 
655
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
656
        throw new ArgumentException("Can not convert {0} to Int16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
657
 
 
658
      return (v.Value != null) ? (short?)Convert.ToInt16(v.Value, CultureInfo.InvariantCulture) : null;
 
659
    }
 
660
 
 
661
    /// <summary>
 
662
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt16}"/>.
 
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 ushort?(JToken value)
 
668
    {
 
669
      if (value == null)
 
670
        return null;
 
671
 
 
672
      JValue v = EnsureValue(value);
 
673
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
674
        throw new ArgumentException("Can not convert {0} to UInt16.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
675
 
 
676
      return (v.Value != null) ? (ushort?)Convert.ToUInt16(v.Value, CultureInfo.InvariantCulture) : null;
 
677
    }
 
678
 
 
679
    /// <summary>
 
680
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Byte}"/>.
 
681
    /// </summary>
 
682
    /// <param name="value">The value.</param>
 
683
    /// <returns>The result of the conversion.</returns>
 
684
    public static explicit operator byte?(JToken value)
 
685
    {
 
686
      if (value == null)
 
687
        return null;
 
688
 
 
689
      JValue v = EnsureValue(value);
 
690
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
691
        throw new ArgumentException("Can not convert {0} to Byte.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
692
 
 
693
      return (v.Value != null) ? (byte?)Convert.ToByte(v.Value, CultureInfo.InvariantCulture) : null;
 
694
    }
 
695
 
 
696
    /// <summary>
 
697
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTime"/>.
 
698
    /// </summary>
 
699
    /// <param name="value">The value.</param>
 
700
    /// <returns>The result of the conversion.</returns>
 
701
    public static explicit operator DateTime(JToken value)
 
702
    {
 
703
      JValue v = EnsureValue(value);
 
704
      if (v == null || !ValidateToken(v, DateTimeTypes, false))
 
705
        throw new ArgumentException("Can not convert {0} to DateTime.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
706
 
 
707
      return Convert.ToDateTime(v.Value, CultureInfo.InvariantCulture);
 
708
    }
 
709
 
 
710
    /// <summary>
 
711
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Int64}"/>.
 
712
    /// </summary>
 
713
    /// <param name="value">The value.</param>
 
714
    /// <returns>The result of the conversion.</returns>
 
715
    public static explicit operator long?(JToken value)
 
716
    {
 
717
      if (value == null)
 
718
        return null;
 
719
 
 
720
      JValue v = EnsureValue(value);
 
721
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
722
        throw new ArgumentException("Can not convert {0} to Int64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
723
 
 
724
      return (v.Value != null) ? (long?)Convert.ToInt64(v.Value, CultureInfo.InvariantCulture) : null;
 
725
    }
 
726
 
 
727
    /// <summary>
 
728
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{Single}"/>.
 
729
    /// </summary>
 
730
    /// <param name="value">The value.</param>
 
731
    /// <returns>The result of the conversion.</returns>
 
732
    public static explicit operator float?(JToken value)
 
733
    {
 
734
      if (value == null)
 
735
        return null;
 
736
 
 
737
      JValue v = EnsureValue(value);
 
738
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
739
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
740
 
 
741
      return (v.Value != null) ? (float?)Convert.ToSingle(v.Value, CultureInfo.InvariantCulture) : null;
 
742
    }
 
743
 
 
744
    /// <summary>
 
745
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Decimal"/>.
 
746
    /// </summary>
 
747
    /// <param name="value">The value.</param>
 
748
    /// <returns>The result of the conversion.</returns>
 
749
    public static explicit operator decimal(JToken value)
 
750
    {
 
751
      JValue v = EnsureValue(value);
 
752
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
753
        throw new ArgumentException("Can not convert {0} to Decimal.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
754
 
 
755
      return Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture);
 
756
    }
 
757
 
 
758
    /// <summary>
 
759
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt32}"/>.
 
760
    /// </summary>
 
761
    /// <param name="value">The value.</param>
 
762
    /// <returns>The result of the conversion.</returns>
 
763
    [CLSCompliant(false)]
 
764
    public static explicit operator uint?(JToken value)
 
765
    {
 
766
      if (value == null)
 
767
        return null;
 
768
 
 
769
      JValue v = EnsureValue(value);
 
770
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
771
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
772
 
 
773
      return (v.Value != null) ? (uint?)Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture) : null;
 
774
    }
 
775
 
 
776
    /// <summary>
 
777
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="Nullable{UInt64}"/>.
 
778
    /// </summary>
 
779
    /// <param name="value">The value.</param>
 
780
    /// <returns>The result of the conversion.</returns>
 
781
    [CLSCompliant(false)]
 
782
    public static explicit operator ulong?(JToken value)
 
783
    {
 
784
      if (value == null)
 
785
        return null;
 
786
 
 
787
      JValue v = EnsureValue(value);
 
788
      if (v == null || !ValidateToken(v, NumberTypes, true))
 
789
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
790
 
 
791
      return (v.Value != null) ? (ulong?)Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture) : null;
 
792
    }
 
793
 
 
794
    /// <summary>
 
795
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Double"/>.
 
796
    /// </summary>
 
797
    /// <param name="value">The value.</param>
 
798
    /// <returns>The result of the conversion.</returns>
 
799
    public static explicit operator double(JToken value)
 
800
    {
 
801
      JValue v = EnsureValue(value);
 
802
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
803
        throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
804
 
 
805
      return Convert.ToDouble(v.Value, CultureInfo.InvariantCulture);
 
806
    }
 
807
 
 
808
    /// <summary>
 
809
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Single"/>.
 
810
    /// </summary>
 
811
    /// <param name="value">The value.</param>
 
812
    /// <returns>The result of the conversion.</returns>
 
813
    public static explicit operator float(JToken value)
 
814
    {
 
815
      JValue v = EnsureValue(value);
 
816
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
817
        throw new ArgumentException("Can not convert {0} to Single.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
818
 
 
819
      return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);
 
820
    }
 
821
 
 
822
    /// <summary>
 
823
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.String"/>.
 
824
    /// </summary>
 
825
    /// <param name="value">The value.</param>
 
826
    /// <returns>The result of the conversion.</returns>
 
827
    public static explicit operator string(JToken value)
 
828
    {
 
829
      if (value == null)
 
830
        return null;
 
831
 
 
832
      JValue v = EnsureValue(value);
 
833
      if (v == null || !ValidateToken(v, StringTypes, true))
 
834
        throw new ArgumentException("Can not convert {0} to String.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
835
 
 
836
      if (v.Value == null)
 
837
        return null;
 
838
      if (v.Value is byte[])
 
839
        return Convert.ToBase64String((byte[]) v.Value);
 
840
 
 
841
      return Convert.ToString(v.Value, CultureInfo.InvariantCulture);
 
842
    }
 
843
 
 
844
    /// <summary>
 
845
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt32"/>.
 
846
    /// </summary>
 
847
    /// <param name="value">The value.</param>
 
848
    /// <returns>The result of the conversion.</returns>
 
849
    [CLSCompliant(false)]
 
850
    public static explicit operator uint(JToken value)
 
851
    {
 
852
      JValue v = EnsureValue(value);
 
853
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
854
        throw new ArgumentException("Can not convert {0} to UInt32.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
855
 
 
856
      return Convert.ToUInt32(v.Value, CultureInfo.InvariantCulture);
 
857
    }
 
858
 
 
859
    /// <summary>
 
860
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.UInt64"/>.
 
861
    /// </summary>
 
862
    /// <param name="value">The value.</param>
 
863
    /// <returns>The result of the conversion.</returns>
 
864
    [CLSCompliant(false)]
 
865
    public static explicit operator ulong(JToken value)
 
866
    {
 
867
      JValue v = EnsureValue(value);
 
868
      if (v == null || !ValidateToken(v, NumberTypes, false))
 
869
        throw new ArgumentException("Can not convert {0} to UInt64.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
870
 
 
871
      return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
 
872
    }
 
873
 
 
874
    /// <summary>
 
875
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="T:System.Byte[]"/>.
 
876
    /// </summary>
 
877
    /// <param name="value">The value.</param>
 
878
    /// <returns>The result of the conversion.</returns>
 
879
    public static explicit operator byte[](JToken value)
 
880
    {
 
881
      if (value == null)
 
882
        return null;
 
883
 
 
884
      JValue v = EnsureValue(value);
 
885
      if (v == null || !ValidateToken(v, BytesTypes, false))
 
886
        throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
887
 
 
888
      if (v.Value is string)
 
889
        return Convert.FromBase64String(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
890
      return (byte[])v.Value;
 
891
    }
 
892
 
 
893
    /// <summary>
 
894
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Guid"/>.
 
895
    /// </summary>
 
896
    /// <param name="value">The value.</param>
 
897
    /// <returns>The result of the conversion.</returns>
 
898
    public static explicit operator Guid(JToken value)
 
899
    {
 
900
      JValue v = EnsureValue(value);
 
901
      if (v == null || !ValidateToken(v, GuidTypes, false))
 
902
        throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
903
 
 
904
      return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
905
    }
 
906
 
 
907
    /// <summary>
 
908
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Guid"/>.
 
909
    /// </summary>
 
910
    /// <param name="value">The value.</param>
 
911
    /// <returns>The result of the conversion.</returns>
 
912
    public static explicit operator Guid?(JToken value)
 
913
    {
 
914
      if (value == null)
 
915
        return null;
 
916
 
 
917
      JValue v = EnsureValue(value);
 
918
      if (v == null || !ValidateToken(v, GuidTypes, true))
 
919
        throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
920
 
 
921
      if (v.Value == null)
 
922
        return null;
 
923
 
 
924
      return (v.Value is Guid) ? (Guid)v.Value : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
925
    }
 
926
 
 
927
    /// <summary>
 
928
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.TimeSpan"/>.
 
929
    /// </summary>
 
930
    /// <param name="value">The value.</param>
 
931
    /// <returns>The result of the conversion.</returns>
 
932
    public static explicit operator TimeSpan(JToken value)
 
933
    {
 
934
      JValue v = EnsureValue(value);
 
935
      if (v == null || !ValidateToken(v, TimeSpanTypes, false))
 
936
        throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
937
 
 
938
      return (v.Value is TimeSpan) ? (TimeSpan)v.Value : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
939
    }
 
940
 
 
941
    /// <summary>
 
942
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.TimeSpan"/>.
 
943
    /// </summary>
 
944
    /// <param name="value">The value.</param>
 
945
    /// <returns>The result of the conversion.</returns>
 
946
    public static explicit operator TimeSpan?(JToken value)
 
947
    {
 
948
      if (value == null)
 
949
        return null;
 
950
 
 
951
      JValue v = EnsureValue(value);
 
952
      if (v == null || !ValidateToken(v, TimeSpanTypes, true))
 
953
        throw new ArgumentException("Can not convert {0} to TimeSpan.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
954
 
 
955
      if (v.Value == null)
 
956
        return null;
 
957
 
 
958
      return (v.Value is TimeSpan) ? (TimeSpan)v.Value : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
959
    }
 
960
 
 
961
    /// <summary>
 
962
    /// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Uri"/>.
 
963
    /// </summary>
 
964
    /// <param name="value">The value.</param>
 
965
    /// <returns>The result of the conversion.</returns>
 
966
    public static explicit operator Uri(JToken value)
 
967
    {
 
968
      if (value == null)
 
969
        return null;
 
970
 
 
971
      JValue v = EnsureValue(value);
 
972
      if (v == null || !ValidateToken(v, UriTypes, true))
 
973
        throw new ArgumentException("Can not convert {0} to Uri.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
 
974
 
 
975
      if (v.Value == null)
 
976
        return null;
 
977
 
 
978
      return (v.Value is Uri) ? (Uri)v.Value : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture));
 
979
    }
 
980
    #endregion
 
981
 
 
982
    #region Cast to operators
 
983
    /// <summary>
 
984
    /// Performs an implicit conversion from <see cref="Boolean"/> to <see cref="JToken"/>.
 
985
    /// </summary>
 
986
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
987
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
988
    public static implicit operator JToken(bool value)
 
989
    {
 
990
      return new JValue(value);
 
991
    }
 
992
 
 
993
#if !PocketPC && !NET20
 
994
    /// <summary>
 
995
    /// Performs an implicit conversion from <see cref="DateTimeOffset"/> to <see cref="JToken"/>.
 
996
    /// </summary>
 
997
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
998
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
999
    public static implicit operator JToken(DateTimeOffset value)
 
1000
    {
 
1001
      return new JValue(value);
 
1002
    }
 
1003
#endif
 
1004
 
 
1005
    /// <summary>
 
1006
    /// Performs an implicit conversion from <see cref="Nullable{Boolean}"/> to <see cref="JToken"/>.
 
1007
    /// </summary>
 
1008
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1009
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1010
    public static implicit operator JToken(bool? value)
 
1011
    {
 
1012
      return new JValue(value);
 
1013
    }
 
1014
 
 
1015
    /// <summary>
 
1016
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
 
1017
    /// </summary>
 
1018
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1019
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1020
    public static implicit operator JToken(long value)
 
1021
    {
 
1022
      return new JValue(value);
 
1023
    }
 
1024
 
 
1025
    /// <summary>
 
1026
    /// Performs an implicit conversion from <see cref="Nullable{DateTime}"/> to <see cref="JToken"/>.
 
1027
    /// </summary>
 
1028
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1029
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1030
    public static implicit operator JToken(DateTime? value)
 
1031
    {
 
1032
      return new JValue(value);
 
1033
    }
 
1034
 
 
1035
#if !PocketPC && !NET20
 
1036
    /// <summary>
 
1037
    /// Performs an implicit conversion from <see cref="Nullable{DateTimeOffset}"/> to <see cref="JToken"/>.
 
1038
    /// </summary>
 
1039
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1040
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1041
    public static implicit operator JToken(DateTimeOffset? value)
 
1042
    {
 
1043
      return new JValue(value);
 
1044
    }
 
1045
#endif
 
1046
 
 
1047
    /// <summary>
 
1048
    /// Performs an implicit conversion from <see cref="Nullable{Decimal}"/> to <see cref="JToken"/>.
 
1049
    /// </summary>
 
1050
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1051
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1052
    public static implicit operator JToken(decimal? value)
 
1053
    {
 
1054
      return new JValue(value);
 
1055
    }
 
1056
 
 
1057
    /// <summary>
 
1058
    /// Performs an implicit conversion from <see cref="Nullable{Double}"/> to <see cref="JToken"/>.
 
1059
    /// </summary>
 
1060
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1061
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1062
    public static implicit operator JToken(double? value)
 
1063
    {
 
1064
      return new JValue(value);
 
1065
    }
 
1066
 
 
1067
    /// <summary>
 
1068
    /// Performs an implicit conversion from <see cref="Int16"/> to <see cref="JToken"/>.
 
1069
    /// </summary>
 
1070
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1071
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1072
    [CLSCompliant(false)]
 
1073
    public static implicit operator JToken(short value)
 
1074
    {
 
1075
      return new JValue(value);
 
1076
    }
 
1077
 
 
1078
    /// <summary>
 
1079
    /// Performs an implicit conversion from <see cref="UInt16"/> to <see cref="JToken"/>.
 
1080
    /// </summary>
 
1081
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1082
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1083
    [CLSCompliant(false)]
 
1084
    public static implicit operator JToken(ushort value)
 
1085
    {
 
1086
      return new JValue(value);
 
1087
    }
 
1088
 
 
1089
    /// <summary>
 
1090
    /// Performs an implicit conversion from <see cref="Int32"/> to <see cref="JToken"/>.
 
1091
    /// </summary>
 
1092
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1093
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1094
    public static implicit operator JToken(int value)
 
1095
    {
 
1096
      return new JValue(value);
 
1097
    }
 
1098
 
 
1099
    /// <summary>
 
1100
    /// Performs an implicit conversion from <see cref="Nullable{Int32}"/> to <see cref="JToken"/>.
 
1101
    /// </summary>
 
1102
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1103
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1104
    public static implicit operator JToken(int? value)
 
1105
    {
 
1106
      return new JValue(value);
 
1107
    }
 
1108
 
 
1109
    /// <summary>
 
1110
    /// Performs an implicit conversion from <see cref="DateTime"/> to <see cref="JToken"/>.
 
1111
    /// </summary>
 
1112
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1113
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1114
    public static implicit operator JToken(DateTime value)
 
1115
    {
 
1116
      return new JValue(value);
 
1117
    }
 
1118
 
 
1119
    /// <summary>
 
1120
    /// Performs an implicit conversion from <see cref="Nullable{Int64}"/> to <see cref="JToken"/>.
 
1121
    /// </summary>
 
1122
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1123
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1124
    public static implicit operator JToken(long? value)
 
1125
    {
 
1126
      return new JValue(value);
 
1127
    }
 
1128
 
 
1129
    /// <summary>
 
1130
    /// Performs an implicit conversion from <see cref="Nullable{Single}"/> to <see cref="JToken"/>.
 
1131
    /// </summary>
 
1132
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1133
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1134
    public static implicit operator JToken(float? value)
 
1135
    {
 
1136
      return new JValue(value);
 
1137
    }
 
1138
 
 
1139
    /// <summary>
 
1140
    /// Performs an implicit conversion from <see cref="Decimal"/> to <see cref="JToken"/>.
 
1141
    /// </summary>
 
1142
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1143
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1144
    public static implicit operator JToken(decimal value)
 
1145
    {
 
1146
      return new JValue(value);
 
1147
    }
 
1148
 
 
1149
    /// <summary>
 
1150
    /// Performs an implicit conversion from <see cref="Nullable{Int16}"/> to <see cref="JToken"/>.
 
1151
    /// </summary>
 
1152
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1153
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1154
    [CLSCompliant(false)]
 
1155
    public static implicit operator JToken(short? value)
 
1156
    {
 
1157
      return new JValue(value);
 
1158
    }
 
1159
 
 
1160
    /// <summary>
 
1161
    /// Performs an implicit conversion from <see cref="Nullable{UInt16}"/> to <see cref="JToken"/>.
 
1162
    /// </summary>
 
1163
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1164
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1165
    [CLSCompliant(false)]
 
1166
    public static implicit operator JToken(ushort? value)
 
1167
    {
 
1168
      return new JValue(value);
 
1169
    }
 
1170
 
 
1171
    /// <summary>
 
1172
    /// Performs an implicit conversion from <see cref="Nullable{UInt32}"/> to <see cref="JToken"/>.
 
1173
    /// </summary>
 
1174
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1175
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1176
    [CLSCompliant(false)]
 
1177
    public static implicit operator JToken(uint? value)
 
1178
    {
 
1179
      return new JValue(value);
 
1180
    }
 
1181
 
 
1182
    /// <summary>
 
1183
    /// Performs an implicit conversion from <see cref="Nullable{UInt64}"/> to <see cref="JToken"/>.
 
1184
    /// </summary>
 
1185
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1186
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1187
    [CLSCompliant(false)]
 
1188
    public static implicit operator JToken(ulong? value)
 
1189
    {
 
1190
      return new JValue(value);
 
1191
    }
 
1192
 
 
1193
    /// <summary>
 
1194
    /// Performs an implicit conversion from <see cref="Double"/> to <see cref="JToken"/>.
 
1195
    /// </summary>
 
1196
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1197
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1198
    public static implicit operator JToken(double value)
 
1199
    {
 
1200
      return new JValue(value);
 
1201
    }
 
1202
 
 
1203
    /// <summary>
 
1204
    /// Performs an implicit conversion from <see cref="Single"/> to <see cref="JToken"/>.
 
1205
    /// </summary>
 
1206
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1207
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1208
    public static implicit operator JToken(float value)
 
1209
    {
 
1210
      return new JValue(value);
 
1211
    }
 
1212
 
 
1213
    /// <summary>
 
1214
    /// Performs an implicit conversion from <see cref="String"/> to <see cref="JToken"/>.
 
1215
    /// </summary>
 
1216
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1217
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1218
    public static implicit operator JToken(string value)
 
1219
    {
 
1220
      return new JValue(value);
 
1221
    }
 
1222
 
 
1223
    /// <summary>
 
1224
    /// Performs an implicit conversion from <see cref="UInt32"/> to <see cref="JToken"/>.
 
1225
    /// </summary>
 
1226
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1227
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1228
    [CLSCompliant(false)]
 
1229
    public static implicit operator JToken(uint value)
 
1230
    {
 
1231
      return new JValue(value);
 
1232
    }
 
1233
 
 
1234
    /// <summary>
 
1235
    /// Performs an implicit conversion from <see cref="UInt64"/> to <see cref="JToken"/>.
 
1236
    /// </summary>
 
1237
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1238
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1239
    [CLSCompliant(false)]
 
1240
    public static implicit operator JToken(ulong value)
 
1241
    {
 
1242
      return new JValue(value);
 
1243
    }
 
1244
 
 
1245
    /// <summary>
 
1246
    /// Performs an implicit conversion from <see cref="T:System.Byte[]"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1247
    /// </summary>
 
1248
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1249
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1250
    public static implicit operator JToken(byte[] value)
 
1251
    {
 
1252
      return new JValue(value);
 
1253
    }
 
1254
 
 
1255
    /// <summary>
 
1256
    /// Performs an implicit conversion from <see cref="T:System.Uri"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1257
    /// </summary>
 
1258
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1259
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1260
    public static implicit operator JToken(Uri value)
 
1261
    {
 
1262
      return new JValue(value);
 
1263
    }
 
1264
 
 
1265
    /// <summary>
 
1266
    /// Performs an implicit conversion from <see cref="T:System.TimeSpan"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1267
    /// </summary>
 
1268
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1269
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1270
    public static implicit operator JToken(TimeSpan value)
 
1271
    {
 
1272
      return new JValue(value);
 
1273
    }
 
1274
 
 
1275
    /// <summary>
 
1276
    /// Performs an implicit conversion from <see cref="Nullable{TimeSpan}"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1277
    /// </summary>
 
1278
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1279
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1280
    public static implicit operator JToken(TimeSpan? value)
 
1281
    {
 
1282
      return new JValue(value);
 
1283
    }
 
1284
 
 
1285
    /// <summary>
 
1286
    /// Performs an implicit conversion from <see cref="T:System.Guid"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1287
    /// </summary>
 
1288
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1289
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1290
    public static implicit operator JToken(Guid value)
 
1291
    {
 
1292
      return new JValue(value);
 
1293
    }
 
1294
 
 
1295
    /// <summary>
 
1296
    /// Performs an implicit conversion from <see cref="Nullable{Guid}"/> to <see cref="Newtonsoft.Json.Linq.JToken"/>.
 
1297
    /// </summary>
 
1298
    /// <param name="value">The value to create a <see cref="JValue"/> from.</param>
 
1299
    /// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
 
1300
    public static implicit operator JToken(Guid? value)
 
1301
    {
 
1302
      return new JValue(value);
 
1303
    }
 
1304
    #endregion
 
1305
 
 
1306
    IEnumerator IEnumerable.GetEnumerator()
 
1307
    {
 
1308
      return ((IEnumerable<JToken>)this).GetEnumerator();
 
1309
    }
 
1310
 
 
1311
    IEnumerator<JToken> IEnumerable<JToken>.GetEnumerator()
 
1312
    {
 
1313
      return Children().GetEnumerator();
 
1314
    }
 
1315
 
 
1316
    internal abstract int GetDeepHashCode();
 
1317
 
 
1318
    IJEnumerable<JToken> IJEnumerable<JToken>.this[object key]
 
1319
    {
 
1320
      get { return this[key]; }
 
1321
    }
 
1322
 
 
1323
    /// <summary>
 
1324
    /// Creates an <see cref="JsonReader"/> for this token.
 
1325
    /// </summary>
 
1326
    /// <returns>An <see cref="JsonReader"/> that can be used to read this token and its descendants.</returns>
 
1327
    public JsonReader CreateReader()
 
1328
    {
 
1329
      return new JTokenReader(this);
 
1330
    }
 
1331
 
 
1332
    internal static JToken FromObjectInternal(object o, JsonSerializer jsonSerializer)
 
1333
    {
 
1334
      ValidationUtils.ArgumentNotNull(o, "o");
 
1335
      ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
 
1336
 
 
1337
      JToken token;
 
1338
      using (JTokenWriter jsonWriter = new JTokenWriter())
 
1339
      {
 
1340
        jsonSerializer.Serialize(jsonWriter, o);
 
1341
        token = jsonWriter.Token;
 
1342
      }
 
1343
 
 
1344
      return token;
 
1345
    }
 
1346
 
 
1347
    /// <summary>
 
1348
    /// Creates a <see cref="JToken"/> from an object.
 
1349
    /// </summary>
 
1350
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
 
1351
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
 
1352
    public static JToken FromObject(object o)
 
1353
    {
 
1354
      return FromObjectInternal(o, new JsonSerializer());
 
1355
    }
 
1356
 
 
1357
    /// <summary>
 
1358
    /// Creates a <see cref="JToken"/> from an object using the specified <see cref="JsonSerializer"/>.
 
1359
    /// </summary>
 
1360
    /// <param name="o">The object that will be used to create <see cref="JToken"/>.</param>
 
1361
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when reading the object.</param>
 
1362
    /// <returns>A <see cref="JToken"/> with the value of the specified object</returns>
 
1363
    public static JToken FromObject(object o, JsonSerializer jsonSerializer)
 
1364
    {
 
1365
      return FromObjectInternal(o, jsonSerializer);
 
1366
    }
 
1367
 
 
1368
    /// <summary>
 
1369
    /// Creates the specified .NET type from the <see cref="JToken"/>.
 
1370
    /// </summary>
 
1371
    /// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
 
1372
    /// <returns>The new object created from the JSON value.</returns>
 
1373
    public T ToObject<T>()
 
1374
    {
 
1375
      return (T)ToObject(typeof(T));
 
1376
    }
 
1377
 
 
1378
    /// <summary>
 
1379
    /// Creates the specified .NET type from the <see cref="JToken"/>.
 
1380
    /// </summary>
 
1381
    /// <param name="objectType">The object type that the token will be deserialized to.</param>
 
1382
    /// <returns>The new object created from the JSON value.</returns>
 
1383
    public object ToObject(Type objectType)
 
1384
    {
 
1385
      return ToObject(objectType, false);
 
1386
    }
 
1387
 
 
1388
    private object ToObject(Type objectType, bool isNullable)
 
1389
    {
 
1390
      TypeCode typeCode = ConvertUtils.GetTypeCode(objectType);
 
1391
 
 
1392
      switch (typeCode)
 
1393
      {
 
1394
        case TypeCode.Object:
 
1395
          if (!isNullable && ReflectionUtils.IsNullableType(objectType))
 
1396
            return ToObject(Nullable.GetUnderlyingType(objectType), true);
 
1397
 
 
1398
          // fall back to standard JsonSerializer
 
1399
          break;
 
1400
        case TypeCode.Boolean:
 
1401
          if (isNullable)
 
1402
            return (bool?) this;
 
1403
 
 
1404
          return (bool) this;
 
1405
        case TypeCode.Char:
 
1406
          if (isNullable)
 
1407
            return (char?) this;
 
1408
 
 
1409
          return (char) this;
 
1410
        case TypeCode.SByte:
 
1411
          if (isNullable)
 
1412
            return (sbyte?) this;
 
1413
 
 
1414
          return (sbyte) this;
 
1415
        case TypeCode.Byte:
 
1416
          if (isNullable)
 
1417
            return (byte?) this;
 
1418
 
 
1419
          return (byte) this;
 
1420
        case TypeCode.Int16:
 
1421
          if (isNullable)
 
1422
            return (short?) this;
 
1423
 
 
1424
          return (short) this;
 
1425
        case TypeCode.UInt16:
 
1426
          if (isNullable)
 
1427
            return (ushort?) this;
 
1428
 
 
1429
          return (ushort) this;
 
1430
        case TypeCode.Int32:
 
1431
          if (isNullable)
 
1432
            return (int?) this;
 
1433
 
 
1434
          return (int) this;
 
1435
        case TypeCode.UInt32:
 
1436
          if (isNullable)
 
1437
            return (uint?) this;
 
1438
 
 
1439
          return (uint) this;
 
1440
        case TypeCode.Int64:
 
1441
          if (isNullable)
 
1442
            return (long?) this;
 
1443
 
 
1444
          return (long) this;
 
1445
        case TypeCode.UInt64:
 
1446
          if (isNullable)
 
1447
            return (ulong?) this;
 
1448
 
 
1449
          return (ulong) this;
 
1450
        case TypeCode.Single:
 
1451
          if (isNullable)
 
1452
            return (float?) this;
 
1453
 
 
1454
          return (float) this;
 
1455
        case TypeCode.Double:
 
1456
          if (isNullable)
 
1457
            return (double?) this;
 
1458
 
 
1459
          return (double) this;
 
1460
        case TypeCode.Decimal:
 
1461
          if (isNullable)
 
1462
            return (decimal?) this;
 
1463
 
 
1464
          return (decimal) this;
 
1465
        case TypeCode.DateTime:
 
1466
          if (isNullable)
 
1467
            return (DateTime?) this;
 
1468
 
 
1469
          return (DateTime) this;
 
1470
        case TypeCode.String:
 
1471
          return (string) this;
 
1472
      }
 
1473
 
 
1474
#if !PocketPC && !NET20
 
1475
      if (objectType == typeof (DateTimeOffset))
 
1476
      {
 
1477
        if (isNullable)
 
1478
          return (DateTimeOffset?) this;
 
1479
 
 
1480
        return (DateTimeOffset) this;
 
1481
      }
 
1482
#endif
 
1483
      if (objectType == typeof (Guid))
 
1484
      {
 
1485
        if (isNullable)
 
1486
          return (Guid?) this;
 
1487
 
 
1488
        return (Guid) this;
 
1489
      }
 
1490
      if (objectType == typeof (Uri))
 
1491
      {
 
1492
        return (Uri) this;
 
1493
      }
 
1494
      if (objectType == typeof (TimeSpan))
 
1495
      {
 
1496
        if (isNullable)
 
1497
          return (TimeSpan?) this;
 
1498
 
 
1499
        return (TimeSpan) this;
 
1500
      }
 
1501
 
 
1502
      return ToObject(objectType, new JsonSerializer());
 
1503
    }
 
1504
 
 
1505
    /// <summary>
 
1506
    /// Creates the specified .NET type from the <see cref="JToken"/> using the specified <see cref="JsonSerializer"/>.
 
1507
    /// </summary>
 
1508
    /// <typeparam name="T">The object type that the token will be deserialized to.</typeparam>
 
1509
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when creating the object.</param>
 
1510
    /// <returns>The new object created from the JSON value.</returns>
 
1511
    public T ToObject<T>(JsonSerializer jsonSerializer)
 
1512
    {
 
1513
      return (T)ToObject(typeof(T), jsonSerializer);
 
1514
    }
 
1515
 
 
1516
    /// <summary>
 
1517
    /// Creates the specified .NET type from the <see cref="JToken"/> using the specified <see cref="JsonSerializer"/>.
 
1518
    /// </summary>
 
1519
    /// <param name="objectType">The object type that the token will be deserialized to.</param>
 
1520
    /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> that will be used when creating the object.</param>
 
1521
    /// <returns>The new object created from the JSON value.</returns>
 
1522
    public object ToObject(Type objectType, JsonSerializer jsonSerializer)
 
1523
    {
 
1524
      ValidationUtils.ArgumentNotNull(jsonSerializer, "jsonSerializer");
 
1525
 
 
1526
      using (JTokenReader jsonReader = new JTokenReader(this))
 
1527
      {
 
1528
        return jsonSerializer.Deserialize(jsonReader, objectType);
 
1529
      }
 
1530
    }
 
1531
 
 
1532
    /// <summary>
 
1533
    /// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
 
1534
    /// </summary>
 
1535
    /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
 
1536
    /// <returns>
 
1537
    /// An <see cref="JToken"/> that contains the token and its descendant tokens
 
1538
    /// that were read from the reader. The runtime type of the token is determined
 
1539
    /// by the token type of the first token encountered in the reader.
 
1540
    /// </returns>
 
1541
    public static JToken ReadFrom(JsonReader reader)
 
1542
    {
 
1543
      ValidationUtils.ArgumentNotNull(reader, "reader");
 
1544
 
 
1545
      if (reader.TokenType == JsonToken.None)
 
1546
      {
 
1547
        if (!reader.Read())
 
1548
          throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
 
1549
      }
 
1550
 
 
1551
      if (reader.TokenType == JsonToken.StartObject)
 
1552
        return JObject.Load(reader);
 
1553
 
 
1554
      if (reader.TokenType == JsonToken.StartArray)
 
1555
        return JArray.Load(reader);
 
1556
 
 
1557
      if (reader.TokenType == JsonToken.PropertyName)
 
1558
        return JProperty.Load(reader);
 
1559
 
 
1560
      if (reader.TokenType == JsonToken.StartConstructor)
 
1561
        return JConstructor.Load(reader);
 
1562
 
 
1563
      if (!JsonReader.IsStartToken(reader.TokenType))
 
1564
        return new JValue(reader.Value);
 
1565
 
 
1566
      throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
 
1567
    }
 
1568
 
 
1569
    /// <summary>
 
1570
    /// Load a <see cref="JToken"/> from a string that contains JSON.
 
1571
    /// </summary>
 
1572
    /// <param name="json">A <see cref="String"/> that contains JSON.</param>
 
1573
    /// <returns>A <see cref="JToken"/> populated from the string that contains JSON.</returns>
 
1574
    public static JToken Parse(string json)
 
1575
    {
 
1576
      JsonReader reader = new JsonTextReader(new StringReader(json));
 
1577
 
 
1578
      JToken t = Load(reader);
 
1579
 
 
1580
      if (reader.Read() && reader.TokenType != JsonToken.Comment)
 
1581
        throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
 
1582
 
 
1583
      return t;
 
1584
    }
 
1585
 
 
1586
    /// <summary>
 
1587
    /// Creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
 
1588
    /// </summary>
 
1589
    /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
 
1590
    /// <returns>
 
1591
    /// An <see cref="JToken"/> that contains the token and its descendant tokens
 
1592
    /// that were read from the reader. The runtime type of the token is determined
 
1593
    /// by the token type of the first token encountered in the reader.
 
1594
    /// </returns>
 
1595
    public static JToken Load(JsonReader reader)
 
1596
    {
 
1597
      return ReadFrom(reader);
 
1598
    }
 
1599
 
 
1600
    internal void SetLineInfo(IJsonLineInfo lineInfo)
 
1601
    {
 
1602
      if (lineInfo == null || !lineInfo.HasLineInfo())
 
1603
        return;
 
1604
 
 
1605
      SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
 
1606
    }
 
1607
 
 
1608
    internal void SetLineInfo(int lineNumber, int linePosition)
 
1609
    {
 
1610
      _lineNumber = lineNumber;
 
1611
      _linePosition = linePosition;
 
1612
    }
 
1613
 
 
1614
    bool IJsonLineInfo.HasLineInfo()
 
1615
    {
 
1616
      return (_lineNumber != null && _linePosition != null);
 
1617
    }
 
1618
 
 
1619
    int IJsonLineInfo.LineNumber
 
1620
    {
 
1621
      get { return _lineNumber ?? 0; }
 
1622
    }
 
1623
 
 
1624
    int IJsonLineInfo.LinePosition
 
1625
    {
 
1626
      get { return _linePosition ?? 0; }
 
1627
    }
 
1628
 
 
1629
    /// <summary>
 
1630
    /// Selects the token that matches the object path.
 
1631
    /// </summary>
 
1632
    /// <param name="path">
 
1633
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
 
1634
    /// to be returned. This must be a string of property names or array indexes separated
 
1635
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
 
1636
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
 
1637
    /// </param>
 
1638
    /// <returns>The <see cref="JToken"/> that matches the object path or a null reference if no matching token is found.</returns>
 
1639
    public JToken SelectToken(string path)
 
1640
    {
 
1641
      return SelectToken(path, false);
 
1642
    }
 
1643
 
 
1644
    /// <summary>
 
1645
    /// Selects the token that matches the object path.
 
1646
    /// </summary>
 
1647
    /// <param name="path">
 
1648
    /// The object path from the current <see cref="JToken"/> to the <see cref="JToken"/>
 
1649
    /// to be returned. This must be a string of property names or array indexes separated
 
1650
    /// by periods, such as <code>Tables[0].DefaultView[0].Price</code> in C# or
 
1651
    /// <code>Tables(0).DefaultView(0).Price</code> in Visual Basic.
 
1652
    /// </param>
 
1653
    /// <param name="errorWhenNoMatch">A flag to indicate whether an error should be thrown if no token is found.</param>
 
1654
    /// <returns>The <see cref="JToken"/> that matches the object path.</returns>
 
1655
    public JToken SelectToken(string path, bool errorWhenNoMatch)
 
1656
    {
 
1657
      JPath p = new JPath(path);
 
1658
      return p.Evaluate(this, errorWhenNoMatch);
 
1659
    }
 
1660
 
 
1661
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
1662
    /// <summary>
 
1663
    /// Returns the <see cref="T:System.Dynamic.DynamicMetaObject"/> responsible for binding operations performed on this object.
 
1664
    /// </summary>
 
1665
    /// <param name="parameter">The expression tree representation of the runtime value.</param>
 
1666
    /// <returns>
 
1667
    /// The <see cref="T:System.Dynamic.DynamicMetaObject"/> to bind this object.
 
1668
    /// </returns>
 
1669
    protected virtual DynamicMetaObject GetMetaObject(Expression parameter)
 
1670
    {
 
1671
      return new DynamicProxyMetaObject<JToken>(parameter, this, new DynamicProxy<JToken>(), true);
 
1672
    }
 
1673
 
 
1674
    /// <summary>
 
1675
    /// Returns the <see cref="T:System.Dynamic.DynamicMetaObject"/> responsible for binding operations performed on this object.
 
1676
    /// </summary>
 
1677
    /// <param name="parameter">The expression tree representation of the runtime value.</param>
 
1678
    /// <returns>
 
1679
    /// The <see cref="T:System.Dynamic.DynamicMetaObject"/> to bind this object.
 
1680
    /// </returns>
 
1681
    DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
 
1682
    {
 
1683
      return GetMetaObject(parameter);
 
1684
    }
 
1685
#endif
 
1686
 
 
1687
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
1688
    object ICloneable.Clone()
 
1689
    {
 
1690
      return DeepClone();
 
1691
    }
 
1692
#endif
 
1693
 
 
1694
    /// <summary>
 
1695
    /// Creates a new instance of the <see cref="JToken"/>. All child tokens are recursively cloned.
 
1696
    /// </summary>
 
1697
    /// <returns>A new instance of the <see cref="JToken"/>.</returns>
 
1698
    public JToken DeepClone()
 
1699
    {
 
1700
      return CloneToken();
 
1701
    }
 
1702
  }
 
1703
}
 
 
b'\\ No newline at end of file'