-More documentation
-Changed CustomCreationConverter to throw NotImplementedException when serializing
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
public class CustomCreationConverterTests : TestFixtureBase
|
||||
{
|
||||
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 PersonConverter : CustomCreationConverter<IPerson>
|
||||
{
|
||||
public override IPerson Create(Type objectType)
|
||||
{
|
||||
return new Employee();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeserializeObject()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(new List<Employee>
|
||||
{
|
||||
new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1977, 12, 30, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Maurice",
|
||||
LastName = "Moss",
|
||||
Department = "IT",
|
||||
JobTitle = "Support"
|
||||
},
|
||||
new Employee
|
||||
{
|
||||
BirthDate = new DateTime(1978, 3, 15, 1, 1, 1, DateTimeKind.Utc),
|
||||
FirstName = "Jen",
|
||||
LastName = "Barber",
|
||||
Department = "IT",
|
||||
JobTitle = "Manager"
|
||||
}
|
||||
}, Formatting.Indented);
|
||||
|
||||
//[
|
||||
// {
|
||||
// "FirstName": "Maurice",
|
||||
// "LastName": "Moss",
|
||||
// "BirthDate": "\/Date(252291661000)\/",
|
||||
// "Department": "IT",
|
||||
// "JobTitle": "Support"
|
||||
// },
|
||||
// {
|
||||
// "FirstName": "Jen",
|
||||
// "LastName": "Barber",
|
||||
// "BirthDate": "\/Date(258771661000)\/",
|
||||
// "Department": "IT",
|
||||
// "JobTitle": "Manager"
|
||||
// }
|
||||
//]
|
||||
|
||||
List<IPerson> people = JsonConvert.DeserializeObject<List<IPerson>>(json, new PersonConverter());
|
||||
|
||||
IPerson person = people[0];
|
||||
|
||||
Console.WriteLine(person.GetType());
|
||||
// Newtonsoft.Json.Tests.Employee
|
||||
|
||||
Console.WriteLine(person.FirstName);
|
||||
// Maurice
|
||||
|
||||
Employee employee = (Employee)person;
|
||||
|
||||
Console.WriteLine(employee.JobTitle);
|
||||
// Support
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -480,6 +480,12 @@ Parameter name: arrayIndex")]
|
||||
}
|
||||
};
|
||||
|
||||
Console.WriteLine(shortie.Original);
|
||||
// http://www.foo.com/
|
||||
|
||||
Console.WriteLine(shortie.Error.ErrorMessage);
|
||||
// No action taken
|
||||
|
||||
Assert.AreEqual("http://www.foo.com/", shortie.Original);
|
||||
Assert.AreEqual("krehqk", shortie.Short);
|
||||
Assert.AreEqual(null, shortie.Shortened);
|
||||
|
||||
@@ -84,6 +84,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Converters\BinaryConverterTests.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverterTests.cs" />
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="JsonValidatingReaderTests.cs" />
|
||||
<Compile Include="LinqToSql\Department.cs" />
|
||||
|
||||
@@ -6,33 +6,17 @@ using System.Xml.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.TestObjects
|
||||
{
|
||||
[XmlRoot("short")]
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
public class Shortie
|
||||
{
|
||||
[JsonProperty("original")]
|
||||
[XmlElement("original")]
|
||||
public String Original { get; set; }
|
||||
|
||||
[JsonProperty("shortened")]
|
||||
[XmlElement("shortened")]
|
||||
public String Shortened { get; set; }
|
||||
|
||||
[JsonProperty("short")]
|
||||
[XmlElement("short")]
|
||||
public String Short { get; set; }
|
||||
|
||||
public string Original { get; set; }
|
||||
public string Shortened { get; set; }
|
||||
public string Short { get; set; }
|
||||
public ShortieException Error { get; set; }
|
||||
}
|
||||
|
||||
public class ShortieException
|
||||
{
|
||||
[JsonProperty("code")]
|
||||
[XmlElement("code")]
|
||||
public int Code { get; set; }
|
||||
|
||||
[JsonProperty("msg")]
|
||||
[XmlElement("msg")]
|
||||
public String ErrorMessage { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user