Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Net deserialization using setter Methods

Tags:

c#

json.net

Using the following example is it possible to use the AddChild Setter method in Json.Net during Deserialization to populate the list of children?

public class Foo
{
    private IList<Foo> _children;
    private Foo _parent;

    public Foo()
    {
         _children = new List<Foo>();
    }

    public string Name { get; set; }
    public IEnumerable<Foo> Children
    {
         get { return _children.AsEnumerable() }
    }

    public void AddChild(Foo child)
    {
         child._parent = this;
         _children.Add(child);
    }
}
like image 247
Raspar Avatar asked May 09 '26 14:05

Raspar


1 Answers

You can add a custom type converter by creating a concrete implementation of the JsonConverter class.

like image 62
Paul Tyng Avatar answered May 11 '26 04:05

Paul Tyng