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

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.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;
 
28
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
 
29
using System.Collections.Concurrent;
 
30
#endif
 
31
using System.Collections.Generic;
 
32
using System.ComponentModel;
 
33
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
34
using System.Dynamic;
 
35
#endif
 
36
using System.Globalization;
 
37
using System.Reflection;
 
38
using System.Runtime.Serialization;
 
39
#if !(NETFX_CORE || PORTABLE)
 
40
using System.Security.Permissions;
 
41
#endif
 
42
using System.Xml.Serialization;
 
43
using Newtonsoft.Json.Converters;
 
44
using Newtonsoft.Json.Utilities;
 
45
using Newtonsoft.Json.Linq;
 
46
using System.Runtime.CompilerServices;
 
47
#if NETFX_CORE || PORTABLE
 
48
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
 
49
#endif
 
50
#if NET20
 
51
using Newtonsoft.Json.Utilities.LinqBridge;
 
52
#else
 
53
using System.Linq;
 
54
#endif
 
55
 
 
56
namespace Newtonsoft.Json.Serialization
 
57
{
 
58
  internal struct ResolverContractKey : IEquatable<ResolverContractKey>
 
59
  {
 
60
    private readonly Type _resolverType;
 
61
    private readonly Type _contractType;
 
62
 
 
63
    public ResolverContractKey(Type resolverType, Type contractType)
 
64
    {
 
65
      _resolverType = resolverType;
 
66
      _contractType = contractType;
 
67
    }
 
68
 
 
69
    public override int GetHashCode()
 
70
    {
 
71
      return _resolverType.GetHashCode() ^ _contractType.GetHashCode();
 
72
    }
 
73
 
 
74
    public override bool Equals(object obj)
 
75
    {
 
76
      if (!(obj is ResolverContractKey))
 
77
        return false;
 
78
 
 
79
      return Equals((ResolverContractKey)obj);
 
80
    }
 
81
 
 
82
    public bool Equals(ResolverContractKey other)
 
83
    {
 
84
      return (_resolverType == other._resolverType && _contractType == other._contractType);
 
85
    }
 
86
  }
 
87
 
 
88
  /// <summary>
 
89
  /// Used by <see cref="JsonSerializer"/> to resolves a <see cref="JsonContract"/> for a given <see cref="Type"/>.
 
90
  /// </summary>
 
91
  public class DefaultContractResolver : IContractResolver
 
92
  {
 
93
    private static readonly IContractResolver _instance = new DefaultContractResolver(true);
 
94
    internal static IContractResolver Instance
 
95
    {
 
96
        get { return _instance; }
 
97
    }
 
98
    private static readonly IList<JsonConverter> BuiltInConverters = new List<JsonConverter>
 
99
      {
 
100
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
101
        new EntityKeyMemberConverter(),
 
102
#endif
 
103
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
104
        new ExpandoObjectConverter(),
 
105
#endif
 
106
#if (!(SILVERLIGHT || PORTABLE) || WINDOWS_PHONE)
 
107
        new XmlNodeConverter(),
 
108
#endif
 
109
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
110
        new BinaryConverter(),
 
111
        new DataSetConverter(),
 
112
        new DataTableConverter(),
 
113
#endif
 
114
#if NETFX_CORE
 
115
        new JsonValueConverter(),
 
116
#endif
 
117
        new KeyValuePairConverter(),
 
118
        new BsonObjectIdConverter()
 
119
      };
 
120
 
 
121
    private static Dictionary<ResolverContractKey, JsonContract> _sharedContractCache;
 
122
    private static readonly object _typeContractCacheLock = new object();
 
123
 
 
124
    private Dictionary<ResolverContractKey, JsonContract> _instanceContractCache;
 
125
    private readonly bool _sharedCache;
 
126
 
 
127
    /// <summary>
 
128
    /// Gets a value indicating whether members are being get and set using dynamic code generation.
 
129
    /// This value is determined by the runtime permissions available.
 
130
    /// </summary>
 
131
    /// <value>
 
132
    ///         <c>true</c> if using dynamic code generation; otherwise, <c>false</c>.
 
133
    /// </value>
 
134
    public bool DynamicCodeGeneration
 
135
    {
 
136
      get { return JsonTypeReflector.DynamicCodeGeneration; }
 
137
    }
 
138
 
 
139
#if !NETFX_CORE
 
140
    /// <summary>
 
141
    /// Gets or sets the default members search flags.
 
142
    /// </summary>
 
143
    /// <value>The default members search flags.</value>
 
144
    public BindingFlags DefaultMembersSearchFlags { get; set; }
 
145
#else
 
146
    private BindingFlags DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public;
 
147
#endif
 
148
 
 
149
    /// <summary>
 
150
    /// Gets or sets a value indicating whether compiler generated members should be serialized.
 
151
    /// </summary>
 
152
    /// <value>
 
153
    ///         <c>true</c> if serialized compiler generated members; otherwise, <c>false</c>.
 
154
    /// </value>
 
155
    public bool SerializeCompilerGeneratedMembers { get; set; }
 
156
 
 
157
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
158
    /// <summary>
 
159
    /// Gets or sets a value indicating whether to ignore the <see cref="ISerializable"/> interface when serializing and deserializing types.
 
160
    /// </summary>
 
161
    /// <value>
 
162
    ///         <c>true</c> if the <see cref="ISerializable"/> interface will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
 
163
    /// </value>
 
164
    public bool IgnoreSerializableInterface { get; set; }
 
165
 
 
166
    /// <summary>
 
167
    /// Gets or sets a value indicating whether to ignore the <see cref="SerializableAttribute"/> attribute when serializing and deserializing types.
 
168
    /// </summary>
 
169
    /// <value>
 
170
    ///         <c>true</c> if the <see cref="SerializableAttribute"/> attribute will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
 
171
    /// </value>
 
172
    public bool IgnoreSerializableAttribute { get; set; }
 
173
#endif
 
174
 
 
175
    /// <summary>
 
176
    /// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
 
177
    /// </summary>
 
178
    public DefaultContractResolver()
 
179
      : this(false)
 
180
    {
 
181
    }
 
182
 
 
183
    /// <summary>
 
184
    /// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
 
185
    /// </summary>
 
186
    /// <param name="shareCache">
 
187
    /// If set to <c>true</c> the <see cref="DefaultContractResolver"/> will use a cached shared with other resolvers of the same type.
 
188
    /// Sharing the cache will significantly performance because expensive reflection will only happen once but could cause unexpected
 
189
    /// behavior if different instances of the resolver are suppose to produce different results. When set to false it is highly
 
190
    /// recommended to reuse <see cref="DefaultContractResolver"/> instances with the <see cref="JsonSerializer"/>.
 
191
    /// </param>
 
192
    public DefaultContractResolver(bool shareCache)
 
193
    {
 
194
#if !NETFX_CORE
 
195
      DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance;
 
196
#endif
 
197
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
198
      IgnoreSerializableAttribute = true;
 
199
#endif
 
200
 
 
201
      _sharedCache = shareCache;
 
202
    }
 
203
 
 
204
    private Dictionary<ResolverContractKey, JsonContract> GetCache()
 
205
    {
 
206
      if (_sharedCache)
 
207
        return _sharedContractCache;
 
208
      else
 
209
        return _instanceContractCache;
 
210
    }
 
211
 
 
212
    private void UpdateCache(Dictionary<ResolverContractKey, JsonContract> cache)
 
213
    {
 
214
      if (_sharedCache)
 
215
        _sharedContractCache = cache;
 
216
      else
 
217
        _instanceContractCache = cache;
 
218
    }
 
219
 
 
220
    /// <summary>
 
221
    /// Resolves the contract for a given type.
 
222
    /// </summary>
 
223
    /// <param name="type">The type to resolve a contract for.</param>
 
224
    /// <returns>The contract for a given type.</returns>
 
225
    public virtual JsonContract ResolveContract(Type type)
 
226
    {
 
227
      if (type == null)
 
228
        throw new ArgumentNullException("type");
 
229
 
 
230
      JsonContract contract;
 
231
      ResolverContractKey key = new ResolverContractKey(GetType(), type);
 
232
      Dictionary<ResolverContractKey, JsonContract> cache = GetCache();
 
233
      if (cache == null || !cache.TryGetValue(key, out contract))
 
234
      {
 
235
        contract = CreateContract(type);
 
236
 
 
237
        // avoid the possibility of modifying the cache dictionary while another thread is accessing it
 
238
        lock (_typeContractCacheLock)
 
239
        {
 
240
          cache = GetCache();
 
241
          Dictionary<ResolverContractKey, JsonContract> updatedCache =
 
242
            (cache != null)
 
243
              ? new Dictionary<ResolverContractKey, JsonContract>(cache)
 
244
              : new Dictionary<ResolverContractKey, JsonContract>();
 
245
          updatedCache[key] = contract;
 
246
 
 
247
          UpdateCache(updatedCache);
 
248
        }
 
249
      }
 
250
 
 
251
      return contract;
 
252
    }
 
253
 
 
254
    /// <summary>
 
255
    /// Gets the serializable members for the type.
 
256
    /// </summary>
 
257
    /// <param name="objectType">The type to get serializable members for.</param>
 
258
    /// <returns>The serializable members for the type.</returns>
 
259
    protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)
 
260
    {
 
261
      bool ignoreSerializableAttribute;
 
262
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
263
      ignoreSerializableAttribute = IgnoreSerializableAttribute;
 
264
#else
 
265
      ignoreSerializableAttribute = true;
 
266
#endif
 
267
 
 
268
      MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
 
269
 
 
270
      List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
 
271
        .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
 
272
 
 
273
      List<MemberInfo> serializableMembers = new List<MemberInfo>();
 
274
      
 
275
      if (memberSerialization != MemberSerialization.Fields)
 
276
      {
 
277
#if !PocketPC && !NET20
 
278
        DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
 
279
#endif
 
280
 
 
281
        List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
 
282
         .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
 
283
        
 
284
        foreach (MemberInfo member in allMembers)
 
285
        {
 
286
          // exclude members that are compiler generated if set
 
287
          if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof (CompilerGeneratedAttribute), true))
 
288
          {
 
289
            if (defaultMembers.Contains(member))
 
290
            {
 
291
              // add all members that are found by default member search
 
292
              serializableMembers.Add(member);
 
293
            }
 
294
            else
 
295
            {
 
296
              // add members that are explicitly marked with JsonProperty/DataMember attribute
 
297
              // or are a field if serializing just fields
 
298
              if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member.GetCustomAttributeProvider()) != null)
 
299
                serializableMembers.Add(member);
 
300
#if !PocketPC && !NET20
 
301
              else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member.GetCustomAttributeProvider()) != null)
 
