Skip to content

Commit

Permalink
-Updated build number
Browse files Browse the repository at this point in the history
-Example SerializationBinder test
  • Loading branch information
JamesNK committed Nov 19, 2011
1 parent 1896081 commit 48dd217
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Build/Newtonsoft.Json.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions Build/build.ps1
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 0 additions & 17 deletions Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
102 changes: 61 additions & 41 deletions Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
Expand All @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
17 changes: 17 additions & 0 deletions Src/Newtonsoft.Json.Tests/TestObjects/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

0 comments on commit 48dd217

Please sign in to comment.