private void LoadTree(object jsonData) { Nodes.Clear(); if (jsonData is JArray jsonArray) { var rootNode = new JsonTreeNode(JsonNodeType.Object, "(root array)"); foreach (JToken jToken in jsonArray) { var node = new JsonTreeNode(JsonNodeType.Array, jToken.Type.ToString()); rootNode.Nodes.Add(node); LoadObject(jToken as JObject, node); } TreeNode[] rootNodeArray = new TreeNode[rootNode.Nodes.Count]; rootNode.Nodes.CopyTo(rootNodeArray, 0); Nodes.Add(rootNode); SelectedNode = rootNodeArray.First() as JsonTreeNode; rootNode.ImageKey = rootNode.NodeType.ToString(); rootNode.SelectedImageKey = rootNode.ImageKey; rootNode.ExpandAll(); } if (jsonData is JObject json) { var rootNode = new JsonTreeNode(JsonNodeType.Object, "(root)"); Nodes.Add(rootNode); SelectedNode = rootNode; rootNode.ImageKey = rootNode.NodeType.ToString(); rootNode.SelectedImageKey = rootNode.ImageKey; LoadObject(json, rootNode); rootNode.ExpandAll(); } }
private void LoadArray(JToken value, JsonTreeNode node) { foreach (var item in value) { AddNode(node, null, item); } }
internal static JsonTreeNode CreateNode(string property, JToken item) { JsonNodeType type; string text; string textWhenSelected = null; if (item.Type == JTokenType.Object || item.Type == JTokenType.Array) { text = property; textWhenSelected = text; } else { type = JsonNodeType.Value; string value = item.ToString(); text = property == null ? value : string.Format($"{property}: {value}"); textWhenSelected = string.Format($"{text} (type: {item.Type})"); } type = item.Type == JTokenType.Object ? JsonNodeType.Object : item.Type == JTokenType.Array ? JsonNodeType.Array : JsonNodeType.Value; var node = new JsonTreeNode(type, text, textWhenSelected) { ImageKey = item.Type.ToString() }; node.SelectedImageKey = node.ImageKey; return(node); }
private void LoadObject(JObject obj, JsonTreeNode node) { if (obj is null) { return; } foreach (var item in obj) { AddNode(node, item.Key, item.Value); } }
private void AddNode(JsonTreeNode parentNode, string property, JToken item) { var node = JsonTreeNodeCreator.CreateNode(property, item); parentNode.Nodes.Add(node); if (item.Type == JTokenType.Array) { LoadArray(item, node); } if (item.Type == JTokenType.Object) { LoadObject(item as JObject, node); } }