1

I'm trying to use Json.NET to serialize and deserialze a tree structure for jsTree.

Here are the class definitions:

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; }
        }
    }

And here is some example 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}}]}]}]}]}]}]

I try to deserialize like this: List<TreeNode> tree = (List<TreeNode>)JsonConvert.DeserializeObject(form["treeJson"], typeof (List<TreeNode>));

The tree structure is deserialized correctly, but none of the nodes have Metadata.

Anyone see what's going wrong here?

Thanks!

2 Answers 2

1

When i change Metadata property as

[JsonProperty(PropertyName = "metadata")]
public Metadata Metadata { get; set; }

it seems to work fine

var tree = JsonConvert.DeserializeObject<List<TreeNode>>(jsonstring);

Any reason for implementing it as

[JsonProperty(PropertyName = "metadata")]
public Metadata Metadata
{
    get
    {
        return new Metadata() {NodeType = NodeType, TypeDepth = TypeDepth};
    }
    set { 
          TypeDepth = value.TypeDepth;
           NodeType = value.NodeType;
    }
}

?

Sign up to request clarification or add additional context in comments.

1 Comment

The property was implemented as such to make the existing tree code compatible with jsTree. Was wondering if that setter had something to do with it, but didn't want to change a bunch of code on a hunch. Thanks.
0

The class metadata is private. From what I know, this will prevent the Json serializer from accessing it; therefore, it will leave the property value null;

1 Comment

Hmm, still not working after changing class to public. Any other ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.