302
                serializableMembers.Add(member);
 
303
#endif
 
304
              else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
 
305
                serializableMembers.Add(member);
 
306
            }
 
307
          }
 
308
        }
 
309
 
 
310
#if !PocketPC && !SILVERLIGHT && !NET20
 
311
        Type match;
 
312
        // don't include EntityKey on entities objects... this is a bit hacky
 
313
        if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match))
 
314
          serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
 
315
#endif
 
316
      }
 
317
      else
 
318
      {
 
319
        // serialize all fields
 
320
        foreach (MemberInfo member in allMembers)
 
321
        {
 
322
          if (member.MemberType() == MemberTypes.Field)
 
323
            serializableMembers.Add(member);
 
324
        }
 
325
      }
 
326
 
 
327
      return serializableMembers;
 
328
    }
 
329
 
 
330
#if !PocketPC && !SILVERLIGHT && !NET20
 
331
    private bool ShouldSerializeEntityMember(MemberInfo memberInfo)
 
332
    {
 
333
      PropertyInfo propertyInfo = memberInfo as PropertyInfo;
 
334
      if (propertyInfo != null)
 
335
      {
 
336
        if (propertyInfo.PropertyType.IsGenericType() && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName == "System.Data.Objects.DataClasses.EntityReference`1")
 
337
          return false;
 
338
      }
 
339
 
 
340
      return true;
 
341
    }
 
