-DefaultSerializationBinder WIP
This commit is contained in:
@@ -1891,6 +1891,26 @@ namespace Newtonsoft.Json.Tests.Serialization
|
|||||||
Assert.AreEqual("property", deserialized.Rows["key"].First().SomeProperty);
|
Assert.AreEqual("property", deserialized.Rows["key"].First().SomeProperty);
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
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>
|
/// <summary>
|
||||||
/// When overridden in a derived class, controls the binding of a serialized object to a type.
|
/// When overridden in a derived class, controls the binding of a serialized object to a type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -778,14 +778,12 @@ namespace Newtonsoft.Json.Serialization
|
|||||||
|
|
||||||
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
if (resolvedTypeNameHandling != TypeNameHandling.None)
|
||||||
{
|
{
|
||||||
string typeName;
|
TypeNameKey typeNameKey = ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName);
|
||||||
string assemblyName;
|
|
||||||
ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName);
|
|
||||||
|
|
||||||
Type specifiedType;
|
Type specifiedType;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
specifiedType = Serializer._binder.BindToType(assemblyName, typeName);
|
specifiedType = Serializer._binder.BindToType(typeNameKey.AssemblyName, typeNameKey.TypeName);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -858,10 +858,13 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName)
|
public static TypeNameKey SplitFullyQualifiedTypeName(string fullyQualifiedTypeName)
|
||||||
{
|
{
|
||||||
int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName);
|
int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName);
|
||||||
|
|
||||||
|
string typeName;
|
||||||
|
string assemblyName;
|
||||||
|
|
||||||
if (assemblyDelimiterIndex != null)
|
if (assemblyDelimiterIndex != null)
|
||||||
{
|
{
|
||||||
typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.GetValueOrDefault()).Trim();
|
typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.GetValueOrDefault()).Trim();
|
||||||
@@ -872,6 +875,8 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
typeName = fullyQualifiedTypeName;
|
typeName = fullyQualifiedTypeName;
|
||||||
assemblyName = null;
|
assemblyName = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new TypeNameKey(assemblyName, typeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int? GetAssemblyDelimiterIndex(string fullyQualifiedTypeName)
|
private static int? GetAssemblyDelimiterIndex(string fullyQualifiedTypeName)
|
||||||
@@ -1121,4 +1126,36 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
return Activator.CreateInstance(type);
|
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