Example #1
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));
        }
        /// <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));
        }