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(),
115
new JsonValueConverter(),
117
new KeyValuePairConverter(),
118
new BsonObjectIdConverter()
121
private static Dictionary<ResolverContractKey, JsonContract> _sharedContractCache;
122
private static readonly object _typeContractCacheLock = new object();
124
private Dictionary<ResolverContractKey, JsonContract> _instanceContractCache;
125
private readonly bool _sharedCache;
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.
132
/// <c>true</c> if using dynamic code generation; otherwise, <c>false</c>.
134
public bool DynamicCodeGeneration
136
get { return JsonTypeReflector.DynamicCodeGeneration; }
141
/// Gets or sets the default members search flags.
143
/// <value>The default members search flags.</value>
144
public BindingFlags DefaultMembersSearchFlags { get; set; }
146
private BindingFlags DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public;
150
/// Gets or sets a value indicating whether compiler generated members should be serialized.
153
/// <c>true</c> if serialized compiler generated members; otherwise, <c>false</c>.
155
public bool SerializeCompilerGeneratedMembers { get; set; }
157
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
159
/// Gets or sets a value indicating whether to ignore the <see cref="ISerializable"/> interface when serializing and deserializing types.
162
/// <c>true</c> if the <see cref="ISerializable"/> interface will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
164
public bool IgnoreSerializableInterface { get; set; }
167
/// Gets or sets a value indicating whether to ignore the <see cref="SerializableAttribute"/> attribute when serializing and deserializing types.
170
/// <c>true</c> if the <see cref="SerializableAttribute"/> attribute will be ignored when serializing and deserializing types; otherwise, <c>false</c>.
172
public bool IgnoreSerializableAttribute { get; set; }
176
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
178
public DefaultContractResolver()
184
/// Initializes a new instance of the <see cref="DefaultContractResolver"/> class.
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"/>.
192
public DefaultContractResolver(bool shareCache)
195
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance;
197
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
198
IgnoreSerializableAttribute = true;
201
_sharedCache = shareCache;
204
private Dictionary<ResolverContractKey, JsonContract> GetCache()
207
return _sharedContractCache;
209
return _instanceContractCache;
212
private void UpdateCache(Dictionary<ResolverContractKey, JsonContract> cache)
215
_sharedContractCache = cache;
217
_instanceContractCache = cache;
221
/// Resolves the contract for a given type.
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)
228
throw new ArgumentNullException("type");
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))
235
contract = CreateContract(type);
237
// avoid the possibility of modifying the cache dictionary while another thread is accessing it
238
lock (_typeContractCacheLock)
241
Dictionary<ResolverContractKey, JsonContract> updatedCache =
243
? new Dictionary<ResolverContractKey, JsonContract>(cache)
244
: new Dictionary<ResolverContractKey, JsonContract>();
245
updatedCache[key] = contract;
247
UpdateCache(updatedCache);
255
/// Gets the serializable members for the type.
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)
261
bool ignoreSerializableAttribute;
262
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
263
ignoreSerializableAttribute = IgnoreSerializableAttribute;
265
ignoreSerializableAttribute = true;
268
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
270
List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
271
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
273
List<MemberInfo> serializableMembers = new List<MemberInfo>();
275
if (memberSerialization != MemberSerialization.Fields)
277
#if !PocketPC && !NET20
278
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(objectType);
281
List<MemberInfo> defaultMembers = ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
282
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
284
foreach (MemberInfo member in allMembers)
286
// exclude members that are compiler generated if set
287
if (SerializeCompilerGeneratedMembers || !member.IsDefined(typeof (CompilerGeneratedAttribute), true))
289
if (defaultMembers.Contains(member))
291
// add all members that are found by default member search
292
serializableMembers.Add(member);
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);
304
else if (memberSerialization == MemberSerialization.Fields && member.MemberType() == MemberTypes.Field)
305
serializableMembers.Add(member);
310
#if !PocketPC && !SILVERLIGHT && !NET20
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();
319
// serialize all fields
320
foreach (MemberInfo member in allMembers)
322
if (member.MemberType() == MemberTypes.Field)
323
serializableMembers.Add(member);
327
return serializableMembers;
330
#if !PocketPC && !SILVERLIGHT && !NET20
331
private bool ShouldSerializeEntityMember(MemberInfo memberInfo)
333
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
334
if (propertyInfo != null)
336
if (propertyInfo.PropertyType.IsGenericType() && propertyInfo.PropertyType.GetGenericTypeDefinition().FullName == "System.Data.Objects.DataClasses.EntityReference`1")
345
/// Creates a <see cref="JsonObjectContract"/> for the given type.
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)
351
JsonObjectContract contract = new JsonObjectContract(objectType);
352
InitializeContract(contract);
354
bool ignoreSerializableAttribute;
355
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
356
ignoreSerializableAttribute = IgnoreSerializableAttribute;
358
ignoreSerializableAttribute = true;
361
contract.MemberSerialization = JsonTypeReflector.GetObjectMemberSerialization(contract.NonNullableUnderlyingType, ignoreSerializableAttribute);
362
contract.Properties.AddRange(CreateProperties(contract.NonNullableUnderlyingType, contract.MemberSerialization));
364
JsonObjectAttribute attribute = JsonTypeReflector.GetJsonObjectAttribute(contract.NonNullableUnderlyingType);
365
if (attribute != null)
366
contract.ItemRequired = attribute._itemRequired;
368
ConstructorInfo overrideConstructor = GetAttributeConstructor(contract.NonNullableUnderlyingType);
370
// check if a JsonConstructorAttribute has been defined and use that
371
if (overrideConstructor != null)
373
contract.OverrideConstructor = overrideConstructor;
374
contract.ConstructorParameters.AddRange(CreateConstructorParameters(overrideConstructor, contract.Properties));
376
else if (contract.MemberSerialization == MemberSerialization.Fields)
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;
385
else if (contract.DefaultCreator == null || contract.DefaultCreatorNonPublic)
387
ConstructorInfo constructor = GetParametrizedConstructor(contract.NonNullableUnderlyingType);
388
if (constructor != null)
390
contract.ParametrizedConstructor = constructor;
391
contract.ConstructorParameters.AddRange(CreateConstructorParameters(constructor, contract.Properties));
397
private ConstructorInfo GetAttributeConstructor(Type objectType)
399
IList<ConstructorInfo> markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList();
401
if (markedConstructors.Count > 1)
402
throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
403
else if (markedConstructors.Count == 1)
404
return markedConstructors[0];
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) });
413
private ConstructorInfo GetParametrizedConstructor(Type objectType)
415
IList<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList();
417
if (constructors.Count == 1)
418
return constructors[0];
424
/// Creates the constructor parameters.
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)
431
var constructorParameters = constructor.GetParameters();
433
JsonPropertyCollection parameterCollection = new JsonPropertyCollection(constructor.DeclaringType);
435
foreach (ParameterInfo parameterInfo in constructorParameters)
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;
441
// type must match as well as name
442
if (matchingMemberProperty != null && matchingMemberProperty.PropertyType != parameterInfo.ParameterType)
443
matchingMemberProperty = null;
445
JsonProperty property = CreatePropertyFromConstructorParameter(matchingMemberProperty, parameterInfo);
447
if (property != null)
449
parameterCollection.AddProperty(property);
453
return parameterCollection;
457
/// Creates a <see cref="JsonProperty"/> for the given <see cref="ParameterInfo"/>.
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)
464
JsonProperty property = new JsonProperty();
465
property.PropertyType = parameterInfo.ParameterType;
467
bool allowNonPublicAccess;
468
SetPropertySettingsFromAttributes(property, parameterInfo.GetCustomAttributeProvider(), parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess);
470
property.Readable = false;
471
property.Writable = true;
473
// "inherit" values from matching member property if unset on parameter
474
if (matchingMemberProperty != null)
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;
480
if (!property._hasExplicitDefaultValue && matchingMemberProperty._hasExplicitDefaultValue)
481
property.DefaultValue = matchingMemberProperty.DefaultValue;
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;
496
/// Resolves the default <see cref="JsonConverter" /> for the contract.
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)
502
return JsonTypeReflector.GetJsonConverter(objectType.GetCustomAttributeProvider(), objectType);
505
private Func<object> GetDefaultCreator(Type createdType)
507
return JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor<object>(createdType);
510
#if !PocketPC && !NET20
511
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1903:UseOnlyApiFromTargetedFramework", MessageId = "System.Runtime.Serialization.DataContractAttribute.#get_IsReference()")]
513
private void InitializeContract(JsonContract contract)
515
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.NonNullableUnderlyingType);
516
if (containerAttribute != null)
518
contract.IsReference = containerAttribute._isReference;
520
#if !PocketPC && !NET20
523
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.NonNullableUnderlyingType);
524
// doesn't have a null value
525
if (dataContractAttribute != null && dataContractAttribute.IsReference)
526
contract.IsReference = true;
530
contract.Converter = ResolveContractConverter(contract.NonNullableUnderlyingType);
532
// then see whether object is compadible with any of the built in converters
533
contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.NonNullableUnderlyingType);
535
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
536
|| contract.CreatedType.IsValueType())
538
contract.DefaultCreator = GetDefaultCreator(contract.CreatedType);
540
contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType() &&
541
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
544
ResolveCallbackMethods(contract, contract.NonNullableUnderlyingType);
547
private void ResolveCallbackMethods(JsonContract contract, Type t)
549
if (t.BaseType() != null)
550
ResolveCallbackMethods(contract, t.BaseType());
552
MethodInfo onSerializing;
553
MethodInfo onSerialized;
554
MethodInfo onDeserializing;
555
MethodInfo onDeserialized;
558
GetCallbackMethodsForType(t, out onSerializing, out onSerialized, out onDeserializing, out onDeserialized, out onError);
560
if (onSerializing != null)
563
if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
564
contract.OnSerializing = onSerializing;
566
contract.OnSerializing = onSerializing;
570
if (onSerialized != null)
571
contract.OnSerialized = onSerialized;
573
if (onDeserializing != null)
574
contract.OnDeserializing = onDeserializing;
576
if (onDeserialized != null)
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;
583
contract.OnDeserialized = onDeserialized;
588
contract.OnError = onError;
591
private void GetCallbackMethodsForType(Type type, out MethodInfo onSerializing, out MethodInfo onSerialized, out MethodInfo onDeserializing, out MethodInfo onDeserialized, out MethodInfo onError)
593
onSerializing = null;
595
onDeserializing = null;
596
onDeserialized = null;
599
foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
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)
606
Type prevAttributeType = null;
607
ParameterInfo[] parameters = method.GetParameters();
609
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), onSerializing, ref prevAttributeType))
611
onSerializing = method;
613
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), onSerialized, ref prevAttributeType))
615
onSerialized = method;
617
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), onDeserializing, ref prevAttributeType))
619
onDeserializing = method;
621
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), onDeserialized, ref prevAttributeType))
623
onDeserialized = method;
625
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), onError, ref prevAttributeType))
633
/// Creates a <see cref="JsonDictionaryContract"/> for the given type.
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)
639
JsonDictionaryContract contract = new JsonDictionaryContract(objectType);
640
InitializeContract(contract);
642
contract.PropertyNameResolver = ResolvePropertyName;
648
/// Creates a <see cref="JsonArrayContract"/> for the given type.
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)
654
JsonArrayContract contract = new JsonArrayContract(objectType);
655
InitializeContract(contract);
661
/// Creates a <see cref="JsonPrimitiveContract"/> for the given type.
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)
667
JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);
668
InitializeContract(contract);
674
/// Creates a <see cref="JsonLinqContract"/> for the given type.
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)
680
JsonLinqContract contract = new JsonLinqContract(objectType);
681
InitializeContract(contract);
686
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
688
/// Creates a <see cref="JsonISerializableContract"/> for the given type.
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)
694
JsonISerializableContract contract = new JsonISerializableContract(objectType);
695
InitializeContract(contract);
697
ConstructorInfo constructorInfo = contract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
698
if (constructorInfo != null)
700
MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(constructorInfo);
702
contract.ISerializableCreator = (args => methodCall(null, args));
709
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
711
/// Creates a <see cref="JsonDynamicContract"/> for the given type.
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)
717
JsonDynamicContract contract = new JsonDynamicContract(objectType);
718
InitializeContract(contract);
720
contract.PropertyNameResolver = ResolvePropertyName;
721
contract.Properties.AddRange(CreateProperties(objectType, MemberSerialization.OptOut));
728
/// Creates a <see cref="JsonStringContract"/> for the given type.
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)
734
JsonStringContract contract = new JsonStringContract(objectType);
735
InitializeContract(contract);
741
/// Determines which contract type is created for the given type.
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)
747
Type t = ReflectionUtils.EnsureNotNullableType(objectType);
749
if (JsonConvert.IsJsonPrimitiveType(t))
750
return CreatePrimitiveContract(objectType);
752
if (JsonTypeReflector.GetJsonObjectAttribute(t) != null)
753
return CreateObjectContract(objectType);
755
if (JsonTypeReflector.GetJsonArrayAttribute(t) != null)
756
return CreateArrayContract(objectType);
758
if (JsonTypeReflector.GetJsonDictionaryAttribute(t) != null)
759
return CreateDictionaryContract(objectType);
761
if (t == typeof(JToken) || t.IsSubclassOf(typeof(JToken)))
762
return CreateLinqContract(objectType);
764
if (CollectionUtils.IsDictionaryType(t))
765
return CreateDictionaryContract(objectType);
767
if (typeof(IEnumerable).IsAssignableFrom(t))
768
return CreateArrayContract(objectType);
770
if (CanConvertToString(t))
771
return CreateStringContract(objectType);
773
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
774
if (!IgnoreSerializableInterface && typeof(ISerializable).IsAssignableFrom(t))
775
return CreateISerializableContract(objectType);
778
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
779
if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(t))
780
return CreateDynamicContract(objectType);
783
return CreateObjectContract(objectType);
786
internal static bool CanConvertToString(Type type)
788
#if !(NETFX_CORE || PORTABLE)
789
TypeConverter converter = ConvertUtils.GetConverter(type);
791
// use the objectType's TypeConverter if it has one and can convert to a string
792
if (converter != null
794
&& !(converter is ComponentConverter)
795
&& !(converter is ReferenceConverter)
797
&& converter.GetType() != typeof(TypeConverter))
799
if (converter.CanConvertTo(typeof(string)))
804
if (type == typeof(Type) || type.IsSubclassOf(typeof(Type)))
807
#if SILVERLIGHT || PocketPC
808
if (type == typeof(Guid) || type == typeof(Uri) || type == typeof(TimeSpan))
815
private static bool IsValidCallback(MethodInfo method, ParameterInfo[] parameters, Type attributeType, MethodInfo currentCallback, ref Type prevAttributeType)
817
if (!method.IsDefined(attributeType, false))
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));
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));
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));
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));
832
if (attributeType == typeof(OnErrorAttribute))
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)));
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)));
843
prevAttributeType = attributeType;
848
internal static string GetClrTypeFullName(Type type)
850
if (type.IsGenericTypeDefinition() || !type.ContainsGenericParameters())
851
return type.FullName;
853
return string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { type.Namespace, type.Name });
857
/// Creates properties for the given <see cref="JsonContract"/>.
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)
864
List<MemberInfo> members = GetSerializableMembers(type);
866
throw new JsonSerializationException("Null collection of seralizable members returned.");
868
JsonPropertyCollection properties = new JsonPropertyCollection(type);
870
foreach (MemberInfo member in members)
872
JsonProperty property = CreateProperty(member, memberSerialization);
874
if (property != null)
875
properties.AddProperty(property);
878
IList<JsonProperty> orderedProperties = properties.OrderBy(p => p.Order ?? -1).ToList();
879
return orderedProperties;
883
/// Creates the <see cref="IValueProvider"/> used by the serializer to get and set values from a member.
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)
889
// warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes
890
IValueProvider valueProvider;
892
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
893
if (DynamicCodeGeneration)
894
valueProvider = new DynamicValueProvider(member);
896
valueProvider = new ReflectionValueProvider(member);
898
valueProvider = new ReflectionValueProvider(member);
901
return valueProvider;
905
/// Creates a <see cref="JsonProperty"/> for the given <see cref="MemberInfo"/>.
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)
912
JsonProperty property = new JsonProperty();
913
property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member);
914
property.DeclaringType = member.DeclaringType;
915
property.ValueProvider = CreateMemberValueProvider(member);
917
bool allowNonPublicAccess;
918
SetPropertySettingsFromAttributes(property, member.GetCustomAttributeProvider(), member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess);
920
if (memberSerialization != MemberSerialization.Fields)
922
property.Readable = ReflectionUtils.CanReadMemberValue(member, allowNonPublicAccess);
923
property.Writable = ReflectionUtils.CanSetMemberValue(member, allowNonPublicAccess, property.HasMemberAttribute);
927
// write to readonly fields
928
property.Readable = true;
929
property.Writable = true;
931
property.ShouldSerialize = CreateShouldSerializeTest(member);
933
SetIsSpecifiedActions(property, member, allowNonPublicAccess);
938
private void SetPropertySettingsFromAttributes(JsonProperty property, ICustomAttributeProvider attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
940
#if !PocketPC && !NET20
941
DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(declaringType);
943
MemberInfo memberInfo = null;
944
#if !(NETFX_CORE || PORTABLE)
945
memberInfo = attributeProvider as MemberInfo;
947
memberInfo = attributeProvider.UnderlyingObject as MemberInfo;
950
DataMemberAttribute dataMemberAttribute;
951
if (dataContractAttribute != null && memberInfo != null)
952
dataMemberAttribute = JsonTypeReflector.GetDataMemberAttribute((MemberInfo) memberInfo);
954
dataMemberAttribute = null;
957
JsonPropertyAttribute propertyAttribute = JsonTypeReflector.GetAttribute<JsonPropertyAttribute>(attributeProvider);
958
if (propertyAttribute != null)
959
property.HasMemberAttribute = true;
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;
971
property.PropertyName = ResolvePropertyName(mappedName);
972
property.UnderlyingName = name;
974
bool hasMemberAttribute = false;
975
if (propertyAttribute != null)
977
property._required = propertyAttribute._required;
978
property.Order = propertyAttribute._order;
979
property.DefaultValueHandling = propertyAttribute._defaultValueHandling;
980
hasMemberAttribute = true;
982
#if !PocketPC && !NET20
983
else if (dataMemberAttribute != null)
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;
992
bool hasJsonIgnoreAttribute =
993
JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(attributeProvider) != null
994
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
995
|| JsonTypeReflector.GetAttribute<NonSerializedAttribute>(attributeProvider) != null
999
if (memberSerialization != MemberSerialization.OptIn)
1001
bool hasIgnoreDataMemberAttribute = false;
1003
#if !(NET20 || NET35)
1004
hasIgnoreDataMemberAttribute = (JsonTypeReflector.GetAttribute<IgnoreDataMemberAttribute>(attributeProvider) != null);
1007
// ignored if it has JsonIgnore or NonSerialized or IgnoreDataMember attributes
1008
property.Ignored = (hasJsonIgnoreAttribute || hasIgnoreDataMemberAttribute);
1012
// ignored if it has JsonIgnore/NonSerialized or does not have DataMember or JsonProperty attributes
1013
property.Ignored = (hasJsonIgnoreAttribute || !hasMemberAttribute);
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);
1021
DefaultValueAttribute defaultValueAttribute = JsonTypeReflector.GetAttribute<DefaultValueAttribute>(attributeProvider);
1022
if (defaultValueAttribute != null)
1023
property.DefaultValue = defaultValueAttribute.Value;
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;
1031
property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
1032
property.ItemConverter =
1033
(propertyAttribute != null && propertyAttribute.ItemConverterType != null)
1034
? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType)
1036
property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
1037
property.ItemTypeNameHandling = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null;
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;
1047
#if !PocketPC && !NET20
1048
if (dataMemberAttribute != null)
1050
allowNonPublicAccess = true;
1051
property.HasMemberAttribute = true;
1056
private Predicate<object> CreateShouldSerializeTest(MemberInfo member)
1058
MethodInfo shouldSerializeMethod = member.DeclaringType.GetMethod(JsonTypeReflector.ShouldSerializePrefix + member.Name, ReflectionUtils.EmptyTypes);
1060
if (shouldSerializeMethod == null || shouldSerializeMethod.ReturnType != typeof(bool))
1063
MethodCall<object, object> shouldSerializeCall =
1064
JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>(shouldSerializeMethod);
1066
return o => (bool)shouldSerializeCall(o);
1069
private void SetIsSpecifiedActions(JsonProperty property, MemberInfo member, bool allowNonPublicAccess)
1071
MemberInfo specifiedMember = member.DeclaringType.GetProperty(member.Name + JsonTypeReflector.SpecifiedPostfix);
1072
if (specifiedMember == null)
1073
specifiedMember = member.DeclaringType.GetField(member.Name + JsonTypeReflector.SpecifiedPostfix);
1075
if (specifiedMember == null || ReflectionUtils.GetMemberUnderlyingType(specifiedMember) != typeof(bool))
1080
Func<object, object> specifiedPropertyGet = JsonTypeReflector.ReflectionDelegateFactory.CreateGet<object>(specifiedMember);
1082
property.GetIsSpecified = o => (bool)specifiedPropertyGet(o);
1084
if (ReflectionUtils.CanSetMemberValue(specifiedMember, allowNonPublicAccess, false))
1085
property.SetIsSpecified = JsonTypeReflector.ReflectionDelegateFactory.CreateSet<object>(specifiedMember);
1089
/// Resolves the name of the property.
1091
/// <param name="propertyName">Name of the property.</param>
1092
/// <returns>Name of the property.</returns>
1093
protected internal virtual string ResolvePropertyName(string propertyName)
1095
return propertyName;
1099
/// Gets the resolved name of the property.
1101
/// <param name="propertyName">Name of the property.</param>
1102
/// <returns>Name of the property.</returns>
1103
public string GetResolvedPropertyName(string propertyName)
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);
b'\\ No newline at end of file'