Example #1
0
        private void SetJsonSourceStream(Stream stream, string fileName)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            OpenedFileName = fileName;

            JTokenRoot jsonEditorItem;

            try
            {
                jsonEditorItem = new JTokenRoot(stream);
            }
            catch
            {
                MessageBox.Show(this, string.Format("An error occured when reading \"{0}\"", OpenedFileName), @"Open...");

                OpenedFileName = null;
                SetActionStatus(@"Document NOT loaded.", true);

                return;
            }

            SetActionStatus(@"Document successfully loaded.", false);
            saveAsToolStripMenuItem.Enabled = true;

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
        /// <summary>
        /// Add a new <see cref="JToken"/> instance in current <see cref="JArrayTreeNode"/>
        /// </summary>
        /// <param name="newJToken"></param>
        private TreeNode InsertJToken(JToken newJToken, bool last = false)
        {
            var jArrayTreeNode = JTokenNode as JArrayTreeNode;

            if (jArrayTreeNode == null)
            {
                return(null);
            }

            if (!last)
            {
                jArrayTreeNode.JArrayTag.AddFirst(newJToken);
            }
            else
            {
                jArrayTreeNode.JArrayTag.Add(newJToken);
            }

            TreeNode newTreeNode = JsonTreeNodeFactory.Create(newJToken);

            if (!last)
            {
                jArrayTreeNode.Nodes.Insert(0, newTreeNode);
            }
            else
            {
                jArrayTreeNode.Nodes.Insert(jArrayTreeNode.Nodes.Count, newTreeNode);
            }

            jArrayTreeNode.TreeView.SelectedNode = newTreeNode;

            return(newTreeNode);
        }
        private JPropertyTreeNode InsertProperty(string name, object propertyValue, bool last = false)
        {
            var jObjectTreeNode = JTokenNode as JObjectTreeNode;

            if (jObjectTreeNode == null)
            {
                return(null);
            }

            var newJProperty = new JProperty(name, propertyValue);

            if (!last)
            {
                jObjectTreeNode.JObjectTag.AddFirst(newJProperty);
            }
            else
            {
                jObjectTreeNode.JObjectTag.Add(newJProperty);
            }

            var jPropertyTreeNode = (JPropertyTreeNode)JsonTreeNodeFactory.Create(newJProperty);

            jObjectTreeNode.Nodes.Insert(0, jPropertyTreeNode);

            jObjectTreeNode.TreeView.SelectedNode = jPropertyTreeNode;

            return(jPropertyTreeNode);
        }
Example #4
0
        public void SetJsonSourceStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }


            try
            {
                jsonEditorItem = new JTokenRoot(stream);
            }
            catch
            {
                MessageBox.Show(this, string.Format("An error occured when reading JSON"), @"Open...");

                SetActionStatus(@"Document NOT loaded.", true);

                return;
            }

            SetActionStatus(@"Document successfully loaded.", false);

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
Example #5
0
        private void newJsonArrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jsonEditorItem = new JTokenRoot("[]");

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
        /// <inheritdoc />
        /// <remarks>Default simple implementation to be overriden if needed.</remarks>
        public virtual TreeNode AfterJsonTextChange(string jsonString)
        {
            var jTokenRoot = new JTokenRoot(jsonString);

            if (JTokenTag.Parent != null)
            {
                JTokenTag.Replace(jTokenRoot.JTokenValue);
            }

            return(InsertInParent(JsonTreeNodeFactory.Create(jTokenRoot.JTokenValue), true));
        }
Example #7
0
        private void newJsonObjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var jsonEditorItem = new JTokenRoot("{}");

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());

            saveAsToolStripMenuItem.Enabled = true;
        }
        /// <summary>
        /// Add a new <see cref="JToken"/> instance in current <see cref="JArrayTreeNode"/>
        /// </summary>
        /// <param name="newJToken"></param>
        private void InsertJToken(JToken newJToken)
        {
            var jArrayTreeNode = JTokenNode as JArrayTreeNode;

            if (jArrayTreeNode == null)
            {
                return;
            }

            jArrayTreeNode.JArrayTag.AddFirst(newJToken);

            TreeNode newTreeNode = JsonTreeNodeFactory.Create(newJToken);

            jArrayTreeNode.Nodes.Insert(0, newTreeNode);

            jArrayTreeNode.TreeView.SelectedNode = newTreeNode;
        }
Example #9
0
        /// <summary>
        /// Click event handler for <see cref="InsertPropertyToolStripItem"/>.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertProperty_Click(Object sender, EventArgs e)
        {
            var jObjectTreeNode = JTokenNode as JObjectTreeNode;

            if (jObjectTreeNode == null)
            {
                return;
            }

            var newJProperty = new JProperty("name" + DateTime.Now.Ticks, "v");

            jObjectTreeNode.JObjectTag.AddFirst(newJProperty);

            var jPropertyTreeNode = (JPropertyTreeNode)JsonTreeNodeFactory.Create(newJProperty);

            jObjectTreeNode.Nodes.Insert(0, jPropertyTreeNode);

            jObjectTreeNode.TreeView.SelectedNode = jPropertyTreeNode;
        }
Example #10
0
        /// <inheritdoc />
        public override TreeNode AfterJsonTextChange(string jsonString)
        {
            if (CheckEmptyJsonString(jsonString))
            {
                return(null);
            }

            // To allow parsing, the partial json string is first enclosed as a json object

            var jTokenRoot = new JTokenRoot("{" + jsonString + "}");

            // Extract the contained JProperties as the JObject was only a container
            // As Json.NET internally clones JToken instances having Parent!=null when inserting in a JContainer,
            // explicitly clones the new JProperties to nullify Parent and to know of the instances
            var jParsedProperties = ((JObject)jTokenRoot.JTokenValue).Properties()
                                    .Select(p => new JProperty(p))
                                    .ToList();

            // Update the properties of parent JObject by inserting jParsedProperties and removing edited JProperty
            var jObjectParent = (JObject)JPropertyTag.Parent;

            var jProperties = jObjectParent.Properties()
                              .SelectMany(p => ReferenceEquals(p, JPropertyTag) ? jParsedProperties : new List <JProperty> {
                p
            })
                              .Distinct(new JPropertyEqualityComparer())
                              .ToList();

            jObjectParent.ReplaceAll(jProperties);

            // Build a new list of TreeNodes for these JProperties
            var jParsedTreeNodes = jParsedProperties
                                   .Select(p => JsonTreeNodeFactory.Create(p))
                                   .Cast <JPropertyTreeNode>()
                                   .ToList();

            return(UpdateTreeNodes(jParsedTreeNodes));
        }