Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2011-11-19 13:10:38 +0400
committerJamesNK <james@newtonking.com>2011-11-19 13:10:38 +0400
commit48dd217c847470bd4a5a89a22bcb6945e825517b (patch)
tree85a8088fab77c6431ba25554c6419f61e58cd06f
parent18960815fdda9a2317f559d1e6fb016979c3f319 (diff)
-Updated build number
-Example SerializationBinder test
-rw-r--r--Build/Newtonsoft.Json.nuspec2
-rw-r--r--Build/build.ps14
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs17
-rw-r--r--Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs102
-rw-r--r--Src/Newtonsoft.Json.Tests/TestObjects/Person.cs17
5 files changed, 81 insertions, 61 deletions
diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec
index 6e97128..ab467cf 100644
--- a/Build/Newtonsoft.Json.nuspec
+++ b/Build/Newtonsoft.Json.nuspec
@@ -3,7 +3,7 @@
<metadata>
<id>Newtonsoft.Json</id>
<title>Json.NET</title>
- <version>4.0.3</version>
+ <version>4.0.4</version>
<authors>James Newton-King</authors>
<description>Json.NET is a popular high-performance JSON framework for .NET</description>
<language>en-US</language>
diff --git a/Build/build.ps1 b/Build/build.ps1
index af6fdfe..b094122 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 e1938df..eef6b5e 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs
@@ -39,23 +39,6 @@ namespace Newtonsoft.Json.Tests.Serialization
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 92b77b5..9c38696 100644
--- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
+++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
@@ -510,65 +510,79 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeUsingCustomBinder()
{
+ TypeNameSerializationBinder binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests");
+
IList<object> values = new List<object>
{
- 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<object> newValues = JsonConvert.DeserializeObject<IList<object>>(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 @@ namespace Newtonsoft.Json.Tests.Serialization
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 @@ namespace Newtonsoft.Json.Tests.Serialization
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 159473c..70176b8 100644
--- a/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs
+++ b/Src/Newtonsoft.Json.Tests/TestObjects/Person.cs
@@ -51,4 +51,21 @@ namespace Newtonsoft.Json.Tests.TestObjects
// 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