I have a type, Person, that contains an array of additional Persons:
public class Person {
public String FirstName { get; set; }
public String LastName { get; set; }
public int Age { get; set; }
public List<Person> Children { get; internal set; }
public Person(Person parent)
{
if (parent != null) {
this.LastName = parent.LastName;
}
}
}
I want to deserialize this from JSON using the NewtonSoft library.
{
FirstName: "George",
LastName: "Jetson",
Age: 37,
Children: [{
FirstName: "Elroy",
Age: 7
}]
}
Elroy's last name should be inherited from that outer object. I deserialize this like this:
var jetsons = JsonConvert.Deserialize<Person>(json);
but the constructor for Elroy gets a null passed to it and therefore doesn't get his last name set.
How can I get the deserializer to pass the parent object as an argument to children's constructor?