342
#endif
 
343
 
 
344
    /// <summary>
 
345
    /// Creates a <see cref="JsonObjectContract"/> for the given type.
 
346
    /// </summary>
 
347
    /// <param name="objectType">Type of the object.</param>
 
348
    /// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
 
349
    protected virtual JsonObjectContract CreateObjectContract(Type objectType)
 
350
    {
 
351
      JsonObjectContract contract = new JsonObjectContract(objectType);
 
352
      InitializeContract(contract);
 
353
 
 
354
      bool ignoreSerializableAttribute;
 
355
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
356
      ignoreSerializableAttribute = IgnoreSerializableAttribute;
 
357
#else
 
358
      ignoreSerializableAttribute = true;
 
359
#endif
 
360
 
 
361
      contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(contract.NonNullableUnderlyingType, ignoreSerializableAttribute);
 
362
      contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, contract.MemberSerialization));
 
363
 
 
364
      JsonObjectAttribute attribute = JsonTypeReflector.GetJsonObjectAttribute(contract.NonNullableUnderlyingType);
 
365
      if (attribute != null)
 
366
        contract.ItemRequired = attribute._itemRequired;
 
367
 
 
368
      ConstructorInfo overrideConstructor = GetAttributeConstructor(contract.NonNullableUnderlyingType);
 
369
        
 
370
      // check if a JsonConstructorAttribute has been defined and use that
 
371
      if (overrideConstructor != null)
 
372
      {
 
373
        contract.OverrideConstructor = overrideConstructor;
 
374
        contract.ConstructorParameters.AddRange(CreateConstructorParameters(overrideConstructor, contract.Properties));
 
375
      }
 
376
      else if (contract.MemberSerialization == MemberSerialization.Fields)
 
377
      {
 
378
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
379
        // mimic DataContractSerializer behaviour when populating fields by overriding default creator to create an uninitialized object
 
380
        // note that this is only possible when the application is fully trusted so fall back to using the default constructor (if available) in partial trust
 
381
        if (JsonTypeReflector.FullyTrusted)
 
382
          contract.DefaultCreator = contract.GetUninitializedObject;
 
383
#endif
 
384
      }
 
385
      else if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
 
386
      {
 
387
        ConstructorInfo constructor = GetParametrizedConstructor(contract.NonNullableUnderlyingType);
 
388
        if (constructor != null)
 
389
        {
 
390
          contract.ParametrizedConstructor = constructor;
 
391
          contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
 
392
        }
 
393
      }
 
394
      return contract;
 
395
    }
 
396
 
 
397
    private ConstructorInfo GetAttributeConstructor(Type objectType)
 
398
    {
 
399
      IList<ConstructorInfo> markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList();
 
400
 
 
401
      if (markedConstructors.Count > 1)
 
402
        throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
 
403
      else if (markedConstructors.Count == 1)
 
404
        return markedConstructors[0];
 
405
 
 
406
      // little hack to get Version objects to deserialize correctly
 
407
      if (objectType == typeof(Version))
 
408
        return objectType.GetConstructor(new [] { typeof(int), typeof(int), typeof(int), typeof(int) });
 
409
 
 
410
      return null;
 
411
    }
 
412
 
 
413
    private ConstructorInfo GetParametrizedConstructor(Type objectType)
 
414
    {
 
415
      IList<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList();
 
416
 
 
417
      if (constructors.Count == 1)
 
418
        return constructors[0];
 
419
      else
 
420
        return null;
 
421
    }
 
422
 
 
423
    /// <summary>
 
424
    /// Creates the constructor parameters.
 
425
    /// </summary>
 
426
    /// <param name="constructor">The constructor to create properties for.</param>
 
427
    /// <param name="memberProperties">The type's member properties.</param>
 
428
    /// <returns>Properties for the given <see cref="ConstructorInfo"/>.</returns>
 
429
    protected virtual IList<JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
 
430
    {
 
431
      var constructorParameters = constructor.GetParameters();
 
432
 
 
433
      JsonPropertyCollection parameterCollection = new JsonPropertyCollection(constructor.DeclaringType);
 
434
 
 
435
      foreach (ParameterInfo parameterInfo in constructorParameters)
 
436
      {
 
437
        // it is possible to generate a ParameterInfo with a null name using Reflection.Emit
 
438
        // protect against an ArgumentNullException from GetClosestMatchProperty by testing for null here
 
439
        JsonProperty matchingMemberProperty = (parameterInfo.Name != null) ? memberProperties.GetClosestMatchProperty(parameterInfo.Name) : null;
 
440
 
 
441
        // type must match as well as name
 
442
        if (matchingMemberProperty != null && matchingMemberProperty.PropertyType != parameterInfo.ParameterType)
 
443
          matchingMemberProperty = null;
 
444
 
 
445
        JsonProperty property = CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
 
446
 
 
447
        if (property != null)
 
448
        {
 
449
          parameterCollection.AddProperty(property);
 
450
        }
 
451
      }
 
452
 
 
453
      return parameterCollection;
 
454
    }
 
