-DefaultSerializationBinder WIP
This commit is contained in:
@@ -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<int, HashSet<string>> dictionary = new Dictionary<int, HashSet<string>>
|
||||
{
|
||||
{ 1, new HashSet<string>(new[] { "test" }) },
|
||||
};
|
||||
|
||||
string obtainedJson = JsonConvert.SerializeObject(dictionary, serializerSettings);
|
||||
|
||||
Dictionary<int, HashSet<string>> obtainedDictionary = (Dictionary<int, HashSet<string>>)JsonConvert.DeserializeObject(obtainedJson, serializerSettings);
|
||||
|
||||
Assert.IsNotNull(obtainedDictionary);
|
||||
}
|
||||
}
|
||||
|
||||
public class DataType
|
||||
|
||||
@@ -96,39 +96,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
}
|
||||
|
||||
internal struct TypeNameKey : IEquatable<TypeNameKey>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, controls the binding of a serialized object to a type.
|
||||
/// </summary>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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<TypeNameKey>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user