diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 4490fde4..8c2e61ee 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -1891,6 +1891,26 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("property", deserialized.Rows["key"].First().SomeProperty); } #endif + + [Test] + public void DeserializeDictionary() + { + JsonSerializerSettings serializerSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }; + + Dictionary> dictionary = new Dictionary> + { + { 1, new HashSet(new[] { "test" }) }, + }; + + string obtainedJson = JsonConvert.SerializeObject(dictionary, serializerSettings); + + Dictionary> obtainedDictionary = (Dictionary>)JsonConvert.DeserializeObject(obtainedJson, serializerSettings); + + Assert.IsNotNull(obtainedDictionary); + } } public class DataType diff --git a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs index 5df55dc2..c18779c6 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs @@ -96,39 +96,6 @@ namespace Newtonsoft.Json.Serialization } } - internal struct TypeNameKey : IEquatable - { - internal readonly string AssemblyName; - internal readonly string TypeName; - - public TypeNameKey(string assemblyName, string typeName) - { - AssemblyName = assemblyName; - TypeName = typeName; - } - - public override int GetHashCode() - { - return ((AssemblyName != null) ? AssemblyName.GetHashCode() : 0) - ^ ((TypeName != null) ? TypeName.GetHashCode() : 0); - } - - public override bool Equals(object obj) - { - if (!(obj is TypeNameKey)) - { - return false; - } - - return Equals((TypeNameKey)obj); - } - - public bool Equals(TypeNameKey other) - { - return (AssemblyName == other.AssemblyName && TypeName == other.TypeName); - } - } - /// /// When overridden in a derived class, controls the binding of a serialized object to a type. /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index ff1be4c8..2551f6cd 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -778,14 +778,12 @@ namespace Newtonsoft.Json.Serialization if (resolvedTypeNameHandling != TypeNameHandling.None) { - string typeName; - string assemblyName; - ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName); + TypeNameKey typeNameKey = ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName); Type specifiedType; try { - specifiedType = Serializer._binder.BindToType(assemblyName, typeName); + specifiedType = Serializer._binder.BindToType(typeNameKey.AssemblyName, typeNameKey.TypeName); } catch (Exception ex) { diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 382d8b02..637db561 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -858,10 +858,13 @@ namespace Newtonsoft.Json.Utilities } #endif - public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName) + public static TypeNameKey SplitFullyQualifiedTypeName(string fullyQualifiedTypeName) { int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName); + string typeName; + string assemblyName; + if (assemblyDelimiterIndex != null) { typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.GetValueOrDefault()).Trim(); @@ -872,6 +875,8 @@ namespace Newtonsoft.Json.Utilities typeName = fullyQualifiedTypeName; assemblyName = null; } + + return new TypeNameKey(assemblyName, typeName); } private static int? GetAssemblyDelimiterIndex(string fullyQualifiedTypeName) @@ -1121,4 +1126,36 @@ namespace Newtonsoft.Json.Utilities return Activator.CreateInstance(type); } } + + internal struct TypeNameKey : IEquatable + { + internal readonly string AssemblyName; + internal readonly string TypeName; + + public TypeNameKey(string assemblyName, string typeName) + { + AssemblyName = assemblyName; + TypeName = typeName; + } + + public override int GetHashCode() + { + return (AssemblyName?.GetHashCode() ?? 0) ^ (TypeName?.GetHashCode() ?? 0); + } + + public override bool Equals(object obj) + { + if (!(obj is TypeNameKey)) + { + return false; + } + + return Equals((TypeNameKey)obj); + } + + public bool Equals(TypeNameKey other) + { + return (AssemblyName == other.AssemblyName && TypeName == other.TypeName); + } + } } \ No newline at end of file