2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections;
28
using System.Collections.Generic;
29
using System.ComponentModel;
30
using System.Globalization;
32
using System.Reflection;
33
using System.Runtime.Serialization;
34
using System.Security.Permissions;
35
using Newtonsoft.Json.Converters;
36
using Newtonsoft.Json.Utilities;
37
using Newtonsoft.Json.Linq;
38
using System.Runtime.CompilerServices;
40
namespace Newtonsoft.Json.Serialization
42
internal struct ResolverContractKey : IEquatable<ResolverContractKey>
44
private readonly Type _resolverType;
45
private readonly Type _contractType;
47
public ResolverContractKey(Type resolverType, Type contractType)
49
_resolverType = resolverType;
50
_contractType = contractType;
53
public override int GetHashCode()
55
return _resolverType.GetHashCode() ^ _contractType.GetHashCode();
58
public override bool Equals(object obj)
60
if (!(obj is ResolverContractKey))
63
return Equals((ResolverContractKey) obj);
66
public bool Equals(ResolverContractKey other)
68
return (_resolverType == other._resolverType && _contractType == other._contractType);
73
/// Used by <see cref="JsonSerializer"/> to resolves a <see cref="JsonContract"/> for a given <see cref="Type"/>.
75
public class DefaultContractResolver : IContractResolver
77
internal static readonly IContractResolver Instance = new DefaultContractResolver(true);
78
private static readonly IList<JsonConverter> BuiltInConverters = new List<JsonConverter>
80
#if !PocketPC && !SILVERLIGHT && !NET20
81
new EntityKeyMemberConverter(),
83
new BinaryConverter(),
84
new KeyValuePairConverter(),
86
new XmlNodeConverter(),
87
new DataSetConverter(),
88
new DataTableConverter(),
90
new BsonObjectIdConverter()
93
private static Dictionary<ResolverContractKey, JsonContract> _sharedContractCache;
94
private static readonly object _typeContractCacheLock = new object();
96
private Dictionary<ResolverContractKey, JsonContract> _instanceContractCache;
97
private readonly bool _sharedCache;
100
/// Gets a value indicating whether members are being get and set using dynamic code generation.
101
/// This value is determined by the runtime permissions available.
104
/// <c>true</c> if using dynamic code generation; otherwise, <c>false</c>.
106
public bool DynamicCodeGeneration
108
get { return JsonTypeReflector.DynamicCodeGeneration; }
112
/// Gets or sets the default members search flags.
114
/// <value>The default members search flags.</value>
115
public BindingFlags DefaultMembersSearchFlags { get; set; }
118
/// Gets or sets a value indicating whether compiler generated members should be serialized.
121
/// <c>true</c> if serialized compiler generated members; otherwise, <c>false</c>.
123
public bool SerializeCompilerGeneratedMembers { get; set; }
126
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
128
public DefaultContractResolver()
134
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
136
/// <param name="shareCache">
137
/// If set to <c>true</c> the <see cref="DefaultContractResolver"/> will use a cached shared with other resolvers of the same type.
138
/// Sharing the cache will significantly performance because expensive reflection will only happen once but could cause unexpected
139
/// behavior if different instances of the resolver are suppose to produce different results. When set to false it is highly
140
/// recommended to reuse <see cref="DefaultContractResolver"/> instances with the <see cref="JsonSerializer"/>.
142
public DefaultContractResolver(bool shareCache)
144
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance;
145
_sharedCache = shareCache;
148
private Dictionary<ResolverContractKey, JsonContract> GetCache()
151
return _sharedContractCache;
153
return _instanceContractCache;
156
private void UpdateCache(Dictionary<ResolverContractKey, JsonContract> cache)
159
_sharedContractCache = cache;
161
_instanceContractCache = cache;
165
/// Resolves the contract for a given type.
167
/// <param name="type">The type to resolve a contract for.</param>
168
/// <returns>The contract for a given type.</returns>
169
public virtual JsonContract ResolveContract(Type type)
172
throw new ArgumentNullException("type");
174
JsonContract contract;
175
ResolverContractKey key = new ResolverContractKey(GetType(), type);
176
Dictionary<ResolverContractKey, JsonContract> cache = GetCache();
177
if (cache == null || !cache.TryGetValue(key, out contract))
179
contract = CreateContract(type);
181
// avoid the possibility of modifying the cache dictionary while another thread is accessing it
182
lock (_typeContractCacheLock)
185
Dictionary<ResolverContractKey, JsonContract> updatedCache =
187
? new Dictionary<ResolverContractKey, JsonContract>(cache)
188
: new Dictionary<ResolverContractKey, JsonContract>();
189
updatedCache[key] = contract;
191
UpdateCache(updatedCache);
199
/// Gets the serializable members for the type.
201
/// <param name="objectType">The type to get serializable members for.</param>
202
/// <returns>The serializable members for the type.</returns>
203
protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)
205
#if !PocketPC && !NET20
206
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
209
List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
210
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
211
List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
212
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
214
List<MemberInfo> serializableMembers = new List<MemberInfo>();
215
foreach (MemberInfo member in allMembers)
217
// exclude members that are compiler generated if set
218
if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof(CompilerGeneratedAttribute), true))
220
if (defaultMembers.Contains(member))
222
// add all members that are found by default member search
223
serializableMembers.Add(member);
227
// add members that are explicitly marked with JsonProperty/DataMember attribute
228
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member) != null)
229
serializableMembers.Add(member);
230
#if !PocketPC && !NET20
231
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member) != null)
232
serializableMembers.Add(member);
238
#if !PocketPC && !SILVERLIGHT && !NET20
240
// don't include EntityKey on entities objects... this is a bit hacky
241
if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match))
242
serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
245
return serializableMembers;
248
#if !PocketPC && !SILVERLIGHT && !NET20
249
private bool ShouldSerializeEntityMember(MemberInfo memberInfo)
251
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
252
if (propertyInfo != null)
254
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName == "System.Data.Objects.DataClasses.EntityReference`1")
263
/// Creates a <see cref="JsonObjectContract"/> for the given type.
265
/// <param name="objectType">Type of the object.</param>
266
/// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
267
protected virtual JsonObjectContract CreateObjectContract(Type objectType)
269
JsonObjectContract contract = new JsonObjectContract(objectType);
270
InitializeContract(contract);
272
contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType);
273
contract.Properties.AddRange(CreateProperties(contract));
274
if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
275
contract.ParametrizedConstructor = GetParametrizedConstructor(objectType);
280
private ConstructorInfo GetParametrizedConstructor(Type objectType)
282
ConstructorInfo[] constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
284
if (constructors.Length == 1)
285
return constructors[0];
291
/// Resolves the default <see cref="JsonConverter" /> for the contract.
293
/// <param name="objectType">Type of the object.</param>
294
/// <returns></returns>
295
protected virtual JsonConverter ResolveContractConverter(Type objectType)
297
return JsonTypeReflector.GetJsonConverter(objectType, objectType);
300
private Func<object> GetDefaultCreator(Type createdType)
302
return JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
305
private void InitializeContract(JsonContract contract)
307
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType);
308
if (containerAttribute != null)
310
contract.IsReference = containerAttribute._isReference;
312
#if !PocketPC && !NET20
315
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType);
316
// doesn't have a null value
317
if (dataContractAttribute != null && dataContractAttribute.IsReference)
318
contract.IsReference = true;
322
contract.Converter = ResolveContractConverter(contract.UnderlyingType);
324
// then see whether object is compadible with any of the built in converters
325
contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.UnderlyingType);
327
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
328
|| contract.CreatedType.IsValueType)
330
contract.DefaultCreator = GetDefaultCreator(contract.CreatedType);
332
contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType &&
333
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
336
foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
338
// compact framework errors when getting parameters for a generic method
339
// lame, but generic methods should not be callbacks anyway
340
if (method.ContainsGenericParameters)
343
Type prevAttributeType = null;
344
ParameterInfo[] parameters = method.GetParameters();
347
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType))
349
contract.OnSerializing = method;
351
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType))
353
contract.OnSerialized = method;
355
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType))
357
contract.OnDeserializing = method;
359
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType))
361
contract.OnDeserialized = method;
364
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType))
366
contract.OnError = method;
372
/// Creates a <see cref="JsonDictionaryContract"/> for the given type.
374
/// <param name="objectType">Type of the object.</param>
375
/// <returns>A <see cref="JsonDictionaryContract"/> for the given type.</returns>
376
protected virtual JsonDictionaryContract CreateDictionaryContract(Type objectType)
378
JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
379
InitializeContract(contract);
385
/// Creates a <see cref="JsonArrayContract"/> for the given type.
387
/// <param name="objectType">Type of the object.</param>
388
/// <returns>A <see cref="JsonArrayContract"/> for the given type.</returns>
389
protected virtual JsonArrayContract CreateArrayContract(Type objectType)
391
JsonArrayContract contract = new JsonArrayContract(objectType);
392
InitializeContract(contract);
398
/// Creates a <see cref="JsonPrimitiveContract"/> for the given type.
400
/// <param name="objectType">Type of the object.</param>
401
/// <returns>A <see cref="JsonPrimitiveContract"/> for the given type.</returns>
402
protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
404
JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);
405
InitializeContract(contract);
411
/// Creates a <see cref="JsonLinqContract"/> for the given type.
413
/// <param name="objectType">Type of the object.</param>
414
/// <returns>A <see cref="JsonLinqContract"/> for the given type.</returns>
415
protected virtual JsonLinqContract CreateLinqContract(Type objectType)
417
JsonLinqContract contract = new JsonLinqContract(objectType);
418
InitializeContract(contract);
423
#if !SILVERLIGHT && !PocketPC
425
/// Creates a <see cref="JsonISerializableContract"/> for the given type.
427
/// <param name="objectType">Type of the object.</param>
428
/// <returns>A <see cref="JsonISerializableContract"/> for the given type.</returns>
429
protected virtual JsonISerializableContract CreateISerializableContract(Type objectType)
431
JsonISerializableContract contract = new JsonISerializableContract(objectType);
432
InitializeContract(contract);
434
ConstructorInfo constructorInfo = objectType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new [] {typeof (SerializationInfo), typeof (StreamingContext)}, null);
435
if (constructorInfo != null)
437
MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructorInfo);
439
contract.ISerializableCreator = (args => methodCall(null, args));
447
/// Creates a <see cref="JsonStringContract"/> for the given type.
449
/// <param name="objectType">Type of the object.</param>
450
/// <returns>A <see cref="JsonStringContract"/> for the given type.</returns>
451
protected virtual JsonStringContract CreateStringContract(Type objectType)
453
JsonStringContract contract = new JsonStringContract(objectType);
454
InitializeContract(contract);
460
/// Determines which contract type is created for the given type.
462
/// <param name="objectType">Type of the object.</param>
463
/// <returns>A <see cref="JsonContract"/> for the given type.</returns>
464
protected virtual JsonContract CreateContract(Type objectType)
466
if (JsonConvert.IsJsonPrimitiveType(objectType))
467
return CreatePrimitiveContract(objectType);
469
if (JsonTypeReflector.GetJsonObjectAttribute(objectType) != null)
470
return CreateObjectContract(objectType);
472
if (JsonTypeReflector.GetJsonArrayAttribute(objectType) != null)
473
return CreateArrayContract(objectType);
475
if (objectType.IsSubclassOf(typeof(JToken)))
476
return CreateLinqContract(objectType);
478
if (CollectionUtils.IsDictionaryType(objectType))
479
return CreateDictionaryContract(objectType);
481
if (typeof(IEnumerable).IsAssignableFrom(objectType))
482
return CreateArrayContract(objectType);
484
if (CanConvertToString(objectType))
485
return CreateStringContract(objectType);
487
#if !SILVERLIGHT && !PocketPC
488
if (typeof(ISerializable).IsAssignableFrom(objectType))
489
return CreateISerializableContract(objectType);
492
return CreateObjectContract(objectType);
495
internal static bool CanConvertToString(Type type)
498
TypeConverter converter = ConvertUtils.GetConverter(type);
500
// use the objectType's TypeConverter if it has one and can convert to a string
501
if (converter != null
503
&& !(converter is ComponentConverter)
504
&& !(converter is ReferenceConverter)
506
&& converter.GetType() != typeof(TypeConverter))
508
if (converter.CanConvertTo(typeof(string)))
513
if (type == typeof(Type) || type.IsSubclassOf(typeof(Type)))
516
#if SILVERLIGHT || PocketPC
517
if (type == typeof(Guid) || type == typeof(Uri) || type == typeof(TimeSpan))
524
private static bool IsValidCallback(MethodInfo method, ParameterInfo[] parameters, Type attributeType, MethodInfo currentCallback, ref Type prevAttributeType)
526
if (!method.IsDefined(attributeType, false))
529
if (currentCallback != null)
530
throw new Exception("Invalid attribute. Both '{0}' and '{1}' in type '{2}' have '{3}'.".FormatWith(CultureInfo.InvariantCulture, method, currentCallback, GetClrTypeFullName(method.DeclaringType), attributeType));
532
if (prevAttributeType != null)
533
throw new Exception("Invalid Callback. Method '{3}' in type '{2}' has both '{0}' and '{1}'.".FormatWith(CultureInfo.InvariantCulture, prevAttributeType, attributeType, GetClrTypeFullName(method.DeclaringType), method));
535
if (method.IsVirtual)
536
throw new Exception("Virtual Method '{0}' of type '{1}' cannot be marked with '{2}' attribute.".FormatWith(CultureInfo.InvariantCulture, method, GetClrTypeFullName(method.DeclaringType), attributeType));
538
if (method.ReturnType != typeof(void))
539
throw new Exception("Serialization Callback '{1}' in type '{0}' must return void.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method));
541
if (attributeType == typeof(OnErrorAttribute))
543
if (parameters == null || parameters.Length != 2 || parameters[0].ParameterType != typeof(StreamingContext) || parameters[1].ParameterType != typeof(ErrorContext))
544
throw new Exception("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)));
548
if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
549
throw new Exception("Serialization Callback '{1}' in type '{0}' must have a single parameter of type '{2}'.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method, typeof(StreamingContext)));
552
prevAttributeType = attributeType;
557
internal static string GetClrTypeFullName(Type type)
559
if (type.IsGenericTypeDefinition || !type.ContainsGenericParameters)
560
return type.FullName;
562
return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { type.Namespace, type.Name });
566
/// Creates properties for the given <see cref="JsonObjectContract"/>.
568
/// <param name="contract">The contract to create properties for.</param>
569
/// <returns>Properties for the given <see cref="JsonObjectContract"/>.</returns>
570
protected virtual IList<JsonProperty> CreateProperties(JsonObjectContract contract)
572
List<MemberInfo> members = GetSerializableMembers(contract.UnderlyingType);
574
throw new JsonSerializationException("Null collection of seralizable members returned.");
576
JsonPropertyCollection properties = new JsonPropertyCollection(contract);
578
foreach (MemberInfo member in members)
580
JsonProperty property = CreateProperty(contract, member);
582
if (property != null)
583
properties.AddProperty(property);
590
/// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member.
592
/// <param name="member">The member.</param>
593
/// <returns>The <see cref="IValueProvider"/> used by the serializer to get and set values from a member.</returns>
594
protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
596
#if !PocketPC && !SILVERLIGHT
597
if (DynamicCodeGeneration)
598
return new DynamicValueProvider(member);
601
return new ReflectionValueProvider(member);
605
/// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
607
/// <param name="contract">The member's declaring types <see cref="JsonObjectContract"/>.</param>
608
/// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param>
609
/// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns>
610
protected virtual JsonProperty CreateProperty(JsonObjectContract contract, MemberInfo member)
612
JsonProperty property = new JsonProperty();
613
property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
614
property.ValueProvider = CreateMemberValueProvider(member);
616
// resolve converter for property
617
// the class type might have a converter but the property converter takes presidence
618
property.Converter = JsonTypeReflector.GetJsonConverter(member, property.PropertyType);
620
#if !PocketPC && !NET20
621
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(member.DeclaringType);
623
DataMemberAttribute dataMemberAttribute;
624
if (dataContractAttribute != null)
625
dataMemberAttribute = JsonTypeReflector.GetAttribute<DataMemberAttribute>(member);
627
dataMemberAttribute = null;
630
JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member);
631
bool hasIgnoreAttribute = (JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(member) != null);
634
if (propertyAttribute != null && propertyAttribute.PropertyName != null)
635
mappedName = propertyAttribute.PropertyName;
636
#if !PocketPC && !NET20
637
else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
638
mappedName = dataMemberAttribute.Name;
641
mappedName = member.Name;
643
property.PropertyName = ResolvePropertyName(mappedName);
645
if (propertyAttribute != null)
646
property.Required = propertyAttribute.Required;
647
#if !PocketPC && !NET20
648
else if (dataMemberAttribute != null)
649
property.Required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
652
property.Required = Required.Default;
654
property.Ignored = (hasIgnoreAttribute ||
655
(contract.MemberSerialization == MemberSerialization.OptIn
656
&& propertyAttribute == null
657
#if !PocketPC && !NET20
658
&& dataMemberAttribute == null
662
bool allowNonPublicAccess = false;
663
if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
664
allowNonPublicAccess = true;
665
if (propertyAttribute != null)
666
allowNonPublicAccess = true;
667
#if !PocketPC && !NET20
668
if (dataMemberAttribute != null)
669
allowNonPublicAccess = true;
672
property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
673
property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess);
675
property.MemberConverter = JsonTypeReflector.GetJsonConverter(member, ReflectionUtils.GetMemberUnderlyingType(member));
677
DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(member);
678
property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;
680
property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
681
property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
682
property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
683
property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
684
property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
685
property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null;
687
property.ShouldSerialize = CreateShouldSerializeTest(member);
692
private Predicate<object> CreateShouldSerializeTest(MemberInfo member)
694
MethodInfo shouldSerializeMethod = member.DeclaringType.GetMethod(JsonTypeReflector.ShouldSerializePrefix + member.Name, new Type[0]);
696
if (shouldSerializeMethod == null || shouldSerializeMethod.ReturnType != typeof(bool))
699
MethodCall<object, object> shouldSerializeCall =
700
JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(shouldSerializeMethod);
702
return o => (bool) shouldSerializeCall(o);
706
/// Resolves the name of the property.
708
/// <param name="propertyName">Name of the property.</param>
709
/// <returns>Name of the property.</returns>
710
protected virtual string ResolvePropertyName(string propertyName)
2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections;
28
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
29
using System.Collections.Concurrent;
31
using System.Collections.Generic;
32
using System.ComponentModel;
33
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
36
using System.Globalization;
37
using System.Reflection;
38
using System.Runtime.Serialization;
39
#if !(NETFX_CORE || PORTABLE)
40
using System.Security.Permissions;
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;
51
using Newtonsoft.Json.Utilities.LinqBridge;
56
namespace Newtonsoft.Json.Serialization
58
internal struct ResolverContractKey : IEquatable<ResolverContractKey>
60
private readonly Type _resolverType;
61
private readonly Type _contractType;
63
public ResolverContractKey(Type resolverType, Type contractType)
65
_resolverType = resolverType;
66
_contractType = contractType;
69
public override int GetHashCode()
71
return _resolverType.GetHashCode() ^ _contractType.GetHashCode();
74
public override bool Equals(object obj)
76
if (!(obj is ResolverContractKey))
79
return Equals((ResolverContractKey)obj);
82
public bool Equals(ResolverContractKey other)
84
return (_resolverType == other._resolverType && _contractType == other._contractType);
89
/// Used by <see cref="JsonSerializer"/> to resolves a <see cref="JsonContract"/> for a given <see cref="Type"/>.
91
public class DefaultContractResolver : IContractResolver
93
private static readonly IContractResolver _instance = new DefaultContractResolver(true);
94
internal static IContractResolver Instance
96
get { return _instance; }
98
private static readonly IList<JsonConverter> BuiltInConverters = new List<JsonConverter>
100
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
101
new EntityKeyMemberConverter(),
103
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
104
new ExpandoObjectConverter(),
106
#if (!(SILVERLIGHT || PORTABLE) || WINDOWS_PHONE)
107
new XmlNodeConverter(),
109
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
110
new BinaryConverter(),
111
new DataSetConverter(),
112
new DataTableConverter(),
114
new KeyValuePairConverter(),
115
new BsonObjectIdConverter()
118
private static Dictionary<ResolverContractKey, JsonContract> _sharedContractCache;
119
private static readonly object _typeContractCacheLock = new object();
121
private Dictionary<ResolverContractKey, JsonContract> _instanceContractCache;
122
private readonly bool _sharedCache;
125
/// Gets a value indicating whether members are being get and set using dynamic code generation.
126
/// This value is determined by the runtime permissions available.
129
/// <c>true</c> if using dynamic code generation; otherwise, <c>false</c>.
131
public bool DynamicCodeGeneration
133
get { return JsonTypeReflector.DynamicCodeGeneration; }
138
/// Gets or sets the default members search flags.
140
/// <value>The default members search flags.</value>
141
public BindingFlags DefaultMembersSearchFlags { get; set; }
143
private BindingFlags DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public;
147
/// Gets or sets a value indicating whether compiler generated members should be serialized.
150
/// <c>true</c> if serialized compiler generated members; otherwise, <c>false</c>.
152
public bool SerializeCompilerGeneratedMembers { get; set; }
154
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
156
/// Gets or sets a value indicating whether to ignore the <see cref="ISerializable"/> interface when serializing and deserializing types.
159
/// <c>true</c> if the <see cref="ISerializable"/> interface will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
161
public bool IgnoreSerializableInterface { get; set; }
164
/// Gets or sets a value indicating whether to ignore the <see cref="SerializableAttribute"/> attribute when serializing and deserializing types.
167
/// <c>true</c> if the <see cref="SerializableAttribute"/> attribute will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
169
public bool IgnoreSerializableAttribute { get; set; }
173
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
175
public DefaultContractResolver()
181
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
183
/// <param name="shareCache">
184
/// If set to <c>true</c> the <see cref="DefaultContractResolver"/> will use a cached shared with other resolvers of the same type.
185
/// Sharing the cache will significantly performance because expensive reflection will only happen once but could cause unexpected
186
/// behavior if different instances of the resolver are suppose to produce different results. When set to false it is highly
187
/// recommended to reuse <see cref="DefaultContractResolver"/> instances with the <see cref="JsonSerializer"/>.
189
public DefaultContractResolver(bool shareCache)
192
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance;
194
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
195
IgnoreSerializableAttribute = true;
198
_sharedCache = shareCache;
201
private Dictionary<ResolverContractKey, JsonContract> GetCache()
204
return _sharedContractCache;
206
return _instanceContractCache;
209
private void UpdateCache(Dictionary<ResolverContractKey, JsonContract> cache)
212
_sharedContractCache = cache;
214
_instanceContractCache = cache;
218
/// Resolves the contract for a given type.
220
/// <param name="type">The type to resolve a contract for.</param>
221
/// <returns>The contract for a given type.</returns>
222
public virtual JsonContract ResolveContract(Type type)
225
throw new ArgumentNullException("type");
227
JsonContract contract;
228
ResolverContractKey key = new ResolverContractKey(GetType(), type);
229
Dictionary<ResolverContractKey, JsonContract> cache = GetCache();
230
if (cache == null || !cache.TryGetValue(key, out contract))
232
contract = CreateContract(type);
234
// avoid the possibility of modifying the cache dictionary while another thread is accessing it
235
lock (_typeContractCacheLock)
238
Dictionary<ResolverContractKey, JsonContract> updatedCache =
240
? new Dictionary<ResolverContractKey, JsonContract>(cache)
241
: new Dictionary<ResolverContractKey, JsonContract>();
242
updatedCache[key] = contract;
244
UpdateCache(updatedCache);
252
/// Gets the serializable members for the type.
254
/// <param name="objectType">The type to get serializable members for.</param>
255
/// <returns>The serializable members for the type.</returns>
256
protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)
258
bool ignoreSerializableAttribute;
259
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
260
ignoreSerializableAttribute = IgnoreSerializableAttribute;
262
ignoreSerializableAttribute = true;
265
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
267
List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
268
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
270
List<MemberInfo> serializableMembers = new List<MemberInfo>();
272
if (memberSerialization != MemberSerialization.Fields)
274
#if !PocketPC && !NET20
275
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
278
List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
279
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
281
foreach (MemberInfo member in allMembers)
283
// exclude members that are compiler generated if set
284
if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof (CompilerGeneratedAttribute), true))
286
if (defaultMembers.Contains(member))
288
// add all members that are found by default member search
289
serializableMembers.Add(member);
293
// add members that are explicitly marked with JsonProperty/DataMember attribute
294
// or are a field if serializing just fields
295
if (JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(member.GetCustomAttributeProvider()) != null)
296
serializableMembers.Add(member);
297
#if !PocketPC && !NET20
298
else if (dataContractAttribute != null && JsonTypeReflector.GetAttribute<DataMemberAttribute>(member.GetCustomAttributeProvider()) != null)
299
serializableMembers.Add(member);
301
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
302
serializableMembers.Add(member);
307
#if !PocketPC && !SILVERLIGHT && !NET20
309
// don't include EntityKey on entities objects... this is a bit hacky
310
if (objectType.AssignableToTypeName("System.Data.Objects.DataClasses.EntityObject", out match))
311
serializableMembers = serializableMembers.Where(ShouldSerializeEntityMember).ToList();
316
// serialize all fields
317
foreach (MemberInfo member in allMembers)
319
if (member.MemberType() == MemberTypes.Field)
320
serializableMembers.Add(member);
324
return serializableMembers;
327
#if !PocketPC && !SILVERLIGHT && !NET20
328
private bool ShouldSerializeEntityMember(MemberInfo memberInfo)
330
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
331
if (propertyInfo != null)
333
if (propertyInfo.PropertyType.IsGenericType() && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName == "System.Data.Objects.DataClasses.EntityReference`1")
342
/// Creates a <see cref="JsonObjectContract"/> for the given type.
344
/// <param name="objectType">Type of the object.</param>
345
/// <returns>A <see cref="JsonObjectContract"/> for the given type.</returns>
346
protected virtual JsonObjectContract CreateObjectContract(Type objectType)
348
JsonObjectContract contract = new JsonObjectContract(objectType);
349
InitializeContract(contract);
351
bool ignoreSerializableAttribute;
352
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
353
ignoreSerializableAttribute = IgnoreSerializableAttribute;
355
ignoreSerializableAttribute = true;
358
contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(contract.NonNullableUnderlyingType, ignoreSerializableAttribute);
359
contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, contract.MemberSerialization));
361
JsonObjectAttribute attribute = JsonTypeReflector.GetJsonObjectAttribute(contract.NonNullableUnderlyingType);
362
if (attribute != null)
363
contract.ItemRequired = attribute._itemRequired;
365
// check if a JsonConstructorAttribute has been defined and use that
366
if (contract.NonNullableUnderlyingType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(c => c.IsDefined(typeof(JsonConstructorAttribute), true)))
368
ConstructorInfo constructor = GetAttributeConstructor(contract.NonNullableUnderlyingType);
369
if (constructor != null)
371
contract.OverrideConstructor = constructor;
372
contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
375
else if (contract.MemberSerialization == MemberSerialization.Fields)
377
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
378
// mimic DataContractSerializer behaviour when populating fields by overriding default creator to create an uninitialized object
379
// 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
380
if (JsonTypeReflector.FullyTrusted)
381
contract.DefaultCreator = contract.GetUninitializedObject;
384
else if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
386
ConstructorInfo constructor = GetParametrizedConstructor(contract.NonNullableUnderlyingType);
387
if (constructor != null)
389
contract.ParametrizedConstructor = constructor;
390
contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
396
private ConstructorInfo GetAttributeConstructor(Type objectType)
398
IList<ConstructorInfo> markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList();
400
if (markedConstructors.Count > 1)
401
throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
402
else if (markedConstructors.Count == 1)
403
return markedConstructors[0];
408
private ConstructorInfo GetParametrizedConstructor(Type objectType)
410
IList<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList();
412
if (constructors.Count == 1)
413
return constructors[0];
419
/// Creates the constructor parameters.
421
/// <param name="constructor">The constructor to create properties for.</param>
422
/// <param name="memberProperties">The type's member properties.</param>
423
/// <returns>Properties for the given <see cref="ConstructorInfo"/>.</returns>
424
protected virtual IList<JsonProperty> CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
426
var constructorParameters = constructor.GetParameters();
428
JsonPropertyCollection parameterCollection = new JsonPropertyCollection(constructor.DeclaringType);
430
foreach (ParameterInfo parameterInfo in constructorParameters)
432
JsonProperty matchingMemberProperty = memberProperties.GetClosestMatchProperty(parameterInfo.Name);
433
// type must match as well as name
434
if (matchingMemberProperty != null && matchingMemberProperty.PropertyType != parameterInfo.ParameterType)
435
matchingMemberProperty = null;
437
JsonProperty property = CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
439
if (property != null)
441
parameterCollection.AddProperty(property);
445
return parameterCollection;
449
/// Creates a <see cref="JsonProperty"/> for the given <see cref="ParameterInfo"/>.
451
/// <param name="matchingMemberProperty">The matching member property.</param>
452
/// <param name="parameterInfo">The constructor parameter.</param>
453
/// <returns>A created <see cref="JsonProperty"/> for the given <see cref="ParameterInfo"/>.</returns>
454
protected virtual JsonProperty CreatePropertyFromConstructorParameter(JsonProperty matchingMemberProperty, ParameterInfo parameterInfo)
456
JsonProperty property = new JsonProperty();
457
property.PropertyType = parameterInfo.ParameterType;
459
bool allowNonPublicAccess;
460
SetPropertySettingsFromAttributes(property, parameterInfo.GetCustomAttributeProvider(), parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
462
property.Readable = false;
463
property.Writable = true;
465
// "inherit" values from matching member property if unset on parameter
466
if (matchingMemberProperty != null)
468
property.PropertyName = (property.PropertyName != parameterInfo.Name) ? property.PropertyName : matchingMemberProperty.PropertyName;
469
property.Converter = property.Converter ?? matchingMemberProperty.Converter;
470
property.MemberConverter = property.MemberConverter ?? matchingMemberProperty.MemberConverter;
471
property.DefaultValue = property.DefaultValue ?? matchingMemberProperty.DefaultValue;
472
property._required = property._required ?? matchingMemberProperty._required;
473
property.IsReference = property.IsReference ?? matchingMemberProperty.IsReference;
474
property.NullValueHandling = property.NullValueHandling ?? matchingMemberProperty.NullValueHandling;
475
property.DefaultValueHandling = property.DefaultValueHandling ?? matchingMemberProperty.DefaultValueHandling;
476
property.ReferenceLoopHandling = property.ReferenceLoopHandling ?? matchingMemberProperty.ReferenceLoopHandling;
477
property.ObjectCreationHandling = property.ObjectCreationHandling ?? matchingMemberProperty.ObjectCreationHandling;
478
property.TypeNameHandling = property.TypeNameHandling ?? matchingMemberProperty.TypeNameHandling;
485
/// Resolves the default <see cref="JsonConverter" /> for the contract.
487
/// <param name="objectType">Type of the object.</param>
488
/// <returns>The contract's default <see cref="JsonConverter" />.</returns>
489
protected virtual JsonConverter ResolveContractConverter(Type objectType)
491
return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
494
private Func<object> GetDefaultCreator(Type createdType)
496
return JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
499
#if !PocketPC && !NET20
500
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1903:UseOnlyApiFromTargetedFramework", MessageId = "System.Runtime.Serialization.DataContractAttribute.#get_IsReference()")]
502
private void InitializeContract(JsonContract contract)
504
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.NonNullableUnderlyingType);
505
if (containerAttribute != null)
507
contract.IsReference = containerAttribute._isReference;
509
#if !PocketPC && !NET20
512
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.NonNullableUnderlyingType);
513
// doesn't have a null value
514
if (dataContractAttribute != null && dataContractAttribute.IsReference)
515
contract.IsReference = true;
519
contract.Converter = ResolveContractConverter(contract.NonNullableUnderlyingType);
521
// then see whether object is compadible with any of the built in converters
522
contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.NonNullableUnderlyingType);
524
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
525
|| contract.CreatedType.IsValueType())
527
contract.DefaultCreator = GetDefaultCreator(contract.CreatedType);
529
contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType() &&
530
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
533
ResolveCallbackMethods(contract, contract.NonNullableUnderlyingType);
536
private void ResolveCallbackMethods(JsonContract contract, Type t)
538
if (t.BaseType() != null)
539
ResolveCallbackMethods(contract, t.BaseType());
541
MethodInfo onSerializing;
542
MethodInfo onSerialized;
543
MethodInfo onDeserializing;
544
MethodInfo onDeserialized;
547
GetCallbackMethodsForType(t, out onSerializing, out onSerialized, out onDeserializing, out onDeserialized, out onError);
549
if (onSerializing != null)
552
if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
553
contract.OnSerializing = onSerializing;
555
contract.OnSerializing = onSerializing;
559
if (onSerialized != null)
560
contract.OnSerialized = onSerialized;
562
if (onDeserializing != null)
563
contract.OnDeserializing = onDeserializing;
565
if (onDeserialized != null)
567
// ConcurrentDictionary throws an error here so don't use its OnDeserialized - http://json.codeplex.com/discussions/257093
568
#if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
569
if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
570
contract.OnDeserialized = onDeserialized;
572
contract.OnDeserialized = onDeserialized;
577
contract.OnError = onError;
580
private void GetCallbackMethodsForType(Type type, out MethodInfo onSerializing, out MethodInfo onSerialized, out MethodInfo onDeserializing, out MethodInfo onDeserialized, out MethodInfo onError)
582
onSerializing = null;
584
onDeserializing = null;
585
onDeserialized = null;
588
foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
590
// compact framework errors when getting parameters for a generic method
591
// lame, but generic methods should not be callbacks anyway
592
if (method.ContainsGenericParameters)
595
Type prevAttributeType = null;
596
ParameterInfo[] parameters = method.GetParameters();
598
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), onSerializing, ref prevAttributeType))
600
onSerializing = method;
602
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), onSerialized, ref prevAttributeType))
604
onSerialized = method;
606
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), onDeserializing, ref prevAttributeType))
608
onDeserializing = method;
610
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), onDeserialized, ref prevAttributeType))
612
onDeserialized = method;
614
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), onError, ref prevAttributeType))
622
/// Creates a <see cref="JsonDictionaryContract"/> for the given type.
624
/// <param name="objectType">Type of the object.</param>
625
/// <returns>A <see cref="JsonDictionaryContract"/> for the given type.</returns>
626
protected virtual JsonDictionaryContract CreateDictionaryContract(Type objectType)
628
JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
629
InitializeContract(contract);
631
contract.PropertyNameResolver = ResolvePropertyName;
637
/// Creates a <see cref="JsonArrayContract"/> for the given type.
639
/// <param name="objectType">Type of the object.</param>
640
/// <returns>A <see cref="JsonArrayContract"/> for the given type.</returns>
641
protected virtual JsonArrayContract CreateArrayContract(Type objectType)
643
JsonArrayContract contract = new JsonArrayContract(objectType);
644
InitializeContract(contract);
650
/// Creates a <see cref="JsonPrimitiveContract"/> for the given type.
652
/// <param name="objectType">Type of the object.</param>
653
/// <returns>A <see cref="JsonPrimitiveContract"/> for the given type.</returns>
654
protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
656
JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);
657
InitializeContract(contract);
663
/// Creates a <see cref="JsonLinqContract"/> for the given type.
665
/// <param name="objectType">Type of the object.</param>
666
/// <returns>A <see cref="JsonLinqContract"/> for the given type.</returns>
667
protected virtual JsonLinqContract CreateLinqContract(Type objectType)
669
JsonLinqContract contract = new JsonLinqContract(objectType);
670
InitializeContract(contract);
675
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
677
/// Creates a <see cref="JsonISerializableContract"/> for the given type.
679
/// <param name="objectType">Type of the object.</param>
680
/// <returns>A <see cref="JsonISerializableContract"/> for the given type.</returns>
681
protected virtual JsonISerializableContract CreateISerializableContract(Type objectType)
683
JsonISerializableContract contract = new JsonISerializableContract(objectType);
684
InitializeContract(contract);
686
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
687
if (constructorInfo != null)
689
MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructorInfo);
691
contract.ISerializableCreator = (args => methodCall(null, args));
698
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
700
/// Creates a <see cref="JsonDynamicContract"/> for the given type.
702
/// <param name="objectType">Type of the object.</param>
703
/// <returns>A <see cref="JsonDynamicContract"/> for the given type.</returns>
704
protected virtual JsonDynamicContract CreateDynamicContract(Type objectType)
706
JsonDynamicContract contract = new JsonDynamicContract(objectType);
707
InitializeContract(contract);
709
contract.PropertyNameResolver = ResolvePropertyName;
710
contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut));
717
/// Creates a <see cref="JsonStringContract"/> for the given type.
719
/// <param name="objectType">Type of the object.</param>
720
/// <returns>A <see cref="JsonStringContract"/> for the given type.</returns>
721
protected virtual JsonStringContract CreateStringContract(Type objectType)
723
JsonStringContract contract = new JsonStringContract(objectType);
724
InitializeContract(contract);
730
/// Determines which contract type is created for the given type.
732
/// <param name="objectType">Type of the object.</param>
733
/// <returns>A <see cref="JsonContract"/> for the given type.</returns>
734
protected virtual JsonContract CreateContract(Type objectType)
736
Type t = ReflectionUtils.EnsureNotNullableType(objectType);
738
if (JsonConvert.IsJsonPrimitiveType(t))
739
return CreatePrimitiveContract(objectType);
741
if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
742
return CreateObjectContract(objectType);
744
if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
745
return CreateArrayContract(objectType);
747
if (JsonTypeReflector.GetJsonDictionaryAttribute(t) != null)
748
return CreateDictionaryContract(objectType);
750
if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
751
return CreateLinqContract(objectType);
753
if (CollectionUtils.IsDictionaryType(t))
754
return CreateDictionaryContract(objectType);
756
if (typeof(IEnumerable).IsAssignableFrom(t))
757
return CreateArrayContract(objectType);
759
if (CanConvertToString(t))
760
return CreateStringContract(objectType);
762
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
763
if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t))
764
return CreateISerializableContract(objectType);
767
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
768
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t))
769
return CreateDynamicContract(objectType);
772
return CreateObjectContract(objectType);
775
internal static bool CanConvertToString(Type type)
777
#if !(NETFX_CORE || PORTABLE)
778
TypeConverter converter = ConvertUtils.GetConverter(type);
780
// use the objectType's TypeConverter if it has one and can convert to a string
781
if (converter != null
783
&& !(converter is ComponentConverter)
784
&& !(converter is ReferenceConverter)
786
&& converter.GetType() != typeof(TypeConverter))
788
if (converter.CanConvertTo(typeof(string)))
793
if (type == typeof(Type) || type.IsSubclassOf(typeof(Type)))
796
#if SILVERLIGHT || PocketPC
797
if (type == typeof(Guid) || type == typeof(Uri) || type == typeof(TimeSpan))
804
private static bool IsValidCallback(MethodInfo method, ParameterInfo[] parameters, Type attributeType, MethodInfo currentCallback, ref Type prevAttributeType)
806
if (!method.IsDefined(attributeType, false))
809
if (currentCallback != null)
810
throw new JsonException("Invalid attribute. Both '{0}' and '{1}' in type '{2}' have '{3}'.".FormatWith(CultureInfo.InvariantCulture, method, currentCallback, GetClrTypeFullName(method.DeclaringType), attributeType));
812
if (prevAttributeType != null)
813
throw new JsonException("Invalid Callback. Method '{3}' in type '{2}' has both '{0}' and '{1}'.".FormatWith(CultureInfo.InvariantCulture, prevAttributeType, attributeType, GetClrTypeFullName(method.DeclaringType), method));
815
if (method.IsVirtual)
816
throw new JsonException("Virtual Method '{0}' of type '{1}' cannot be marked with '{2}' attribute.".FormatWith(CultureInfo.InvariantCulture, method, GetClrTypeFullName(method.DeclaringType), attributeType));
818
if (method.ReturnType != typeof(void))
819
throw new JsonException("Serialization Callback '{1}' in type '{0}' must return void.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method));
821
if (attributeType == typeof(OnErrorAttribute))
823
if (parameters == null || parameters.Length != 2 || parameters[0].ParameterType != typeof(StreamingContext) || parameters[1].ParameterType != typeof(ErrorContext))
824
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)));
828
if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
829
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)));
832
prevAttributeType = attributeType;
837
internal static string GetClrTypeFullName(Type type)
839
if (type.IsGenericTypeDefinition() || !type.ContainsGenericParameters())
840
return type.FullName;
842
return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { type.Namespace, type.Name });
846
/// Creates properties for the given <see cref="JsonContract"/>.
848
/// <param name="type">The type to create properties for.</param>
849
/// /// <param name="memberSerialization">The member serialization mode for the type.</param>
850
/// <returns>Properties for the given <see cref="JsonContract"/>.</returns>
851
protected virtual IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
853
List<MemberInfo> members = GetSerializableMembers(type);
855
throw new JsonSerializationException("Null collection of seralizable members returned.");
857
JsonPropertyCollection properties = new JsonPropertyCollection(type);
859
foreach (MemberInfo member in members)
861
JsonProperty property = CreateProperty(member, memberSerialization);
863
if (property != null)
864
properties.AddProperty(property);
867
IList<JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();
868
return orderedProperties;
872
/// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member.
874
/// <param name="member">The member.</param>
875
/// <returns>The <see cref="IValueProvider"/> used by the serializer to get and set values from a member.</returns>
876
protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
878
// warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes
879
IValueProvider valueProvider;
881
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
882
if (DynamicCodeGeneration)
883
valueProvider = new DynamicValueProvider(member);
885
valueProvider = new ReflectionValueProvider(member);
887
valueProvider = new ReflectionValueProvider(member);
890
return valueProvider;
894
/// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
896
/// <param name="memberSerialization">The member's parent <see cref="MemberSerialization"/>.</param>
897
/// <param name="member">The member to create a <see cref="JsonProperty"/> for.</param>
898
/// <returns>A created <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.</returns>
899
protected virtual JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
901
JsonProperty property = new JsonProperty();
902
property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
903
property.DeclaringType = member.DeclaringType;
904
property.ValueProvider = CreateMemberValueProvider(member);
906
bool allowNonPublicAccess;
907
SetPropertySettingsFromAttributes(property, member.GetCustomAttributeProvider(), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
909
if (memberSerialization != MemberSerialization.Fields)
911
property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
912
property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, property.HasMemberAttribute);
916
// write to readonly fields
917
property.Readable = true;
918
property.Writable = true;
920
property.ShouldSerialize = CreateShouldSerializeTest(member);
922
SetIsSpecifiedActions(property, member, allowNonPublicAccess);
927
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
929
#if !PocketPC && !NET20
930
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
932
MemberInfo memberInfo = null;
933
#if !(NETFX_CORE || PORTABLE)
934
memberInfo = attributeProvider as MemberInfo;
936
memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
939
DataMemberAttribute dataMemberAttribute;
940
if (dataContractAttribute != null && memberInfo != null)
941
dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo) memberInfo);
943
dataMemberAttribute = null;
946
JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(attributeProvider);
947
if (propertyAttribute != null)
948
property.HasMemberAttribute = true;
951
if (propertyAttribute != null && propertyAttribute.PropertyName != null)
952
mappedName = propertyAttribute.PropertyName;
953
#if !PocketPC && !NET20
954
else if (dataMemberAttribute != null && dataMemberAttribute.Name != null)
955
mappedName = dataMemberAttribute.Name;
960
property.PropertyName = ResolvePropertyName(mappedName);
961
property.UnderlyingName = name;
963
bool hasMemberAttribute = false;
964
if (propertyAttribute != null)
966
property._required = propertyAttribute._required;
967
property.Order = propertyAttribute._order;
968
hasMemberAttribute = true;
970
#if !PocketPC && !NET20
971
else if (dataMemberAttribute != null)
973
property._required = (dataMemberAttribute.IsRequired) ? Required.AllowNull : Required.Default;
974
property.Order = (dataMemberAttribute.Order != -1) ? (int?) dataMemberAttribute.Order : null;
975
hasMemberAttribute = true;
979
bool hasJsonIgnoreAttribute =
980
JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(attributeProvider) != null
981
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
982
|| JsonTypeReflector.GetAttribute<NonSerializedAttribute>(attributeProvider) != null
986
if (memberSerialization != MemberSerialization.OptIn)
988
bool hasIgnoreDataMemberAttribute = false;
990
#if !(NET20 || NET35)
991
hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute<IgnoreDataMemberAttribute>(attributeProvider) != null);
994
// ignored if it has JsonIgnore or NonSerialized or IgnoreDataMember attributes
995
property.Ignored = (hasJsonIgnoreAttribute || hasIgnoreDataMemberAttribute);
999
// ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes
1000
property.Ignored = (hasJsonIgnoreAttribute || !hasMemberAttribute);
1003
// resolve converter for property
1004
// the class type might have a converter but the property converter takes presidence
1005
property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
1006
property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider, property.PropertyType);
1008
DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(attributeProvider);
1009
property.DefaultValue = (defaultValueAttribute != null) ? defaultValueAttribute.Value : null;
1011
property.NullValueHandling = (propertyAttribute != null) ? propertyAttribute._nullValueHandling : null;
1012
property.DefaultValueHandling = (propertyAttribute != null) ? propertyAttribute._defaultValueHandling : null;
1013
property.ReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._referenceLoopHandling : null;
1014
property.ObjectCreationHandling = (propertyAttribute != null) ? propertyAttribute._objectCreationHandling : null;
1015
property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
1016
property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null;
1018
property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
1019
property.ItemConverter =
1020
(propertyAttribute != null && propertyAttribute.ItemConverterType != null)
1021
? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType)
1023
property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
1024
property.ItemTypeNameHandling = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null;
1026
allowNonPublicAccess = false;
1027
if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
1028
allowNonPublicAccess = true;
1029
if (propertyAttribute != null)
1030
allowNonPublicAccess = true;
1031
if (memberSerialization == MemberSerialization.Fields)
1032
allowNonPublicAccess = true;
1034
#if !PocketPC && !NET20
1035
if (dataMemberAttribute != null)
1037
allowNonPublicAccess = true;
1038
property.HasMemberAttribute = true;
1043
private Predicate<object> CreateShouldSerializeTest(MemberInfo member)
1045
MethodInfo shouldSerializeMethod = member.DeclaringType.GetMethod(JsonTypeReflector.ShouldSerializePrefix + member.Name, ReflectionUtils.EmptyTypes);
1047
if (shouldSerializeMethod == null || shouldSerializeMethod.ReturnType != typeof(bool))
1050
MethodCall<object, object> shouldSerializeCall =
1051
JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(shouldSerializeMethod);
1053
return o => (bool)shouldSerializeCall(o);
1056
private void SetIsSpecifiedActions(JsonProperty property, MemberInfo member, bool allowNonPublicAccess)
1058
MemberInfo specifiedMember = member.DeclaringType.GetProperty(member.Name + JsonTypeReflector.SpecifiedPostfix);
1059
if (specifiedMember == null)
1060
specifiedMember = member.DeclaringType.GetField(member.Name + JsonTypeReflector.SpecifiedPostfix);
1062
if (specifiedMember == null || ReflectionUtils.GetMemberUnderlyingType(specifiedMember) != typeof(bool))
1067
Func<object, object> specifiedPropertyGet = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(specifiedMember);
1069
property.GetIsSpecified = o => (bool)specifiedPropertyGet(o);
1071
if (ReflectionUtils.CanSetMemberValue(specifiedMember, allowNonPublicAccess, false))
1072
property.SetIsSpecified = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(specifiedMember);
1076
/// Resolves the name of the property.
1078
/// <param name="propertyName">Name of the property.</param>
1079
/// <returns>Name of the property.</returns>
1080
protected internal virtual string ResolvePropertyName(string propertyName)
1082
return propertyName;
1086
/// Gets the resolved name of the property.
1088
/// <param name="propertyName">Name of the property.</param>
1089
/// <returns>Name of the property.</returns>
1090
public string GetResolvedPropertyName(string propertyName)
1092
// this is a new method rather than changing the visibility of ResolvePropertyName to avoid
1093
// a breaking change for anyone who has overidden the method
1094
return ResolvePropertyName(propertyName);
b'\\ No newline at end of file'