455
 
 
456
    /// <summary>
 
457
    /// Creates a <see cref="JsonProperty"/> for the given <see cref="ParameterInfo"/>.
 
458
    /// </summary>
 
459
    /// <param name="matchingMemberProperty">The matching member property.</param>
 
460
    /// <param name="parameterInfo">The constructor parameter.</param>
 
461
    /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="ParameterInfo"/>.</returns>
 
462
    protected virtual JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
 
463
    {
 
464
      JsonProperty property = new JsonProperty();
 
465
      property.PropertyType = parameterInfo.ParameterType;
 
466
 
 
467
      bool allowNonPublicAccess;
 
468
      SetPropertySettingsFromAttributes(property, parameterInfo.GetCustomAttributeProvider(), parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
 
469
 
 
470
      property.Readable = false;
 
471
      property.Writable = true;
 
472
 
 
473
      // "inherit" values from matching member property if unset on parameter
 
474
      if (matchingMemberProperty != null)
 
475
      {
 
476
        property.PropertyName = (property.PropertyName != parameterInfo.Name) ? property.PropertyName : matchingMemberProperty.PropertyName;
 
477
        property.Converter = property.Converter ?? matchingMemberProperty.Converter;
 
478
        property.MemberConverter = property.MemberConverter ?? matchingMemberProperty.MemberConverter;
 
479
 
 
480
        if (!property._hasExplicitDefaultValue && matchingMemberProperty._hasExplicitDefaultValue)
 
481
          property.DefaultValue = matchingMemberProperty.DefaultValue;
 
482
 
 
483
        property._required = property._required ?? matchingMemberProperty._required;
 
484
        property.IsReference = property.IsReference ?? matchingMemberProperty.IsReference;
 
485
        property.NullValueHandling = property.NullValueHandling ?? matchingMemberProperty.NullValueHandling;
 
486
        property.DefaultValueHandling = property.DefaultValueHandling ?? matchingMemberProperty.DefaultValueHandling;
 
487
        property.ReferenceLoopHandling = property.ReferenceLoopHandling ?? matchingMemberProperty.ReferenceLoopHandling;
 
488
        property.ObjectCreationHandling = property.ObjectCreationHandling ?? matchingMemberProperty.ObjectCreationHandling;
 
489
        property.TypeNameHandling = property.TypeNameHandling ?? matchingMemberProperty.TypeNameHandling;
 
490
      }
 
491
 
 
492
      return property;
 
493
    }
 
494
 
 
495
    /// <summary>
 
496
    /// Resolves the default <see cref="JsonConverter" /> for the contract.
 
497
    /// </summary>
 
498
    /// <param name="objectType">Type of the object.</param>
 
499
    /// <returns>The contract's default <see cref="JsonConverter" />.</returns>
 
500
    protected virtual JsonConverter ResolveContractConverter(Type objectType)
 
501
    {
 
502
      return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
 
503
    }
 
504
 
 
505
    private Func<object> GetDefaultCreator(Type createdType)
 
506
    {
 
507
      return JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
 
508
    }
 
509
 
 
510
#if !PocketPC && !NET20
 
511
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1903:UseOnlyApiFromTargetedFramework", MessageId = "System.Runtime.Serialization.DataContractAttribute.#get_IsReference()")]
 
512
#endif
 
513
    private void InitializeContract(JsonContract contract)
 
514
    {
 
515
      JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.NonNullableUnderlyingType);
 
516
      if (containerAttribute != null)
 
517
      {
 
518
        contract.IsReference = containerAttribute._isReference;
 
519
      }
 
520
#if !PocketPC && !NET20
 
521
      else
 
522
      {
 
523
        DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.NonNullableUnderlyingType);
 
524
        // doesn't have a null value
 
525
        if (dataContractAttribute != null && dataContractAttribute.IsReference)
 
526
          contract.IsReference = true;
 
527
      }
 
528
#endif
 
529
 
 
530
      contract.Converter = ResolveContractConverter(contract.NonNullableUnderlyingType);
 
531
 
 
532
      // then see whether object is compadible with any of the built in converters
 
533
      contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.NonNullableUnderlyingType);
 
534
 
 
535
      if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
 
536
        || contract.CreatedType.IsValueType())
 
537
      {
 
538
        contract.DefaultCreator = GetDefaultCreator(contract.CreatedType);
 
539
 
 
540
        contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType() &&
 
541
                                            ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
 
542
      }
 
543
 
 
544
      ResolveCallbackMethods(contract, contract.NonNullableUnderlyingType);
 
545
    }
 
546
 
 
547
    private void ResolveCallbackMethods(JsonContract contract, Type t)
 
548
    {
 
549
      if (t.BaseType() != null)
 
550
        ResolveCallbackMethods(contract, t.BaseType());
 
551
 
 
552
      MethodInfo onSerializing;
 
553
      MethodInfo onSerialized;
 
554
      MethodInfo onDeserializing;
 
555
      MethodInfo onDeserialized;
 
556
      MethodInfo onError;
 
557
 
 
558
      GetCallbackMethodsForType(t, out onSerializing, out onSerialized, out onDeserializing, out onDeserialized, out onError);
 
559
 
 
560
      if (onSerializing != null)
 
561
      {
 
562
#if NETFX_CORE
 
563
        if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
 
564
          contract.OnSerializing = onSerializing;
 
565
#else
 
566
        contract.OnSerializing = onSerializing;
 
567
#endif
 
568
      }
 
569
 
 
570
      if (onSerialized != null)
 
571
        contract.OnSerialized = onSerialized;
 
572
 
 
573
      if (onDeserializing != null)
 
574
        contract.OnDeserializing = onDeserializing;
 
575
 
 
576
      if (onDeserialized != null)
 
577
      {
 
578
        // ConcurrentDictionary throws an error here so don't use its OnDeserialized - http://json.codeplex.com/discussions/257093
 
579
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
 
580
        if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
 
581
          contract.OnDeserialized = onDeserialized;
 
582
#else
 
583
        contract.OnDeserialized = onDeserialized;
 
584
#endif
 
585
      }
 
586
 
 
587
      if (onError != null)
 
588
        contract.OnError = onError;
 
589
    }
 
