You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
8.7 KiB

  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.Runtime.Serialization;
  27. using System.Reflection;
  28. using System.Globalization;
  29. using Newtonsoft.Json.Utilities;
  30. using System.Collections.Generic;
  31. namespace Newtonsoft.Json.Serialization
  32. {
  33. /// <summary>
  34. /// The default serialization binder used when resolving and loading classes from type names.
  35. /// </summary>
  36. internal class DefaultSerializationBinder :
  37. #pragma warning disable 618
  38. SerializationBinder,
  39. #pragma warning restore 618
  40. ISerializationBinder
  41. {
  42. internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();
  43. private readonly ThreadSafeStore<TypeNameKey, Type> _typeCache;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="DefaultSerializationBinder"/> class.
  46. /// </summary>
  47. public DefaultSerializationBinder()
  48. {
  49. _typeCache = new ThreadSafeStore<TypeNameKey, Type>(GetTypeFromTypeNameKey);
  50. }
  51. private Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
  52. {
  53. string assemblyName = typeNameKey.AssemblyName;
  54. string typeName = typeNameKey.TypeName;
  55. if (assemblyName != null)
  56. {
  57. Assembly assembly;
  58. #if !(DOTNET || PORTABLE40 || PORTABLE)
  59. // look, I don't like using obsolete methods as much as you do but this is the only way
  60. // Assembly.Load won't check the GAC for a partial name
  61. #pragma warning disable 618,612
  62. assembly = Assembly.LoadWithPartialName(assemblyName);
  63. #pragma warning restore 618,612
  64. #elif DOTNET || PORTABLE
  65. assembly = Assembly.Load(new AssemblyName(assemblyName));
  66. #else
  67. assembly = Assembly.Load(assemblyName);
  68. #endif
  69. #if HAVE_APP_DOMAIN
  70. if (assembly == null)
  71. {
  72. // will find assemblies loaded with Assembly.LoadFile outside of the main directory
  73. Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  74. foreach (Assembly a in loadedAssemblies)
  75. {
  76. // check for both full name or partial name match
  77. if (a.FullName == assemblyName || a.GetName().Name == assemblyName)
  78. {
  79. assembly = a;
  80. break;
  81. }
  82. }
  83. }
  84. #endif
  85. if (assembly == null)
  86. {
  87. throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));
  88. }
  89. Type type = assembly.GetType(typeName);
  90. if (type == null)
  91. {
  92. // if generic type, try manually parsing the type arguments for the case of dynamically loaded assemblies
  93. // example generic typeName format: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
  94. if (typeName.IndexOf('`') >= 0)
  95. {
  96. try
  97. {
  98. type = GetGenericTypeFromTypeName(typeName, assembly);
  99. }
  100. catch (Exception ex)
  101. {
  102. throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName), ex);
  103. }
  104. }
  105. if (type == null)
  106. {
  107. throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
  108. }
  109. }
  110. return type;
  111. }
  112. else
  113. {
  114. return Type.GetType(typeName);
  115. }
  116. }
  117. private Type GetGenericTypeFromTypeName(string typeName, Assembly assembly)
  118. {
  119. Type type = null;
  120. int openBracketIndex = typeName.IndexOf('[');
  121. if (openBracketIndex >= 0)
  122. {
  123. string genericTypeDefName = typeName.Substring(0, openBracketIndex);
  124. Type genericTypeDef = assembly.GetType(genericTypeDefName);
  125. if (genericTypeDef != null)
  126. {
  127. List<Type> genericTypeArguments = new List<Type>();
  128. int scope = 0;
  129. int typeArgStartIndex = 0;
  130. int endIndex = typeName.Length - 1;
  131. for (int i = openBracketIndex + 1; i < endIndex; ++i)
  132. {
  133. char current = typeName[i];
  134. switch (current)
  135. {
  136. case '[':
  137. if (scope == 0)
  138. {
  139. typeArgStartIndex = i + 1;
  140. }
  141. ++scope;
  142. break;
  143. case ']':
  144. --scope;
  145. if (scope == 0)
  146. {
  147. string typeArgAssemblyQualifiedName = typeName.Substring(typeArgStartIndex, i - typeArgStartIndex);
  148. TypeNameKey typeNameKey = ReflectionUtils.SplitFullyQualifiedTypeName(typeArgAssemblyQualifiedName);
  149. genericTypeArguments.Add(GetTypeByName(typeNameKey));
  150. }
  151. break;
  152. }
  153. }
  154. type = genericTypeDef.MakeGenericType(genericTypeArguments.ToArray());
  155. }
  156. }
  157. return type;
  158. }
  159. private Type GetTypeByName(TypeNameKey typeNameKey)
  160. {
  161. return _typeCache.Get(typeNameKey);
  162. }
  163. /// <summary>
  164. /// When overridden in a derived class, controls the binding of a serialized object to a type.
  165. /// </summary>
  166. /// <param name="assemblyName">Specifies the <see cref="Assembly"/> name of the serialized object.</param>
  167. /// <param name="typeName">Specifies the <see cref="System.Type"/> name of the serialized object.</param>
  168. /// <returns>
  169. /// The type of the object the formatter creates a new instance of.
  170. /// </returns>
  171. public override Type BindToType(string assemblyName, string typeName)
  172. {
  173. return GetTypeByName(new TypeNameKey(assemblyName, typeName));
  174. }
  175. /// <summary>
  176. /// When overridden in a derived class, controls the binding of a serialized object to a type.
  177. /// </summary>
  178. /// <param name="serializedType">The type of the object the formatter creates a new instance of.</param>
  179. /// <param name="assemblyName">Specifies the <see cref="Assembly"/> name of the serialized object.</param>
  180. /// <param name="typeName">Specifies the <see cref="System.Type"/> name of the serialized object.</param>
  181. public
  182. #if !NET20
  183. override
  184. #endif
  185. void BindToName(Type serializedType, out string assemblyName, out string typeName)
  186. {
  187. assemblyName = serializedType.Assembly.FullName;
  188. typeName = serializedType.FullName;
  189. }
  190. }
  191. }