I have this structure in C# and I want to create JSON that can be translated to it successfully so that I can easily find my mistake in the JS logic.
public class Tree
{
public Root Root { get; set; }
public Tree()
{}
}
public class Root : Node
{
public Root(int _id, int _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
: base(_id, _fk, _name, _code, _children, _leaves)
{
}
public Root()
{ }
}
public abstract class Node
{
public int? ID { get; set; }
public int? FK { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public List<Node> children { get; set; }
public List<Item> leaves { get; set; }
public Node(int? _id, int? _fk, string _name, string _code, List<Node> _children, List<Item> _leaves)
{
ID = _id;
FK = _fk;
Name = _name;
Code = _code;
children = _children;
leaves = _leaves;
}
public Node ()
{}
}
public class Item
{
public string Code { get; set; }
public string Desc { get; set; }
public string Uom { get; set; }
public float? Measurements { get; set; }
public int? FK { get; set; }
public int? Tier { get; set; }
public bool IsADPresent { get; set; }
}
and the basic most JSON I am trying to feed in command JsonConvert.DeserializeObject<Tree>(tree)
is:
{
"Tree": {
"Root": {
"children": [],
"leaves": [],
"FK": 1,
"ID": 1,
"Name": " LOGISTICS",
"Code": "PT01"
}
}
}
but I still get null in Tree; let alone when the JSON gets much more hairy (the tree can have up to the 5th grandchild).
Thanks
UPDATE: upon removing Tree from JSON I am given exception:
Newtonsoft.Json.JsonSerializationException: 'Could not create an instance of type StatusUpdater.Models.NPoco.Node. Type is an interface or abstract class and cannot be instantiated. Path 'Root.children[0].children', line 1, position 33.'