diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec index 6e971282a..ab467cfb6 100644 --- a/Build/Newtonsoft.Json.nuspec +++ b/Build/Newtonsoft.Json.nuspec @@ -3,7 +3,7 @@ Newtonsoft.Json Json.NET - 4.0.3 + 4.0.4 James Newton-King Json.NET is a popular high-performance JSON framework for .NET en-US diff --git a/Build/build.ps1 b/Build/build.ps1 index af6fdfe9e..b09412261 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -1,6 +1,6 @@ properties { - $zipFileName = "Json40r3.zip" - $majorVersion = "4.0.3" + $zipFileName = "Json40r4.zip" + $majorVersion = "4.0.4" $version = GetVersion $majorVersion $signAssemblies = $false $signKeyPath = "D:\Development\Releases\newtonsoft.snk" diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs index e1938df18..eef6b5e12 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs @@ -39,23 +39,6 @@ public class Book 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 { protected override JsonContract CreateContract(Type objectType) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 92b77b579..9c386960c 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -510,65 +510,79 @@ public override Type BindToType(string assemblyName, string typeName) [Test] public void SerializeUsingCustomBinder() { + TypeNameSerializationBinder binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests"); + IList values = new List { - new Person + new Customer { - 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!" + Name = "Caroline Customer" }, - new Employee - { - BirthDate = new DateTime(2000, 10, 23, 1, 1, 1, DateTimeKind.Utc), - FirstName = "FirstName!", - LastName = "LastName!", - Department = "Department!", - JobTitle = "JobTitle!" - } + new Purchase + { + ProductName = "Elbow Grease", + Price = 5.99m, + Quantity = 1 + } }; string json = JsonConvert.SerializeObject(values, Formatting.Indented, new JsonSerializerSettings { 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(@"[ { - ""$type"": ""Person"", - ""Name"": ""Name!"", - ""BirthDate"": ""\/Date(972262861000)\/"", - ""LastModified"": ""\/Date(972262861000)\/"" + ""$type"": ""Customer"", + ""Name"": ""Caroline Customer"" }, { - ""$type"": ""Employee"", - ""FirstName"": ""FirstName!"", - ""LastName"": ""LastName!"", - ""BirthDate"": ""\/Date(972262861000)\/"", - ""Department"": ""Department!"", - ""JobTitle"": ""JobTitle!"" + ""$type"": ""Purchase"", + ""ProductName"": ""Elbow Grease"", + ""Price"": 5.99, + ""Quantity"": 1 } ]", json); IList newValues = JsonConvert.DeserializeObject>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, - Binder = new CompleteSerializationBinder() + Binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests") }); - Assert.IsInstanceOfType(typeof(Person), newValues[0]); - Person person = (Person)newValues[0]; - Assert.AreEqual("Name!", person.Name); + Assert.IsInstanceOfType(typeof(Customer), newValues[0]); + Customer customer = (Customer)newValues[0]; + Assert.AreEqual("Caroline Customer", customer.Name); - Assert.IsInstanceOfType(typeof(Employee), newValues[1]); - Employee employee = (Employee)newValues[1]; - Assert.AreEqual("FirstName!", employee.FirstName); + Assert.IsInstanceOfType(typeof(Purchase), newValues[1]); + Purchase purchase = (Purchase)newValues[1]; + 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) { assemblyName = null; @@ -577,15 +591,9 @@ public override void BindToName(Type serializedType, out string assemblyName, ou public override Type BindToType(string assemblyName, string typeName) { - switch (typeName) - { - case "Employee": - return typeof (Employee); - case "Person": - return typeof (Person); - default: - throw new ArgumentException(); - } + string resolvedTypeName = string.Format(TypeFormat, typeName); + + return Type.GetType(resolvedTypeName, true); } } #endif @@ -946,6 +954,18 @@ public class SearchDetails 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) public class SerializableWrapper { diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs b/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs index 159473c97..70176b82c 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs @@ -51,4 +51,21 @@ public class Person // not serialized 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; } + } } \ No newline at end of file