@@ -9618,6 +9618,23 @@ Path '', line 1, position 1.");
|
|||||||
"Cannot populate list type System.Net.Mime.HeaderCollection. Path 'Headers', line 26, position 14.");
|
"Cannot populate list type System.Net.Mime.HeaderCollection. Path 'Headers', line 26, position 14.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void DeserializeDictionaryOfHashSetsWithTypeNameHandlingAll()
|
||||||
|
{
|
||||||
|
var dictionary = new Dictionary<int, HashSet<string>>
|
||||||
|
{
|
||||||
|
{ 1, new HashSet<string>(new[] { "test" }) },
|
||||||
|
};
|
||||||
|
|
||||||
|
var jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
|
||||||
|
|
||||||
|
var obtainedJson = JsonConvert.SerializeObject(dictionary, jsonSerializerSettings);
|
||||||
|
|
||||||
|
var obtainedDictionary = (Dictionary<int, HashSet<string>>)JsonConvert.DeserializeObject(obtainedJson, jsonSerializerSettings);
|
||||||
|
|
||||||
|
Assert.IsNotNull(obtainedDictionary);
|
||||||
|
}
|
||||||
|
|
||||||
public class MailAddressReadConverter : JsonConverter
|
public class MailAddressReadConverter : JsonConverter
|
||||||
{
|
{
|
||||||
public override bool CanConvert(Type objectType)
|
public override bool CanConvert(Type objectType)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ using System.Runtime.Serialization;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using Newtonsoft.Json.Utilities;
|
using Newtonsoft.Json.Utilities;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Newtonsoft.Json.Serialization
|
namespace Newtonsoft.Json.Serialization
|
||||||
{
|
{
|
||||||
@@ -38,9 +39,17 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
{
|
{
|
||||||
internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();
|
internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();
|
||||||
|
|
||||||
private readonly ThreadSafeStore<TypeNameKey, Type> _typeCache = new ThreadSafeStore<TypeNameKey, Type>(GetTypeFromTypeNameKey);
|
private readonly ThreadSafeStore<TypeNameKey, Type> _typeCache;
|
||||||
|
|
||||||
private static Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="DefaultSerializationBinder"/> class.
|
||||||
|
/// </summary>
|
||||||
|
public DefaultSerializationBinder()
|
||||||
|
{
|
||||||
|
_typeCache = new ThreadSafeStore<TypeNameKey, Type>(GetTypeFromTypeNameKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
|
||||||
{
|
{
|
||||||
string assemblyName = typeNameKey.AssemblyName;
|
string assemblyName = typeNameKey.AssemblyName;
|
||||||
string typeName = typeNameKey.TypeName;
|
string typeName = typeNameKey.TypeName;
|
||||||
@@ -68,7 +77,8 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||||
foreach (Assembly a in loadedAssemblies)
|
foreach (Assembly a in loadedAssemblies)
|
||||||
{
|
{
|
||||||
if (a.FullName == assemblyName)
|
// check for both full name or partial name match
|
||||||
|
if (a.FullName == assemblyName || a.GetName().Name == assemblyName)
|
||||||
{
|
{
|
||||||
assembly = a;
|
assembly = a;
|
||||||
break;
|
break;
|
||||||
@@ -83,10 +93,27 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
Type type = assembly.GetType(typeName);
|
Type type = assembly.GetType(typeName);
|
||||||
|
if (type == null)
|
||||||
|
{
|
||||||
|
// if generic type, try manually parsing the type arguments for the case of dynamically loaded assemblies
|
||||||
|
// 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]]
|
||||||
|
if (typeName.IndexOf('`') >= 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
type = GetGenericTypeFromTypeName(typeName, assembly);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (type == null)
|
if (type == null)
|
||||||
{
|
{
|
||||||
throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
|
throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
@@ -96,6 +123,52 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Type GetGenericTypeFromTypeName(string typeName, Assembly assembly)
|
||||||
|
{
|
||||||
|
Type type = null;
|
||||||
|
var openBracketIndex = typeName.IndexOf('[');
|
||||||
|
if (openBracketIndex >= 0)
|
||||||
|
{
|
||||||
|
var genericTypeDefName = typeName.Substring(0, openBracketIndex);
|
||||||
|
var genericTypeDef = assembly.GetType(genericTypeDefName);
|
||||||
|
if (genericTypeDef != null)
|
||||||
|
{
|
||||||
|
var typeArgs = new List<Type>();
|
||||||
|
var scope = 0;
|
||||||
|
var typeArgStartIndex = 0;
|
||||||
|
var endIndex = typeName.Length - 1;
|
||||||
|
for (var i = openBracketIndex + 1; i < endIndex; ++i)
|
||||||
|
{
|
||||||
|
var current = typeName[i];
|
||||||
|
switch (current)
|
||||||
|
{
|
||||||
|
case '[':
|
||||||
|
if (scope == 0)
|
||||||
|
{
|
||||||
|
typeArgStartIndex = i + 1;
|
||||||
|
}
|
||||||
|
++scope;
|
||||||
|
break;
|
||||||
|
case ']':
|
||||||
|
--scope;
|
||||||
|
if (scope == 0)
|
||||||
|
{
|
||||||
|
var typeArgAssemblyQualifiedName = typeName.Substring(typeArgStartIndex, i - typeArgStartIndex);
|
||||||
|
string typeArgTypeName;
|
||||||
|
string typeArgAssemblyName;
|
||||||
|
ReflectionUtils.SplitFullyQualifiedTypeName(typeArgAssemblyQualifiedName, out typeArgTypeName, out typeArgAssemblyName);
|
||||||
|
var typeArg = BindToType(typeArgAssemblyName, typeArgTypeName);
|
||||||
|
typeArgs.Add(typeArg);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type = genericTypeDef.MakeGenericType(typeArgs.ToArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
internal struct TypeNameKey : IEquatable<TypeNameKey>
|
internal struct TypeNameKey : IEquatable<TypeNameKey>
|
||||||
{
|
{
|
||||||
internal readonly string AssemblyName;
|
internal readonly string AssemblyName;
|
||||||
|
|||||||
Reference in New Issue
Block a user