Beispiel #1
0
        protected override void Add(AddOperation operation, JToken target)
        {
            JToken  token        = null;
            JObject parenttoken  = null;
            var     parentPath   = operation.Path.ParentPointer.ToString();
            int     index        = parentPath == "/" ? parentPath.Length : parentPath.Length + 1;
            var     propertyName = operation.Path.ToString().Substring(index);

            try
            {
                var  parentArray  = operation.Path.ParentPointer.Find(target) as JArray;
                bool isNewPointer = operation.Path.IsNewPointer();
                if (parentArray == null || isNewPointer)
                {
                    if (isNewPointer)
                    {
                        var parentPointer = operation.Path.ParentPointer;
                        token = parentPointer.Find(target) as JArray;
                    }
                    else
                    {
                        token = operation.Path.Find(target);
                    }
                }
                else
                {
                    parentArray.Insert(int.Parse(propertyName), operation.Value);
                    return;
                }
            }
            catch (ArgumentException)
            {
                var parentPointer = operation.Path.ParentPointer;
                parenttoken = parentPointer.Find(target) as JObject;
            }

            if (token == null && parenttoken != null)
            {
                parenttoken.Add(propertyName, operation.Value);
            }
            else if (token is JArray)
            {
                var array = token as JArray;

                array.Add(operation.Value);
            }
            else if (token.Parent is JProperty)
            {
                var prop = token.Parent as JProperty;
                prop.Value = operation.Value;
            }
        }
        protected override void Add(AddOperation operation, JToken target)
        {
            JToken  token        = null;
            JObject parenttoken  = null;
            var     propertyName = operation.Path.ToString().Split('/').LastOrDefault();

            try
            {
                var parentArray = operation.Path.ParentPointer.Find(target) as JArray;

                if (parentArray == null || propertyName == "-")
                {
                    if (operation.Path.IsNewPointer())
                    {
                        var parentPointer = operation.Path.ParentPointer;
                        token = parentPointer.Find(target) as JArray;
                    }
                    else
                    {
                        token = operation.Path.Find(target);
                    }
                }
                else
                {
                    parentArray.Insert(int.Parse(propertyName), operation.Value);
                    return;
                }
            }
            catch (ArgumentException)
            {
                var parentPointer = operation.Path.ParentPointer;
                parenttoken = parentPointer.Find(target) as JObject;
            }

            if (token == null && parenttoken != null)
            {
                parenttoken.Add(propertyName, operation.Value);
            }
            else if (token is JArray)
            {
                var array = token as JArray;

                array.Add(operation.Value);
            }
            else if (token.Parent is JProperty)
            {
                var prop = token.Parent as JProperty;
                prop.Value = operation.Value;
            }
        }