-Reorganized examples
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user