590
 
 
591
    private void GetCallbackMethodsForType(Type type, out MethodInfo onSerializing, out MethodInfo onSerialized, out MethodInfo onDeserializing, out MethodInfo onDeserialized, out MethodInfo onError)
 
592
    {
 
593
      onSerializing = null;
 
594
      onSerialized = null;
 
595
      onDeserializing = null;
 
596
      onDeserialized = null;
 
597
      onError = null;
 
598
 
 
599
      foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
 
600
      {
 
601
        // compact framework errors when getting parameters for a generic method
 
602
        // lame, but generic methods should not be callbacks anyway
 
603
        if (method.ContainsGenericParameters)
 
604
          continue;
 
605
 
 
606
        Type prevAttributeType = null;
 
607
        ParameterInfo[] parameters = method.GetParameters();
 
608
 
 
609
        if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), onSerializing, ref prevAttributeType))
 
610
        {
 
611
          onSerializing = method;
 
612
        }
 
613
        if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), onSerialized, ref prevAttributeType))
 
614
        {
 
615
          onSerialized = method;
 
616
        }
 
617
        if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), onDeserializing, ref prevAttributeType))
 
618
        {
 
619
          onDeserializing = method;
 
620
        }
 
621
        if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), onDeserialized, ref prevAttributeType))
 
622
        {
 
623
          onDeserialized = method;
 
624
        }
 
625
        if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), onError, ref prevAttributeType))
 
626
        {
 
627
          onError = method;
 
628
        }
 
629
      }
 
630
    }
 
631
 
 
632
    /// <summary>
 
633
    /// Creates a <see cref="JsonDictionaryContract"/> for the given type.
 
634
    /// </summary>
 
635
    /// <param name="objectType">Type of the object.</param>
 
636
    /// <returns>A <see cref="JsonDictionaryContract"/> for the given type.</returns>
 
637
    protected virtual JsonDictionaryContract CreateDictionaryContract(Type objectType)
 
638
    {
 
639
      JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
 
640
      InitializeContract(contract);
 
641
 
 
642
      contract.PropertyNameResolver = ResolvePropertyName;
 
643
 
 
644
      return contract;
 
645
    }
 
646
 
 
647
    /// <summary>
 
648
    /// Creates a <see cref="JsonArrayContract"/> for the given type.
 
649
    /// </summary>
 
650
    /// <param name="objectType">Type of the object.</param>
 
651
    /// <returns>A <see cref="JsonArrayContract"/> for the given type.</returns>
 
652
    protected virtual JsonArrayContract CreateArrayContract(Type objectType)
 
653
    {
 
654
      JsonArrayContract contract = new JsonArrayContract(objectType);
 
655
      InitializeContract(contract);
 
656
 
 
657
      return contract;
 
658
    }
 
659
 
 
660
    /// <summary>
 
661
    /// Creates a <see cref="JsonPrimitiveContract"/> for the given type.
 
662
    /// </summary>
 
663
    /// <param name="objectType">Type of the object.</param>
 
664
    /// <returns>A <see cref="JsonPrimitiveContract"/> for the given type.</returns>
 
665
    protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
 
666
    {
 
667
      JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);
 
668
      InitializeContract(contract);
 
669
 
 
670
      return contract;
 
671
    }
 
672
 
 
673
    /// <summary>
 
674
    /// Creates a <see cref="JsonLinqContract"/> for the given type.
 
675
    /// </summary>
 
676
    /// <param name="objectType">Type of the object.</param>
 
677
    /// <returns>A <see cref="JsonLinqContract"/> for the given type.</returns>
 
678
    protected virtual JsonLinqContract CreateLinqContract(Type objectType)
 
679
    {
 
680
      JsonLinqContract contract = new JsonLinqContract(objectType);
 
681
      InitializeContract(contract);
 
682
 
 
683
      return contract;
 
684
    }
 
685
 
 
686
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
687
    /// <summary>
 
688
    /// Creates a <see cref="JsonISerializableContract"/> for the given type.
 
689
    /// </summary>
 
690
    /// <param name="objectType">Type of the object.</param>
 
691
    /// <returns>A <see cref="JsonISerializableContract"/> for the given type.</returns>
 
692
    protected virtual JsonISerializableContract CreateISerializableContract(Type objectType)
 
693
    {
 
694
      JsonISerializableContract contract = new JsonISerializableContract(objectType);
 
695
      InitializeContract(contract);
 
696
 
 
697
      ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
 
698
      if (constructorInfo != null)
 
699
      {
 
700
        MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructorInfo);
 
701
 
 
702
        contract.ISerializableCreator = (args => methodCall(null, args));
 
703
      }
 
704
 
 
705
      return contract;
 
706
    }
 
707
#endif
 
708
 
 
709
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
710
    /// <summary>
 
711
    /// Creates a <see cref="JsonDynamicContract"/> for the given type.
 
712
    /// </summary>
 
713
    /// <param name="objectType">Type of the object.</param>
 
714
    /// <returns>A <see cref="JsonDynamicContract"/> for the given type.</returns>
 
715
    protected virtual JsonDynamicContract CreateDynamicContract(Type objectType)
 
