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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/JsonSerializer.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
using System.Globalization;
 
29
using System.IO;
 
30
using System.Runtime.Serialization.Formatters;
 
31
using Newtonsoft.Json.Converters;
 
32
using Newtonsoft.Json.Serialization;
 
33
using Newtonsoft.Json.Utilities;
 
34
using System.Runtime.Serialization;
 
35
using ErrorEventArgs=Newtonsoft.Json.Serialization.ErrorEventArgs;
 
36
 
 
37
namespace Newtonsoft.Json
 
38
{
 
39
  /// <summary>
 
40
  /// Serializes and deserializes objects into and from the JSON format.
 
41
  /// The <see cref="JsonSerializer"/> enables you to control how objects are encoded into JSON.
 
42
  /// </summary>
 
43
  public class JsonSerializer
 
44
  {
 
45
    #region Properties
 
46
    private TypeNameHandling _typeNameHandling;
 
47
    private FormatterAssemblyStyle _typeNameAssemblyFormat;
 
48
    private PreserveReferencesHandling _preserveReferencesHandling;
 
49
    private ReferenceLoopHandling _referenceLoopHandling;
 
50
    private MissingMemberHandling _missingMemberHandling;
 
51
    private ObjectCreationHandling _objectCreationHandling;
 
52
    private NullValueHandling _nullValueHandling;
 
53
    private DefaultValueHandling _defaultValueHandling;
 
54
    private ConstructorHandling _constructorHandling;
 
55
    private JsonConverterCollection _converters;
 
56
    private IContractResolver _contractResolver;
 
57
    private IReferenceResolver _referenceResolver;
 
58
    private ITraceWriter _traceWriter;
 
59
    private SerializationBinder _binder;
 
60
    private StreamingContext _context;
 
61
    private Formatting? _formatting;
 
62
    private DateFormatHandling? _dateFormatHandling;
 
63
    private DateTimeZoneHandling? _dateTimeZoneHandling;
 
64
    private DateParseHandling? _dateParseHandling;
 
65
    private CultureInfo _culture;
 
66
    private int? _maxDepth;
 
67
    private bool _maxDepthSet;
 
68
    private bool? _checkAdditionalContent;
 
69
 
 
70
    /// <summary>
 
71
    /// Occurs when the <see cref="JsonSerializer"/> errors during serialization and deserialization.
 
72
    /// </summary>
 
73
    public virtual event EventHandler<ErrorEventArgs> Error;
 
74
 
 
75
    /// <summary>
 
76
    /// Gets or sets the <see cref="IReferenceResolver"/> used by the serializer when resolving references.
 
77
    /// </summary>
 
78
    public virtual IReferenceResolver ReferenceResolver
 
79
    {
 
80
      get
 
81
      {
 
82
        if (_referenceResolver == null)
 
83
          _referenceResolver = new DefaultReferenceResolver();
 
84
 
 
85
        return _referenceResolver;
 
86
      }
 
87
      set
 
88
      {
 
89
        if (value == null)
 
90
          throw new ArgumentNullException("value", "Reference resolver cannot be null.");
 
91
 
 
92
        _referenceResolver = value;
 
93
      }
 
94
    }
 
95
 
 
96
    /// <summary>
 
97
    /// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names.
 
98
    /// </summary>
 
99
    public virtual SerializationBinder Binder
 
100
    {
 
101
      get
 
102
      {
 
103
        return _binder;
 
104
      }
 
105
      set
 
106
      {
 
107
        if (value == null)
 
108
          throw new ArgumentNullException("value", "Serialization binder cannot be null.");
 
109
 
 
110
        _binder = value;
 
111
      }
 
112
    }
 
113
 
 
114
    /// <summary>
 
115
    /// Gets or sets the <see cref="ITraceWriter"/> used by the serializer when writing trace messages.
 
116
    /// </summary>
 
117
    /// <value>The trace writer.</value>
 
118
    public virtual ITraceWriter TraceWriter
 
119
    {
 
120
      get
 
121
      {
 
122
        return _traceWriter;
 
123
      }
 
124
      set
 
125
      {
 
126
        _traceWriter = value;
 
127
      }
 
128
    }
 
129
 
 
130
    /// <summary>
 
131
    /// Gets or sets how type name writing and reading is handled by the serializer.
 
132
    /// </summary>
 
133
    public virtual TypeNameHandling TypeNameHandling
 
134
    {
 
135
      get { return _typeNameHandling; }
 
136
      set
 
137
      {
 
138
        if (value < TypeNameHandling.None || value > TypeNameHandling.Auto)
 
139
          throw new ArgumentOutOfRangeException("value");
 
140
 
 
141
        _typeNameHandling = value;
 
142
      }
 
143
    }
 
144
 
 
145
    /// <summary>
 
146
    /// Gets or sets how a type name assembly is written and resolved by the serializer.
 
147
    /// </summary>
 
148
    /// <value>The type name assembly format.</value>
 
149
    public virtual FormatterAssemblyStyle TypeNameAssemblyFormat
 
150
    {
 
151
      get { return _typeNameAssemblyFormat; }
 
152
      set
 
153
      {
 
154
        if (value < FormatterAssemblyStyle.Simple || value > FormatterAssemblyStyle.Full)
 
155
          throw new ArgumentOutOfRangeException("value");
 
156
 
 
157
        _typeNameAssemblyFormat = value;
 
158
      }
 
159
    }
 
160
 
 
161
    /// <summary>
 
162
    /// Gets or sets how object references are preserved by the serializer.
 
163
    /// </summary>
 
164
    public virtual PreserveReferencesHandling PreserveReferencesHandling
 
165
    {
 
166
      get { return _preserveReferencesHandling; }
 
167
      set
 
168
      {
 
169
        if (value < PreserveReferencesHandling.None || value > PreserveReferencesHandling.All)
 
170
          throw new ArgumentOutOfRangeException("value");
 
171
 
 
172
        _preserveReferencesHandling = value;
 
173
      }
 
174
    }
 
175
 
 
176
    /// <summary>
 
177
    /// Get or set how reference loops (e.g. a class referencing itself) is handled.
 
178
    /// </summary>
 
179
    public virtual ReferenceLoopHandling ReferenceLoopHandling
 
180
    {
 
181
      get { return _referenceLoopHandling; }
 
182
      set
 
183
      {
 
184
        if (value < ReferenceLoopHandling.Error || value > ReferenceLoopHandling.Serialize)
 
185
          throw new ArgumentOutOfRangeException("value");
 
186
 
 
187
        _referenceLoopHandling = value;
 
188
      }
 
189
    }
 
190
 
 
191
    /// <summary>
 
192
    /// Get or set how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.
 
193
    /// </summary>
 
194
    public virtual MissingMemberHandling MissingMemberHandling
 
195
    {
 
196
      get { return _missingMemberHandling; }
 
197
      set
 
198
      {
 
199
        if (value < MissingMemberHandling.Ignore || value > MissingMemberHandling.Error)
 
200
          throw new ArgumentOutOfRangeException("value");
 
201
 
 
202
        _missingMemberHandling = value;
 
203
      }
 
204
    }
 
205
 
 
206
    /// <summary>
 
207
    /// Get or set how null values are handled during serialization and deserialization.
 
208
    /// </summary>
 
209
    public virtual NullValueHandling NullValueHandling
 
210
    {
 
211
      get { return _nullValueHandling; }
 
212
      set
 
213
      {
 
214
        if (value < NullValueHandling.Include || value > NullValueHandling.Ignore)
 
215
          throw new ArgumentOutOfRangeException("value");
 
216
 
 
217
        _nullValueHandling = value;
 
218
      }
 
219
    }
 
220
 
 
221
    /// <summary>
 
222
    /// Get or set how null default are handled during serialization and deserialization.
 
223
    /// </summary>
 
224
    public virtual DefaultValueHandling DefaultValueHandling
 
225
    {
 
226
      get { return _defaultValueHandling; }
 
227
      set
 
228
      {
 
229
        if (value < DefaultValueHandling.Include || value > DefaultValueHandling.IgnoreAndPopulate)
 
230
          throw new ArgumentOutOfRangeException("value");
 
231
 
 
232
        _defaultValueHandling = value;
 
233
      }
 
234
    }
 
235
 
 
236
    /// <summary>
 
237
    /// Gets or sets how objects are created during deserialization.
 
238
    /// </summary>
 
239
    /// <value>The object creation handling.</value>
 
240
    public virtual ObjectCreationHandling ObjectCreationHandling
 
241
    {
 
242
      get { return _objectCreationHandling; }
 
243
      set
 
244
      {
 
245
        if (value < ObjectCreationHandling.Auto || value > ObjectCreationHandling.Replace)
 
246
          throw new ArgumentOutOfRangeException("value");
 
247
 
 
248
        _objectCreationHandling = value;
 
249
      }
 
250
    }
 
251
 
 
252
    /// <summary>
 
253
    /// Gets or sets how constructors are used during deserialization.
 
254
    /// </summary>
 
255
    /// <value>The constructor handling.</value>
 
256
    public virtual ConstructorHandling ConstructorHandling
 
257
    {
 
258
      get { return _constructorHandling; }
 
259
      set
 
260
      {
 
261
        if (value < ConstructorHandling.Default || value > ConstructorHandling.AllowNonPublicDefaultConstructor)
 
262
          throw new ArgumentOutOfRangeException("value");
 
263
 
 
264
        _constructorHandling = value;
 
265
      }
 
266
    }
 
267
 
 
268
    /// <summary>
 
269
    /// Gets a collection <see cref="JsonConverter"/> that will be used during serialization.
 
270
    /// </summary>
 
271
    /// <value>Collection <see cref="JsonConverter"/> that will be used during serialization.</value>
 
272
    public virtual JsonConverterCollection Converters
 
273
    {
 
274
      get
 
275
      {
 
276
        if (_converters == null)
 
277
          _converters = new JsonConverterCollection();
 
278
 
 
279
        return _converters;
 
280
      }
 
281
    }
 
282
 
 
283
    /// <summary>
 
284
    /// Gets or sets the contract resolver used by the serializer when
 
285
    /// serializing .NET objects to JSON and vice versa.
 
286
    /// </summary>
 
287
    public virtual IContractResolver ContractResolver
 
288
    {
 
289
      get
 
290
      {
 
291
        if (_contractResolver == null)
 
292
          _contractResolver = DefaultContractResolver.Instance;
 
293
 
 
294
        return _contractResolver;
 
295
      }
 
296
      set { _contractResolver = value; }
 
297
    }
 
298
 
 
299
    /// <summary>
 
300
    /// Gets or sets the <see cref="StreamingContext"/> used by the serializer when invoking serialization callback methods.
 
301
    /// </summary>
 
302
    /// <value>The context.</value>
 
303
    public virtual StreamingContext Context
 
304
    {
 
305
      get { return _context; }
 
306
      set { _context = value; }
 
307
    }
 
308
 
 
309
    /// <summary>
 
310
    /// Indicates how JSON text output is formatted.
 
311
    /// </summary>
 
312
    public virtual Formatting Formatting
 
313
    {
 
314
      get { return _formatting ?? JsonSerializerSettings.DefaultFormatting; }
 
315
      set { _formatting = value; }
 
316
    }
 
317
 
 
318
    /// <summary>
 
319
    /// Get or set how dates are written to JSON text.
 
320
    /// </summary>
 
321
    public virtual DateFormatHandling DateFormatHandling
 
322
    {
 
323
      get { return _dateFormatHandling ?? JsonSerializerSettings.DefaultDateFormatHandling; }
 
324
      set { _dateFormatHandling = value; }
 
325
    }
 
326
 
 
327
    /// <summary>
 
328
    /// Get or set how <see cref="DateTime"/> time zones are handling during serialization and deserialization.
 
329
    /// </summary>
 
330
    public virtual DateTimeZoneHandling DateTimeZoneHandling
 
331
    {
 
332
      get { return _dateTimeZoneHandling ?? JsonSerializerSettings.DefaultDateTimeZoneHandling; }
 
333
      set { _dateTimeZoneHandling = value; }
 
334
    }
 
335
 
 
336
    /// <summary>
 
337
    /// Get or set how date formatted strings, e.g. "\/Date(1198908717056)\/" and "2012-03-21T05:40Z", are parsed when reading JSON.
 
338
    /// </summary>
 
339
    public virtual DateParseHandling DateParseHandling
 
340
    {
 
341
      get { return _dateParseHandling ?? JsonSerializerSettings.DefaultDateParseHandling; }
 
342
      set { _dateParseHandling = value; }
 
343
    }
 
344
 
 
345
    /// <summary>
 
346
    /// Gets or sets the culture used when reading JSON. Defaults to <see cref="CultureInfo.InvariantCulture"/>.
 
347
    /// </summary>
 
348
    public virtual CultureInfo Culture
 
349
    {
 
350
      get { return _culture ?? JsonSerializerSettings.DefaultCulture; }
 
351
      set { _culture = value; }
 
352
    }
 
353
 
 
354
    /// <summary>
 
355
    /// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref="JsonReaderException"/>.
 
356
    /// </summary>
 
357
    public virtual int? MaxDepth
 
358
    {
 
359
      get { return _maxDepth; }
 
360
      set
 
361
      {
 
362
        if (value <= 0)
 
363
          throw new ArgumentException("Value must be positive.", "value");
 
364
 
 
365
        _maxDepth = value;
 
366
        _maxDepthSet = true;
 
367
      }
 
368
    }
 
369
 
 
370
    /// <summary>
 
371
    /// Gets a value indicating whether there will be a check for additional JSON content after deserializing an object.
 
372
    /// </summary>
 
373
    /// <value>
 
374
    ///         <c>true</c> if there will be a check for additional JSON content after deserializing an object; otherwise, <c>false</c>.
 
375
    /// </value>
 
376
    public virtual bool CheckAdditionalContent
 
377
    {
 
378
      get { return _checkAdditionalContent ?? JsonSerializerSettings.DefaultCheckAdditionalContent; }
 
379
      set { _checkAdditionalContent = value; }
 
380
    }
 
381
 
 
382
    internal bool IsCheckAdditionalContentSet()
 
383
    {
 
384
      return (_checkAdditionalContent != null);
 
385
    }
 
386
 
 
387
    #endregion
 
388
 
 
389
    /// <summary>
 
390
    /// Initializes a new instance of the <see cref="JsonSerializer"/> class.
 
391
    /// </summary>
 
392
    public JsonSerializer()
 
393
    {
 
394
      _referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
 
395
      _missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
 
396
      _nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
 
397
      _defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
 
398
      _objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
 
399
      _preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
 
400
      _constructorHandling = JsonSerializerSettings.DefaultConstructorHandling;
 
401
      _typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling;
 
402
      _context = JsonSerializerSettings.DefaultContext;
 
403
      _binder = DefaultSerializationBinder.Instance;
 
404
    }
 
405
 
 
406
    /// <summary>
 
407
    /// Creates a new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.
 
408
    /// </summary>
 
409
    /// <param name="settings">The settings to be applied to the <see cref="JsonSerializer"/>.</param>
 
410
    /// <returns>A new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/>.</returns>
 
411
    public static JsonSerializer Create(JsonSerializerSettings settings)
 
412
    {
 
413
      JsonSerializer jsonSerializer = new JsonSerializer();
 
414
 
 
415
      if (settings != null)
 
416
      {
 
417
        if (!CollectionUtils.IsNullOrEmpty(settings.Converters))
 
418
          jsonSerializer.Converters.AddRange(settings.Converters);
 
419
 
 
420
        // serializer specific
 
421
        jsonSerializer.TypeNameHandling = settings.TypeNameHandling;
 
422
        jsonSerializer.TypeNameAssemblyFormat = settings.TypeNameAssemblyFormat;
 
423
        jsonSerializer.PreserveReferencesHandling = settings.PreserveReferencesHandling;
 
424
        jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling;
 
425
        jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling;
 
426
        jsonSerializer.ObjectCreationHandling = settings.ObjectCreationHandling;
 
427
        jsonSerializer.NullValueHandling = settings.NullValueHandling;
 
428
        jsonSerializer.DefaultValueHandling = settings.DefaultValueHandling;
 
429
        jsonSerializer.ConstructorHandling = settings.ConstructorHandling;
 
430
        jsonSerializer.Context = settings.Context;
 
431
        jsonSerializer._checkAdditionalContent = settings._checkAdditionalContent;
 
432
 
 
433
        // reader/writer specific
 
434
        // unset values won't override reader/writer set values
 
435
        jsonSerializer._formatting = settings._formatting;
 
436
        jsonSerializer._dateFormatHandling = settings._dateFormatHandling;
 
437
        jsonSerializer._dateTimeZoneHandling = settings._dateTimeZoneHandling;
 
438
        jsonSerializer._dateParseHandling = settings._dateParseHandling;
 
439
        jsonSerializer._culture = settings._culture;
 
440
        jsonSerializer._maxDepth = settings._maxDepth;
 
441
        jsonSerializer._maxDepthSet = settings._maxDepthSet;
 
442
 
 
443
        if (settings.Error != null)
 
444
          jsonSerializer.Error += settings.Error;
 
445
 
 
446
        if (settings.ContractResolver != null)
 
447
          jsonSerializer.ContractResolver = settings.ContractResolver;
 
448
        if (settings.ReferenceResolver != null)
 
449
          jsonSerializer.ReferenceResolver = settings.ReferenceResolver;
 
450
        if (settings.TraceWriter != null)
 
451
          jsonSerializer.TraceWriter = settings.TraceWriter;
 
452
        if (settings.Binder != null)
 
453
          jsonSerializer.Binder = settings.Binder;
 
454
      }
 
455
 
 
456
      return jsonSerializer;
 
457
    }
 
458
 
 
459
    /// <summary>
 
460
    /// Populates the JSON values onto the target object.
 
461
    /// </summary>
 
462
    /// <param name="reader">The <see cref="TextReader"/> that contains the JSON structure to reader values from.</param>
 
463
    /// <param name="target">The target object to populate values onto.</param>
 
464
    public void Populate(TextReader reader, object target)
 
465
    {
 
466
      Populate(new JsonTextReader(reader), target);
 
467
    }
 
468
 
 
469
    /// <summary>
 
470
    /// Populates the JSON values onto the target object.
 
471
    /// </summary>
 
472
    /// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to reader values from.</param>
 
473
    /// <param name="target">The target object to populate values onto.</param>
 
474
    public void Populate(JsonReader reader, object target)
 
475
    {
 
476
      PopulateInternal(reader, target);
 
477
    }
 
478
 
 
479
    internal virtual void PopulateInternal(JsonReader reader, object target)
 
480
    {
 
481
      ValidationUtils.ArgumentNotNull(reader, "reader");
 
482
      ValidationUtils.ArgumentNotNull(target, "target");
 
483
 
 
484
      JsonSerializerInternalReader serializerReader = new JsonSerializerInternalReader(this);
 
485
      serializerReader.Populate(reader, target);
 
486
    }
 
487
 
 
488
    /// <summary>
 
489
    /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>.
 
490
    /// </summary>
 
491
    /// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to deserialize.</param>
 
492
    /// <returns>The <see cref="Object"/> being deserialized.</returns>
 
493
    public object Deserialize(JsonReader reader)
 
494
    {
 
495
      return Deserialize(reader, null);
 
496
    }
 
497
 
 
498
    /// <summary>
 
499
    /// Deserializes the Json structure contained by the specified <see cref="StringReader"/>
 
500
    /// into an instance of the specified type.
 
501
    /// </summary>
 
502
    /// <param name="reader">The <see cref="TextReader"/> containing the object.</param>
 
503
    /// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param>
 
504
    /// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns>
 
505
    public object Deserialize(TextReader reader, Type objectType)
 
506
    {
 
507
      return Deserialize(new JsonTextReader(reader), objectType);
 
508
    }
 
509
 
 
510
    /// <summary>
 
511
    /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>
 
512
    /// into an instance of the specified type.
 
513
    /// </summary>
 
514
    /// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
 
515
    /// <typeparam name="T">The type of the object to deserialize.</typeparam>
 
516
    /// <returns>The instance of <typeparamref name="T"/> being deserialized.</returns>
 
517
    public T Deserialize<T>(JsonReader reader)
 
518
    {
 
519
      return (T)Deserialize(reader, typeof(T));
 
520
    }
 
521
 
 
522
    /// <summary>
 
523
    /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>
 
524
    /// into an instance of the specified type.
 
525
    /// </summary>
 
526
    /// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
 
527
    /// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param>
 
528
    /// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns>
 
529
    public object Deserialize(JsonReader reader, Type objectType)
 
530
    {
 
531
      return DeserializeInternal(reader, objectType);
 
532
    }
 
533
 
 
534
    internal virtual object DeserializeInternal(JsonReader reader, Type objectType)
 
535
    {
 
536
      ValidationUtils.ArgumentNotNull(reader, "reader");
 
537
 
 
538
      // set serialization options onto reader
 
539
      CultureInfo previousCulture = null;
 
540
      if (_culture != null && reader.Culture != _culture)
 
541
      {
 
542
        previousCulture = reader.Culture;
 
543
        reader.Culture = _culture;
 
544
      }
 
545
      DateTimeZoneHandling? previousDateTimeZoneHandling = null;
 
546
      if (_dateTimeZoneHandling != null && reader.DateTimeZoneHandling != _dateTimeZoneHandling)
 
547
      {
 
548
        previousDateTimeZoneHandling = reader.DateTimeZoneHandling;
 
549
        reader.DateTimeZoneHandling = _dateTimeZoneHandling.Value;
 
550
      }
 
551
      DateParseHandling? previousDateParseHandling = null;
 
552
      if (_dateParseHandling != null && reader.DateParseHandling != _dateParseHandling)
 
553
      {
 
554
        previousDateParseHandling = reader.DateParseHandling;
 
555
        reader.DateParseHandling = _dateParseHandling.Value;
 
556
      }
 
557
      int? previousMaxDepth = null;
 
558
      if (_maxDepthSet && reader.MaxDepth != _maxDepth)
 
559
      {
 
560
        previousMaxDepth = reader.MaxDepth;
 
561
        reader.MaxDepth = _maxDepth;
 
562
      }
 
563
 
 
564
      JsonSerializerInternalReader serializerReader = new JsonSerializerInternalReader(this);
 
565
      object value = serializerReader.Deserialize(reader, objectType, CheckAdditionalContent);
 
566
 
 
567
      // reset reader back to previous options
 
568
      if (previousCulture != null)
 
569
        reader.Culture = previousCulture;
 
570
      if (previousDateTimeZoneHandling != null)
 
571
        reader.DateTimeZoneHandling = previousDateTimeZoneHandling.Value;
 
572
      if (previousDateParseHandling != null)
 
573
        reader.DateParseHandling = previousDateParseHandling.Value;
 
574
      if (_maxDepthSet)
 
575
        reader.MaxDepth = previousMaxDepth;
 
576
 
 
577
      return value;
 
578
    }
 
579
 
 
580
    /// <summary>
 
581
    /// Serializes the specified <see cref="Object"/> and writes the Json structure
 
582
    /// to a <c>Stream</c> using the specified <see cref="TextWriter"/>. 
 
583
    /// </summary>
 
584
    /// <param name="textWriter">The <see cref="TextWriter"/> used to write the Json structure.</param>
 
585
    /// <param name="value">The <see cref="Object"/> to serialize.</param>
 
586
    public void Serialize(TextWriter textWriter, object value)
 
587
    {
 
588
      Serialize(new JsonTextWriter(textWriter), value);
 
589
    }
 
590
 
 
591
    /// <summary>
 
592
    /// Serializes the specified <see cref="Object"/> and writes the Json structure
 
593
    /// to a <c>Stream</c> using the specified <see cref="JsonWriter"/>. 
 
594
    /// </summary>
 
595
    /// <param name="jsonWriter">The <see cref="JsonWriter"/> used to write the Json structure.</param>
 
596
    /// <param name="value">The <see cref="Object"/> to serialize.</param>
 
597
    public void Serialize(JsonWriter jsonWriter, object value)
 
598
    {
 
599
      SerializeInternal(jsonWriter, value);
 
600
    }
 
601
 
 
602
    internal virtual void SerializeInternal(JsonWriter jsonWriter, object value)
 
603
    {
 
604
      ValidationUtils.ArgumentNotNull(jsonWriter, "jsonWriter");
 
605
 
 
606
      // set serialization options onto writer
 
607
      Formatting? previousFormatting = null;
 
608
      if (_formatting != null && jsonWriter.Formatting != _formatting)
 
609
      {
 
610
        previousFormatting = jsonWriter.Formatting;
 
611
        jsonWriter.Formatting = _formatting.Value;
 
612
      }
 
613
      DateFormatHandling? previousDateFormatHandling = null;
 
614
      if (_dateFormatHandling != null && jsonWriter.DateFormatHandling != _dateFormatHandling)
 
615
      {
 
616
        previousDateFormatHandling = jsonWriter.DateFormatHandling;
 
617
        jsonWriter.DateFormatHandling = _dateFormatHandling.Value;
 
618
      }
 
619
      DateTimeZoneHandling? previousDateTimeZoneHandling = null;
 
620
      if (_dateTimeZoneHandling != null && jsonWriter.DateTimeZoneHandling != _dateTimeZoneHandling)
 
621
      {
 
622
        previousDateTimeZoneHandling = jsonWriter.DateTimeZoneHandling;
 
623
        jsonWriter.DateTimeZoneHandling = _dateTimeZoneHandling.Value;
 
624
      }
 
625
      
 
626
      JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(this);
 
627
      serializerWriter.Serialize(jsonWriter, value);
 
628
 
 
629
      // reset writer back to previous options
 
630
      if (previousFormatting != null)
 
631
        jsonWriter.Formatting = previousFormatting.Value;
 
632
      if (previousDateFormatHandling != null)
 
633
        jsonWriter.DateFormatHandling = previousDateFormatHandling.Value;
 
634
      if (previousDateTimeZoneHandling != null)
 
635
        jsonWriter.DateTimeZoneHandling = previousDateTimeZoneHandling.Value;
 
636
    }
 
637
 
 
638
    internal JsonConverter GetMatchingConverter(Type type)
 
639
    {
 
640
      return GetMatchingConverter(_converters, type);
 
641
    }
 
642
 
 
643
    internal static JsonConverter GetMatchingConverter(IList<JsonConverter> converters, Type objectType)
 
644
    {
 
645
#if DEBUG
 
646
      ValidationUtils.ArgumentNotNull(objectType, "objectType");
 
647
#endif
 
648
 
 
649
      if (converters != null)
 
650
      {
 
651
        for (int i = 0; i < converters.Count; i++)
 
652
        {
 
653
          JsonConverter converter = converters[i];
 
654
 
 
655
          if (converter.CanConvert(objectType))
 
656
            return converter;
 
657
        }
 
658
      }
 
659
 
 
660
      return null;
 
661
    }
 
662
 
 
663
    internal void OnError(ErrorEventArgs e)
 
664
    {
 
665
      EventHandler<ErrorEventArgs> error = Error;
 
666
      if (error != null)
 
667
        error(this, e);
 
668
    }
 
669
  }
 
670
}
 
 
b'\\ No newline at end of file'