'TreeView To JSON

From the JSON file, I was able to build a treeView in windows forms. Now I need to do the reverse process - save the tree to a JSON file after pressing the button. I have structure for serialize/deserialize to/from file. This is how my tree looks like: enter image description here

This is part of my code, which build my tree:

public TreeViewEditor()
    {
            InitializeComponent(); 
    }

    public void CreateTree(string fileName, TreeView treeView)
    {
        PcNcCommon.SerializationMechanisms _machParamData = new PcNcCommon.SerializationMechanisms();
        if (_machParamData.JsonDeserialization(fileName))
        {
            JObject jObject = new JObject();
            using (StreamReader file_jObject = File.OpenText(fileName))
            using (JsonTextReader reader1 = new JsonTextReader(file_jObject))
            {
                jObject = JObject.Load(reader1);
            }
            treeView.Nodes.Clear();
            TreeNode parent = JsonToTree(jObject);
            parent.Text = "Root Object";
            treeView.Nodes.Add(parent);
        }
        else
            MessageBox.Show("Deserialization problem");
    }

    public TreeNode JsonToTree(JObject obj)
    {
        //create the parent node
        TreeNode parent = new TreeNode();
        //loop through the obj. all token should be pair<key, value>
        foreach (var token in obj)
        {
            //change the display Content of the parent
            parent.Text = token.Key.ToString();
            //create the child node
            TreeNode child = new TreeNode();
            child.Text = token.Key.ToString();
            //check if the value is of type obj recall the method
            if (token.Value.Type.ToString() == "Object")
            {
                // child.Text = token.Key.ToString();
                //create a new JObject using the the Token.value
                JObject o = (JObject)token.Value;
                //recall the method
                child = JsonToTree(o);
                child.Text = token.Key.ToString();
                //add the child to the parentNode
                parent.Nodes.Add(child);
            }
            //if type is of array
            else if (token.Value.Type.ToString() == "Array")
            {
                int ix = -1;
                //  child.Text = token.Key.ToString();
                //loop though the array
                foreach (var itm in token.Value)
                {
                    //check if value is an Array of objects
                    if (itm.Type.ToString() == "Object")
                    {
                        TreeNode objTN = new TreeNode();
                        //child.Text = token.Key.ToString();
                        //call back the method
                        ix++;

                        JObject o = (JObject)itm;
                        objTN = JsonToTree(o);
                        objTN.Text = token.Key.ToString() + "[" + ix + "]";
                        child.Nodes.Add(objTN);
                        //parent.Nodes.Add(child);
                    }
                    //regular array string, int, etc
                    else if (itm.Type.ToString() == "Array")
                    {
                        ix++;
                        TreeNode dataArray = new TreeNode();
                        foreach (var data in itm)
                        {
                            dataArray.Text = token.Key.ToString() + "[" + ix + "]";
                            dataArray.Nodes.Add(data.ToString());
                        }
                        child.Nodes.Add(dataArray);
                    }

                    else
                    {
                        child.Nodes.Add(itm.ToString());
                    }
                }
                parent.Nodes.Add(child);
            }
            else
            {
                if (token.Value.ToString() == "")
                    child.Nodes.Add("N/A");
                else
                    child.Nodes.Add(token.Value.ToString());
                parent.Nodes.Add(child);
            }
        }
        return parent;
    }
    private void buttonBuildTree_Click(object sender, EventArgs e)
    {
        var fileName = textBoxPath.Text;
        CreateTree(fileName, treeView);
        treeView.Update();
    }

And I need to get something like this:enter image description here

SerializationMechanisms.JsonDeserialization doing this:

Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
this._NcMachParam_t = (NcMachParam_t)serializer.Deserialize(file, typeof(NcMachParam_t));
return true;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source