我想使用Json.NET来为jsTree序列化和反序列化树结构。Json.NET不反序列化嵌套对象
下面是类定义:
private class Metadata
{
[JsonProperty(PropertyName = "nodeType")]
public NodeType NodeType;
[JsonProperty(PropertyName = "typeDepth")]
public int TypeDepth;
}
private class TreeNode
{
[JsonProperty(PropertyName = "data")]
public String Title;
[JsonIgnore]
public NodeType NodeType;
[JsonIgnore]
public int TypeDepth;
[JsonProperty(PropertyName = "children", NullValueHandling = NullValueHandling.Ignore)]
public List<TreeNode> Children;
[JsonProperty(PropertyName = "metadata")]
public Metadata Metadata
{
get
{
return new Metadata() {NodeType = NodeType, TypeDepth = TypeDepth};
}
set {
TypeDepth = value.TypeDepth;
NodeType = value.NodeType;
}
}
private ItemGroup _itemGroup;
[JsonIgnore]
public ItemGroup ItemGroup
{
get
{
if(this.NodeType != NodeType.ItemGroup)
throw new InvalidDataException();
return _itemGroup;
}
set { _itemGroup = value; }
}
}
,这里是一些例子JSON:
[{
"data":"Strands",
"attr":{"class":""},
"state":"open",
"metadata":{
"nodeType":3,
"typeDepth":0},
"children":[{
"data":"Math",
"attr":{"class":"","rel":"itemGroup"},
"state":"open",
"metadata":{
"nodeType":1,
"typeDepth":0},
"children":[{
"data":"Subjects",
"attr":{"class":""},
"state":"open",
"metadata":{"nodeType":3,"typeDepth":1},
"children":[{
"data":"Algebra 1",
"attr":{"class":"","rel":"itemGroup"},
"state":"open",
"metadata":{
"nodeType":1,
"typeDepth":1},
"children":[{
"data":"Clusters",
"attr":{"class":""},
"state":"open",
"metadata":{
"nodeType":3,
"typeDepth":2},
"children":[{
"data":"Factoring",
"attr":{"rel":"itemGroup"},
"metadata":{
"nodeType":1,
"typeDepth":2}},
{"data":"Substitution",
"attr":{"class":"","rel":"itemGroup"},
"metadata":{"nodeType":1,"typeDepth":2}}]}]}]}]}]}]
我尝试反序列化这样的:List<TreeNode> tree = (List<TreeNode>)JsonConvert.DeserializeObject(form["treeJson"], typeof (List<TreeNode>));
树状结构正确反序列化,但没有任何节点具有元数据。
有人看到这里有什么问题吗?
谢谢!
该属性被实现为使现有树代码与jsTree兼容。想知道这个二传手是否与它有关,但并不想在预感上改变一堆代码。谢谢。 –