PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Json45r7/Source/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs

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