716
    {
 
717
      JsonDynamicContract contract = new JsonDynamicContract(objectType);
 
718
      InitializeContract(contract);
 
719
 
 
720
      contract.PropertyNameResolver = ResolvePropertyName;
 
721
      contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut));
 
722
 
 
723
      return contract;
 
724
    }
 
725
#endif
 
726
 
 
727
    /// <summary>
 
728
    /// Creates a <see cref="JsonStringContract"/> for the given type.
 
729
    /// </summary>
 
730
    /// <param name="objectType">Type of the object.</param>
 
731
    /// <returns>A <see cref="JsonStringContract"/> for the given type.</returns>
 
732
    protected virtual JsonStringContract CreateStringContract(Type objectType)
 
733
    {
 
734
      JsonStringContract contract = new JsonStringContract(objectType);
 
735
      InitializeContract(contract);
 
736
 
 
737
      return contract;
 
738
    }
 
739
 
 
740
    /// <summary>
 
741
    /// Determines which contract type is created for the given type.
 
742
    /// </summary>
 
743
    /// <param name="objectType">Type of the object.</param>
 
744
    /// <returns>A <see cref="JsonContract"/> for the given type.</returns>
 
745
    protected virtual JsonContract CreateContract(Type objectType)
 
746
    {
 
747
      Type t = ReflectionUtils.EnsureNotNullableType(objectType);
 
748
 
 
749
      if (JsonConvert.IsJsonPrimitiveType(t))
 
750
        return CreatePrimitiveContract(objectType);
 
751
 
 
752
      if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
 
753
        return CreateObjectContract(objectType);
 
754
 
 
755
      if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
 
756
        return CreateArrayContract(objectType);
 
757
 
 
758
      if (JsonTypeReflector.GetJsonDictionaryAttribute(t) != null)
 
759
        return CreateDictionaryContract(objectType);
 
760
 
 
761
      if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
 
762
        return CreateLinqContract(objectType);
 
763
 
 
764
      if (CollectionUtils.IsDictionaryType(t))
 
765
        return CreateDictionaryContract(objectType);
 
766
 
 
767
      if (typeof(IEnumerable).IsAssignableFrom(t))
 
768
        return CreateArrayContract(objectType);
 
769
 
 
770
      if (CanConvertToString(t))
 
771
        return CreateStringContract(objectType);
 
772
 
 
773
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
774
      if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t))
 
775
        return CreateISerializableContract(objectType);
 
776
#endif
 
777
 
 
778
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
 
779
      if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t))
 
780
        return CreateDynamicContract(objectType);
 
781
#endif
 
782
 
 
783
      return CreateObjectContract(objectType);
 
784
    }
 
785
 
 
786
    internal static bool CanConvertToString(Type type)
 
787
    {
 
788
#if !(NETFX_CORE || PORTABLE)
 
789
      TypeConverter converter = ConvertUtils.GetConverter(type);
 
790
 
 
791
      // use the objectType's TypeConverter if it has one and can convert to a string
 
792
      if (converter != null
 
793
#if !SILVERLIGHT
 
794
 && !(converter is ComponentConverter)
 
795
 && !(converter is ReferenceConverter)
 
796
#endif
 
797
 && converter.GetType() != typeof(TypeConverter))
 
798
      {
 
799
        if (converter.CanConvertTo(typeof(string)))
 
800
          return true;
 
801
      }
 
802
#endif
 
803
 
 
804
      if (type == typeof(Type) || type.IsSubclassOf(typeof(Type)))
 
805
        return true;
 
806
 
 
807
#if SILVERLIGHT || PocketPC
 
808
      if (type == typeof(Guid) || type == typeof(Uri) || type == typeof(TimeSpan))
 
809
        return true;
 
810
#endif
 
811
 
 
812
      return false;
 
813
    }
 
814
 
 
815
    private static bool IsValidCallback(MethodInfo method, ParameterInfo[] parameters, Type attributeType, MethodInfo currentCallback, ref Type prevAttributeType)
 
