コード例 #1
0
        /// <summary>
        /// remove operation
        /// </summary>
        /// <param name="target">target JToken object</param>
        /// <param name="patch">json patch object</param>
        /// <returns>JToken result after the remove operation</returns>
        public static JToken Remove(JToken target, JObject patch)
        {
            if (patch["op"].Value <string>() != "remove")
            {
                return(target);
            }

            string path = patch["path"].Value <string>();

            path = JsonPointer.ToJTokenPath(path);

            var jToken = target.SelectToken(path);

            if (jToken == null)
            {
                return(target);
            }

            if (jToken.Parent is JProperty)
            {
                jToken.Parent.Remove();
            }
            else
            {
                jToken.Remove();
            }


            return(target);
        }
コード例 #2
0
        /// <summary>
        /// replace operation
        /// </summary>
        /// <param name="target">target JToken object</param>
        /// <param name="patch">json patch object</param>
        /// <returns>JToken result after the replace operation</returns>
        public static JToken Replace(JToken target, JObject patch)
        {
            if (patch["op"].Value <string>() != "replace")
            {
                return(target);
            }

            string path  = patch["path"].Value <string>();
            JToken value = patch["value"].Value <JToken>();

            path = JsonPointer.ToJTokenPath(path);

            target.SelectToken(path).Replace(value);

            return(target);
        }
コード例 #3
0
        /// <summary>
        /// copy operation
        /// </summary>
        /// <param name="target">target JToken object</param>
        /// <param name="patch">json patch object</param>
        /// <returns>JToken result after the copy operation</returns>
        public static JToken Copy(JToken target, JObject patch)
        {
            if (patch["op"].Value <string>() != "copy")
            {
                return(target);
            }

            string from = patch["from"].Value <string>();
            string path = patch["path"].Value <string>();

            var jTokenPath = JsonPointer.ToJTokenPath(from);
            var jToken     = target.SelectToken(jTokenPath);

            Patch addPatch = new Patch("add", null, path, jToken);

            Add(target, addPatch.Value);

            return(target);
        }
コード例 #4
0
        /// <summary>
        /// test operation
        /// </summary>
        /// <param name="target">target JToken object</param>
        /// <param name="patch">json patch object</param>
        /// <returns>throwing an InvalidOperationException indicate test fail, else success</returns>
        public static void Test(JToken target, JArray patches)
        {
            foreach (JObject patch in patches)
            {
                if (patch["op"].Value <string>() != "test")
                {
                    throw new InvalidOperationException("Not a test operation: " + patch["op"].Value <string>());
                }

                string path  = patch["path"].Value <string>();
                JToken value = patch["value"].Value <JToken>();

                var jTokenPath = JsonPointer.ToJTokenPath(path);
                var jToken     = target.SelectToken(jTokenPath);

                if (!jToken.Equals(value))
                {
                    throw new InvalidOperationException("Value at " + path + " does not match.");
                }
            }
        }