나는 다음과 같은 클래스가 :JSON 객체로 트리를 직렬화
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.
}
을 나는 물체가있을 때 유형이 FolderStructureNode
인 경우 본질적으로 각 노드가 폴더를 나타내는 트리 데이터 구조입니다. 이 객체를 JsonObject로 직렬화하려고합니다. 나는 JavaScriptSerializer와 NewtonSoft를 모두 시도했다. 내가 올바른 JSON을 얻기 위해 직렬화 어떻게
: 직렬화시
[
[
[]
],
[
[]
],
[]
]
, 나무가 다음과 같이 보입니다 - 두 경우 모두에서 나는 출력이 으려고 목적? 나는 나무를 가로 질러 스스로 json을 만들어야합니까?
저는 serializer가 객체의 속성을 살펴보고 대신 _childs 사전을 속성으로 감싸는 것이 좋다고 생각합니다. – Christian
문제는'TreeNode' 클래스가'IEnumerable'을 구현하여 시리얼 라이저가 노드를 배열로 직렬화 할 수 있다는 것입니다 (귀하의 경우 비어 있음). IEnumerable 구현 코드를 보지 않고 왜 비어 있는지 말할 수 없다. –
fero
전체 TreeNode 클래스를 추가했습니다. – mridula