Beispiel #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());
        }
Beispiel #2
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());
        }
Beispiel #3
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);
        }
        /// <summary>
        /// Sets the json string to show.
        /// </summary>
        /// <param name="jsonString"></param>
        public void SetJsonSource(string jsonString)
        {
            var jsonEditorItem = new JTokenRoot(jsonString);

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
Beispiel #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));
        }
Beispiel #7
0
        /// <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);
        }
Beispiel #8
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;
        }
Beispiel #9
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));
        }
        /// <summary>
        /// Sets the stream from which to get the json string to show.
        /// </summary>
        /// <param name="stream"></param>
        public void SetJsonSource(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            JTokenRoot jsonEditorItem;

            try
            {
                jsonEditorItem = new JTokenRoot(stream);
            }
            catch (Exception exception)
            {
                throw new WrongJsonStreamException("Stream could not be parsed from json", exception);
            }

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue, 2));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
        private void SetJsonSourceStream(Stream stream, string fileName)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            OpenedFileName = fileName;

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

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
                .Cast<TreeNode>()
                .ForEach(n => n.Expand());
        }
        private void newJsonObjectToolStripMenuItem_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());
        }
Beispiel #13
0
        private void CreateEmptyJson(string path)
        {
            var jsonEditorItem = new JTokenRoot("[]");

            File.WriteAllText(path, jsonEditorItem.JTokenValue.ToString());
        }
Beispiel #14
0
        private void CreateEmptyJson(string path)
        {
            var jsonEditorItem = new JTokenRoot("[]");

            Helper.WriteAnsiFile(path, jsonEditorItem.JTokenValue.ToString());
        }