816
    {
 
817
      if (!method.IsDefined(attributeType, false))
 
818
        return false;
 
819
 
 
820
      if (currentCallback != null)
 
821
        throw new JsonException("Invalid attribute. Both '{0}' and '{1}' in type '{2}' have '{3}'.".FormatWith(CultureInfo.InvariantCulture, method, currentCallback, GetClrTypeFullName(method.DeclaringType), attributeType));
 
822
 
 
823
      if (prevAttributeType != null)
 
824
        throw new JsonException("Invalid Callback. Method '{3}' in type '{2}' has both '{0}' and '{1}'.".FormatWith(CultureInfo.InvariantCulture, prevAttributeType, attributeType, GetClrTypeFullName(method.DeclaringType), method));
 
825
 
 
826
      if (method.IsVirtual)
 
827
        throw new JsonException("Virtual Method '{0}' of type '{1}' cannot be marked with '{2}' attribute.".FormatWith(CultureInfo.InvariantCulture, method, GetClrTypeFullName(method.DeclaringType), attributeType));
 
828
 
 
829
      if (method.ReturnType != typeof(void))
 
830
        throw new JsonException("Serialization Callback '{1}' in type '{0}' must return void.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method));
 
831
 
 
832
      if (attributeType == typeof(OnErrorAttribute))
 
833
      {
 
834
        if (parameters == null || parameters.Length != 2 || parameters[0].ParameterType != typeof(StreamingContext) || parameters[1].ParameterType != typeof(ErrorContext))
 
835
          throw new JsonException("Serialization Error Callback '{1}' in type '{0}' must have two parameters of type '{2}' and '{3}'.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method, typeof(StreamingContext), typeof(ErrorContext)));
 
836
      }
 
837
      else
 
838
      {
 
839
        if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
 
840
          throw new JsonException("Serialization Callback '{1}' in type '{0}' must have a single parameter of type '{2}'.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method, typeof(StreamingContext)));
 
841
      }
 
842
 
 
843
      prevAttributeType = attributeType;
 
844
 
 
845
      return true;
 
846
    }
 
847
 
 
848
    internal static string GetClrTypeFullName(Type type)
 
849
    {
 
850
      if (type.IsGenericTypeDefinition() || !type.ContainsGenericParameters())
 
851
        return type.FullName;
 
852
 
 
853
      return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { type.Namespace, type.Name });
 
854
    }
 
855
 
 
856
    /// <summary>
 
857
    /// Creates properties for the given <see cref="JsonContract"/>.
 
858
    /// </summary>
 
859
    /// <param name="type">The type to create properties for.</param>
 
860
    /// /// <param name="memberSerialization">The member serialization mode for the type.</param>
 
861
    /// <returns>Properties for the given <see cref="JsonContract"/>.</returns>
 
862
    protected virtual IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
 
863
    {
 
864
      List<MemberInfo> members = GetSerializableMembers(type);
 
865
      if (members == null)
 
866
        throw new JsonSerializationException("Null collection of seralizable members returned.");
 
867
 
 
868
      JsonPropertyCollection properties = new JsonPropertyCollection(type);
 
869
 
 
870
      foreach (MemberInfo member in members)
 
871
      {
 
872
        JsonProperty property = CreateProperty(member, memberSerialization);
 
873
 
 
874
        if (property != null)
 
875
          properties.AddProperty(property);
 
876
      }
 
877
 
 
878
      IList<JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();
 
879
      return orderedProperties;
 
880
    }
 
881
 
 
882
    /// <summary>
 
883
    /// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member.
 
884
    /// </summary>
 
885
    /// <param name="member">The member.</param>
 
886
    /// <returns>The <see cref="IValueProvider"/> used by the serializer to get and set values from a member.</returns>
 
887
    protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
 
888
    {
 
889
      // warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes
 
890
      IValueProvider valueProvider;
 
891
 
 
892
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
 
893
      if (DynamicCodeGeneration)
 
894
        valueProvider = new DynamicValueProvider(member);
 
895
      else
 
896
        valueProvider = new ReflectionValueProvider(member);
 
897
#else
 
898
      valueProvider = new ReflectionValueProvider(member);
 
899
#endif
 
900
 
 
901
      return valueProvider;
 
902
    }
 
903
 
 
904
    /// <summary>
 
905
    /// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
 
906
    /// </summary>
 
907
    /// <param name="memberSerialization">The member's parent <see cref="MemberSerialization"/>.</param>
 
908
    /// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param>
 
909
    /// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns>
 
910
    protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
 
911
    {
 
912
      JsonProperty property = new JsonProperty();
 
913
      property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
 
914
      property.DeclaringType = member.DeclaringType;
 
915
      property.ValueProvider = CreateMemberValueProvider(member);
 
916
 
 
917
      bool allowNonPublicAccess;
 
918
      SetPropertySettingsFromAttributes(property, member.GetCustomAttributeProvider(), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
 
919
 
 
920
      if (memberSerialization != MemberSerialization.Fields)
 
921
      {
 
922
        property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
 
923
        property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, property.HasMemberAttribute);
 
924
      }
 
925
      else
 
926
      {
 
927
        // write to readonly fields
 
928
        property.Readable = true;
 
929
        property.Writable = true;
 
930
      }
 
931
      property.ShouldSerialize = CreateShouldSerializeTest(member);
 
932
 
 
933
      SetIsSpecifiedActions(property, member, allowNonPublicAccess);
 
934
 
 
935
      return property;
 
936
    }
 
937
 
 
938
    private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
 
939
    {
 
940
#if !PocketPC && !NET20
 
941
      DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
 
942
 
 
943
      MemberInfo memberInfo = null;
 
944
#if !(NETFX_CORE || PORTABLE)
 
945
      memberInfo = attributeProvider as MemberInfo;
 
946
#else
 
947
      memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
 
948
#endif
 
949
 
 
950
      DataMemberAttribute dataMemberAttribute;
 
951
      if (dataContractAttribute != null && memberInfo != null)
 
952
        dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo) memberInfo);
 
953
      else
 
954
        dataMemberAttribute = null;
 
955
#endif
 
956
 
 
957
      JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(attributeProvider);
 
958
      if (propertyAttribute != null)
 
959
        property.HasMemberAttribute = true;
 
960
 
 
961
      string mappedName;
 
962
      if (propertyAttribute != null && propertyAttribute.PropertyName != null)
 
963
        mappedName = propertyAttribute.PropertyName;
 
964
#if !PocketPC && !NET20
 
965
      else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
 
966
        mappedName = dataMemberAttribute.Name;
 
967
#endif
 
968
      else
 
969
        mappedName = name;
 
970
 
 
971
      property.PropertyName = ResolvePropertyName(mappedName);
 
972
      property.UnderlyingName = name;
 
973
 
 
974
      bool hasMemberAttribute = false;
 
975
      if (propertyAttribute != null)
 
976
      {
 
977
        property._required = propertyAttribute._required;
 
978
        property.Order = propertyAttribute._order;
 
979
        property.DefaultValueHandling = propertyAttribute._defaultValueHandling;
 
980
        hasMemberAttribute = true;
 
981
      }
 
982
#if !PocketPC && !NET20
 
983
      else if (dataMemberAttribute != null)
 
984
      {
 
985
        property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
 
986
        property.Order = (dataMemberAttribute.Order != -1) ? (int?) dataMemberAttribute.Order : null;
 
987
        property.DefaultValueHandling = (!dataMemberAttribute.EmitDefaultValue) ? (DefaultValueHandling?) DefaultValueHandling.Ignore : null;
 
988
        hasMemberAttribute = true;
 
989
      }
 
990
#endif
 
991
 
 
992
      bool hasJsonIgnoreAttribute =
 
993
        JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(attributeProvider) != null
 
994
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
995
        || JsonTypeReflector.GetAttribute<NonSerializedAttribute>(attributeProvider) != null
 
996
#endif
 
997
        ;
 
998
 
 
999
      if (memberSerialization != MemberSerialization.OptIn)
 
1000
      {
 
1001
       bool hasIgnoreDataMemberAttribute = false;
 
1002
        
 
1003
#if !(NET20 || NET35)
 
1004
        hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute<IgnoreDataMemberAttribute>(attributeProvider) != null);
 
1005
#endif
 
1006
 
 
1007
        // ignored if it has JsonIgnore or NonSerialized or IgnoreDataMember attributes
 
1008
        property.Ignored = (hasJsonIgnoreAttribute || hasIgnoreDataMemberAttribute);
 
1009
      }
 
