-Updated build number

-Example SerializationBinder test
This commit is contained in:
JamesNK
2011-11-19 09:10:38 +00:00
parent 18960815fd
commit 48dd217c84
5 changed files with 81 additions and 61 deletions
+1 -1
View File
@@ -3,7 +3,7 @@
<metadata> <metadata>
<id>Newtonsoft.Json</id> <id>Newtonsoft.Json</id>
<title>Json.NET</title> <title>Json.NET</title>
<version>4.0.3</version> <version>4.0.4</version>
<authors>James Newton-King</authors> <authors>James Newton-King</authors>
<description>Json.NET is a popular high-performance JSON framework for .NET</description> <description>Json.NET is a popular high-performance JSON framework for .NET</description>
<language>en-US</language> <language>en-US</language>
+2 -2
View File
@@ -1,6 +1,6 @@
properties { properties {
$zipFileName = "Json40r3.zip" $zipFileName = "Json40r4.zip"
$majorVersion = "4.0.3" $majorVersion = "4.0.4"
$version = GetVersion $majorVersion $version = GetVersion $majorVersion
$signAssemblies = $false $signAssemblies = $false
$signKeyPath = "D:\Development\Releases\newtonsoft.snk" $signKeyPath = "D:\Development\Releases\newtonsoft.snk"
@@ -39,23 +39,6 @@ namespace Newtonsoft.Json.Tests.Serialization
public string AuthorCountry { get; set; } public string AuthorCountry { get; set; }
} }
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
DateTime BirthDate { get; set; }
}
public class Employee : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class IPersonContractResolver : DefaultContractResolver public class IPersonContractResolver : DefaultContractResolver
{ {
protected override JsonContract CreateContract(Type objectType) protected override JsonContract CreateContract(Type objectType)
@@ -510,65 +510,79 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test] [Test]
public void SerializeUsingCustomBinder() public void SerializeUsingCustomBinder()
{ {
TypeNameSerializationBinder binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests");
IList<object> values = new List<object> IList<object> values = new List<object>
{ {
new Person new Customer
{ {
BirthDate = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc), Name = "Caroline Customer"
LastModified = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc),
Name = "Name!",
Department = "Department!"
}, },
new Employee new Purchase
{ {
BirthDate = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc), ProductName = "Elbow Grease",
FirstName = "FirstName!", Price = 5.99m,
LastName = "LastName!", Quantity = 1
Department = "Department!", }
JobTitle = "JobTitle!"
}
}; };
string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings
{ {
TypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto,
Binder = new CompleteSerializationBinder() Binder = binder
}); });
//[
// {
// "$type": "Customer",
// "Name": "Caroline Customer"
// },
// {
// "$type": "Purchase",
// "ProductName": "Elbow Grease",
// "Price": 5.99,
// "Quantity": 1
// }
//]
Assert.AreEqual(@"[ Assert.AreEqual(@"[
{ {
""$type"": ""Person"", ""$type"": ""Customer"",
""Name"": ""Name!"", ""Name"": ""Caroline Customer""
""BirthDate"": ""\/Date(972262861000)\/"",
""LastModified"": ""\/Date(972262861000)\/""
}, },
{ {
""$type"": ""Employee"", ""$type"": ""Purchase"",
""FirstName"": ""FirstName!"", ""ProductName"": ""Elbow Grease"",
""LastName"": ""LastName!"", ""Price"": 5.99,
""BirthDate"": ""\/Date(972262861000)\/"", ""Quantity"": 1
""Department"": ""Department!"",
""JobTitle"": ""JobTitle!""
} }
]", json); ]", json);
IList<object> newValues = JsonConvert.DeserializeObject<IList<object>>(json, new JsonSerializerSettings IList<object> newValues = JsonConvert.DeserializeObject<IList<object>>(json, new JsonSerializerSettings
{ {
TypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto,
Binder = new CompleteSerializationBinder() Binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests")
}); });
Assert.IsInstanceOfType(typeof(Person), newValues[0]); Assert.IsInstanceOfType(typeof(Customer), newValues[0]);
Person person = (Person)newValues[0]; Customer customer = (Customer)newValues[0];
Assert.AreEqual("Name!", person.Name); Assert.AreEqual("Caroline Customer", customer.Name);
Assert.IsInstanceOfType(typeof(Employee), newValues[1]); Assert.IsInstanceOfType(typeof(Purchase), newValues[1]);
Employee employee = (Employee)newValues[1]; Purchase purchase = (Purchase)newValues[1];
Assert.AreEqual("FirstName!", employee.FirstName); Assert.AreEqual("Elbow Grease", purchase.ProductName);
} }
public class CompleteSerializationBinder : SerializationBinder public class TypeNameSerializationBinder : SerializationBinder
{ {
public string TypeFormat { get; private set; }
public TypeNameSerializationBinder(string typeFormat)
{
TypeFormat = typeFormat;
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{ {
assemblyName = null; assemblyName = null;
@@ -577,15 +591,9 @@ namespace Newtonsoft.Json.Tests.Serialization
public override Type BindToType(string assemblyName, string typeName) public override Type BindToType(string assemblyName, string typeName)
{ {
switch (typeName) string resolvedTypeName = string.Format(TypeFormat, typeName);
{
case "Employee": return Type.GetType(resolvedTypeName, true);
return typeof (Employee);
case "Person":
return typeof (Person);
default:
throw new ArgumentException();
}
} }
} }
#endif #endif
@@ -946,6 +954,18 @@ namespace Newtonsoft.Json.Tests.Serialization
public string Language { get; set; } public string Language { get; set; }
} }
public class Customer
{
public string Name { get; set; }
}
public class Purchase
{
public string ProductName { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
#if !(WINDOWS_PHONE || SILVERLIGHT) #if !(WINDOWS_PHONE || SILVERLIGHT)
public class SerializableWrapper public class SerializableWrapper
{ {
@@ -51,4 +51,21 @@ namespace Newtonsoft.Json.Tests.TestObjects
// not serialized // not serialized
public string Department { get; set; } public string Department { get; set; }
} }
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
DateTime BirthDate { get; set; }
}
public class Employee : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public string Department { get; set; }
public string JobTitle { get; set; }
}
} }