-Added support for BindToName on SerializationBinder
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#if !(NET35 || NET20)
|
||||
#if !(NET35 || NET20 || WINDOWS_PHONE)
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -4511,5 +4511,98 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(1, c2.Categories["one"]);
|
||||
Assert.AreEqual(2, c2.Categories["two"]);
|
||||
}
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class ClassAttributeBase
|
||||
{
|
||||
[JsonProperty]
|
||||
public string BaseClassValue { get; set; }
|
||||
}
|
||||
|
||||
public class ClassAttributeDerived : ClassAttributeBase
|
||||
{
|
||||
[JsonProperty]
|
||||
public string DerivedClassValue { get; set; }
|
||||
|
||||
public string NonSerialized { get; set; }
|
||||
}
|
||||
|
||||
public class CollectionClassAttributeDerived : ClassAttributeBase, ICollection<object>
|
||||
{
|
||||
[JsonProperty]
|
||||
public string CollectionDerivedClassValue { get; set; }
|
||||
|
||||
public void Add(object item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Contains(object item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void CopyTo(object[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public bool Remove(object item)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerator<object> GetEnumerator()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClassAttributesInheritance()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new ClassAttributeDerived
|
||||
{
|
||||
BaseClassValue = "BaseClassValue!",
|
||||
DerivedClassValue = "DerivedClassValue!",
|
||||
NonSerialized = "NonSerialized!"
|
||||
}, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""DerivedClassValue"": ""DerivedClassValue!"",
|
||||
""BaseClassValue"": ""BaseClassValue!""
|
||||
}", json);
|
||||
|
||||
json = JsonConvert.SerializeObject(new CollectionClassAttributeDerived
|
||||
{
|
||||
BaseClassValue = "BaseClassValue!",
|
||||
CollectionDerivedClassValue = "CollectionDerivedClassValue!"
|
||||
}, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""CollectionDerivedClassValue"": ""CollectionDerivedClassValue!"",
|
||||
""BaseClassValue"": ""BaseClassValue!""
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -430,7 +430,6 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
Assert.AreEqual(5, nested[4]);
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC
|
||||
[Test]
|
||||
public void DeserializeUsingCustomBinder()
|
||||
{
|
||||
@@ -460,6 +459,89 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
return typeof (Person);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(NET20 || NET35)
|
||||
[Test]
|
||||
public void SerializeUsingCustomBinder()
|
||||
{
|
||||
IList<object> values = new List<object>
|
||||
{
|
||||
new Person
|
||||
{
|
||||
BirthDate = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc),
|
||||
LastModified = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc),
|
||||
Name = "Name!",
|
||||
Department = "Department!"
|
||||
},
|
||||
new Employee
|
||||
{
|
||||
BirthDate = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "FirstName!",
|
||||
LastName = "LastName!",
|
||||
Department = "Department!",
|
||||
JobTitle = "JobTitle!"
|
||||
}
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
Binder = new CompleteSerializationBinder()
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""$type"": ""Person"",
|
||||
""Name"": ""Name!"",
|
||||
""BirthDate"": ""\/Date(972262861000)\/"",
|
||||
""LastModified"": ""\/Date(972262861000)\/""
|
||||
},
|
||||
{
|
||||
""$type"": ""Employee"",
|
||||
""FirstName"": ""FirstName!"",
|
||||
""LastName"": ""LastName!"",
|
||||
""BirthDate"": ""\/Date(972262861000)\/"",
|
||||
""Department"": ""Department!"",
|
||||
""JobTitle"": ""JobTitle!""
|
||||
}
|
||||
]", json);
|
||||
|
||||
IList<object> newValues = JsonConvert.DeserializeObject<IList<object>>(json, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
Binder = new CompleteSerializationBinder()
|
||||
});
|
||||
|
||||
Assert.IsInstanceOfType(typeof(Person), newValues[0]);
|
||||
Person person = (Person)newValues[0];
|
||||
Assert.AreEqual("Name!", person.Name);
|
||||
|
||||
Assert.IsInstanceOfType(typeof(Employee), newValues[1]);
|
||||
Employee employee = (Employee)newValues[1];
|
||||
Assert.AreEqual("FirstName!", employee.FirstName);
|
||||
}
|
||||
|
||||
public class CompleteSerializationBinder : SerializationBinder
|
||||
{
|
||||
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
|
||||
{
|
||||
assemblyName = null;
|
||||
typeName = serializedType.Name;
|
||||
}
|
||||
|
||||
public override Type BindToType(string assemblyName, string typeName)
|
||||
{
|
||||
switch (typeName)
|
||||
{
|
||||
case "Employee":
|
||||
return typeof (Employee);
|
||||
case "Person":
|
||||
return typeof (Person);
|
||||
default:
|
||||
throw new ArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in New Issue
Block a user