1010
      else
 
1011
      {
 
1012
        // ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes
 
1013
        property.Ignored = (hasJsonIgnoreAttribute || !hasMemberAttribute);
 
1014
      }
 
1015
 
 
1016
      // resolve converter for property
 
1017
      // the class type might have a converter but the property converter takes presidence
 
1018
      property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
 
1019
      property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
 
1020
 
 
1021
      DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(attributeProvider);
 
1022
      if (defaultValueAttribute != null)
 
1023
        property.DefaultValue = defaultValueAttribute.Value;
 
1024
 
 
1025
      property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
 
1026
      property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
 
1027
      property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
 
1028
      property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
 
1029
      property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null;
 
1030
 
 
1031
      property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
 
1032
      property.ItemConverter =
 
1033
        (propertyAttribute != null && propertyAttribute.ItemConverterType != null)
 
1034
          ? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType)
 
1035
          : null;
 
1036
      property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
 
1037
      property.ItemTypeNameHandling = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null;
 
1038
 
 
1039
      allowNonPublicAccess = false;
 
1040
      if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
 
1041
        allowNonPublicAccess = true;
 
1042
      if (propertyAttribute != null)
 
1043
        allowNonPublicAccess = true;
 
1044
      if (memberSerialization == MemberSerialization.Fields)
 
1045
        allowNonPublicAccess = true;
 
1046
 
 
1047
#if !PocketPC && !NET20
 
1048
      if (dataMemberAttribute != null)
 
1049
      {
 
1050
        allowNonPublicAccess = true;
 
1051
        property.HasMemberAttribute = true;
 
1052
      }
 
1053
#endif
 
1054
    }
 
1055
 
 
1056
    private Predicate<object> CreateShouldSerializeTest(MemberInfo member)
 
1057
    {
 
1058
      MethodInfo shouldSerializeMethod = member.DeclaringType.GetMethod(JsonTypeReflector.ShouldSerializePrefix + member.Name, ReflectionUtils.EmptyTypes);
 
1059
 
 
1060
      if (shouldSerializeMethod == null || shouldSerializeMethod.ReturnType != typeof(bool))
 
1061
        return null;
 
1062
 
 
1063
      MethodCall<object, object> shouldSerializeCall =
 
1064
        JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(shouldSerializeMethod);
 
1065
 
 
1066
      return o => (bool)shouldSerializeCall(o);
 
1067
    }
 
1068
 
 
1069
    private void SetIsSpecifiedActions(JsonProperty property, MemberInfo member, bool allowNonPublicAccess)
 
1070
    {
 
1071
      MemberInfo specifiedMember = member.DeclaringType.GetProperty(member.Name + JsonTypeReflector.SpecifiedPostfix);
 
1072
      if (specifiedMember == null)
 
1073
        specifiedMember = member.DeclaringType.GetField(member.Name + JsonTypeReflector.SpecifiedPostfix);
 
1074
 
 
1075
      if (specifiedMember == null || ReflectionUtils.GetMemberUnderlyingType(specifiedMember) != typeof(bool))
 
1076
      {
 
1077
        return;
 
1078
      }
 
1079
 
 
1080
      Func<object, object> specifiedPropertyGet = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(specifiedMember);
 
1081
 
 
1082
      property.GetIsSpecified = o => (bool)specifiedPropertyGet(o);
 
1083
 
 
1084
      if (ReflectionUtils.CanSetMemberValue(specifiedMember, allowNonPublicAccess, false))
 
1085
        property.SetIsSpecified = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(specifiedMember);
 
1086
    }
 
1087
 
 
1088
    /// <summary>
 
1089
    /// Resolves the name of the property.
 
1090
    /// </summary>
 
1091
    /// <param name="propertyName">Name of the property.</param>
 
1092
    /// <returns>Name of the property.</returns>
 
1093
    protected internal virtual string ResolvePropertyName(string propertyName)
 
1094
    {
 
1095
      return propertyName;
 
1096
    }
 
1097
 
 
1098
    /// <summary>
 
1099
    /// Gets the resolved name of the property.
 
1100
    /// </summary>
 
1101
    /// <param name="propertyName">Name of the property.</param>
 
1102
    /// <returns>Name of the property.</returns>
 
1103
    public string GetResolvedPropertyName(string propertyName)
 
1104
    {
 
1105
      // this is a new method rather than changing the visibility of ResolvePropertyName to avoid
 
1106
      // a breaking change for anyone who has overidden the method
 
1107
      return ResolvePropertyName(propertyName);
 
1108
    }
 
1109
  }
 
1110
}
 
 
b'\\ No newline at end of file'