Files
Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Documentation/Examples/DeserializeCustomCreationConverter.cs
T
James Newton-King 68eacc480b -JSON Schema fixes
-Fixed static values being included with fields serialization
-Examples! (work in progress)
2013-01-23 21:39:54 +13:00

53 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Tests.Documentation.Examples
{
public class DeserializeCustomCreationConverter
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class PersonConverter : CustomCreationConverter<Person>
{
public override Person Create(Type objectType)
{
return new Employee();
}
}
public void Example()
{
string json = @"{
""Department"": ""Furniture"",
""JobTitle"": ""Carpenter"",
""FirstName"": ""John"",
""LastName"": ""Joinery"",
""BirthDate"": ""1983-02-02T00:00:00""
}";
Person person = JsonConvert.DeserializeObject<Person>(json, new PersonConverter());
Console.WriteLine(person.GetType().Name);
// Employee
Employee employee = (Employee) person;
Console.WriteLine(employee.JobTitle);
// Capenter
}
}
}