Szeregowanie drzewa w obiekcie Json
Mam następujące klasy:
TreeNode.cs
public class TreeNode : IEnumerable<TreeNode>
{
public readonly Dictionary<string, TreeNode> _children = new Dictionary<string, TreeNode>();
public readonly string Id;
public TreeNode Parent { get; private set; }
public TreeNode(string id)
{
this.Id = id;
}
public TreeNode GetChild(string id)
{
return this._childs[id];
}
public void Add(TreeNode item)
{
if (item.Parent != null)
{
item.Parent._childs.Remove(item.Id);
}
item.Parent = this;
this._childs.Add(item.Id, item);
}
public IEnumerator<TreeNode> GetEnumerator()
{
return this._childs.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public int Count
{
get { return this._childs.Count; }
}
}
FolderStructureNode.cs
public class FolderStructureNode : TreeNode
{
//Some properties such as FolderName, RelativePath etc.
}
Więc kiedy mam obiekt typuFolderStructureNode
jest to zasadniczo struktura drzewa, w której każdy węzeł reprezentuje folder. Chcę serializować ten obiekt w JsonObject. Próbowałem obu - JavaScriptSerializer i NewtonSoft. W obu przypadkach otrzymuję wynik jako -
[
[
[]
],
[
[]
],
[]
]
W czasie serializacji drzewo wygląda mniej więcej tak:
Jak go serializować, aby uzyskać poprawny obiekt json? Czy muszę przechodzić przez drzewo i samemu stworzyć jsona?