public static Operation Build(JObject jOperation)
        {
            var op = PatchDocument.CreateOperation((string)jOperation["op"]);

            op.Read(jOperation);
            return(op);
        }
        public static PatchDocument Load(JArray document) {
            var root = new PatchDocument();

            if (document == null)
                return root;

            foreach (var jOperation in document.Children().Cast<JObject>()) {
                var op = Operation.Build(jOperation);
                root.AddOperation(op);
            }

            return root;
        }
        public void Add_an_non_existing_member_property() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/SBN";

            patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "213324234343" });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken(pointer).Value<string>();
            Assert.Equal("213324234343", result);
        }
        public void Copy_array_element() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer = "/books/0";
            var topointer = "/books/-";

            patchDocument.AddOperation(new CopyOperation { FromPath = frompointer, Path = topointer });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken("/books/2");
            Assert.IsType(typeof(JObject), result);
        }
        public void Copy_property() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer = "/books/0/ISBN";
            var topointer = "/books/1/ISBN";

            patchDocument.AddOperation(new AddOperation { Path = frompointer, Value = new JValue("21123123") });
            patchDocument.AddOperation(new CopyOperation { FromPath = frompointer, Path = topointer });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken("/books/1/ISBN");
            Assert.Equal("21123123", result);
        }
        public void Add_an_existing_member_property()  // Why isn't this replace?
        {

            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/title";

            patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "Little Red Riding Hood" });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken(pointer).Value<string>();
            Assert.Equal("Little Red Riding Hood", result);
        }
        public void Add_an_array_element() {

            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/-";

            patchDocument.AddOperation(new AddOperation { Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") }) });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var list = sample["books"] as JArray;

            Assert.Equal(3, list.Count);
        }
        public static PatchDocument Load(JArray document)
        {
            var root = new PatchDocument();

            if (document == null)
            {
                return(root);
            }

            foreach (var jOperation in document.Children().Cast <JObject>())
            {
                var op = Operation.Build(jOperation);
                root.AddOperation(op);
            }

            return(root);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType != typeof(PatchDocument))
            {
                throw new ArgumentException("Object must be of type PatchDocument", nameof(objectType));
            }

            try {
                if (reader.TokenType == JsonToken.Null)
                {
                    return(null);
                }

                var patch = JArray.Load(reader);
                return(PatchDocument.Parse(patch.ToString()));
            } catch (Exception ex) {
                throw new ArgumentException("Invalid patch document: " + ex.Message);
            }
        }
        public async Task JsonPatch() {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Default);

            var patch = new PatchDocument(new ReplaceOperation { Path = "name", Value = "Patched" });
            await _employeeRepository.PatchAsync(employee.Id, patch);

            employee = await _employeeRepository.GetByIdAsync(employee.Id);
            Assert.Equal("Patched", employee.Name);
            Assert.Equal(2, employee.Version);
        }
        public void Test_a_value() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/author";

            patchDocument.AddOperation(new TestOperation { Path = pointer, Value = new JValue("Billy Burton") });

            Assert.Throws(typeof(InvalidOperationException), () => {
                var patcher = new JsonPatcher();
                patcher.Patch(ref sample, patchDocument);
            });
        }
        public void Remove_a_property() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/author";

            patchDocument.AddOperation(new RemoveOperation { Path = pointer });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Null(sample.SelectPatchToken(pointer));
        }
        public void SerializePatchDocument() {
            var patchDoc = new PatchDocument(
                new TestOperation { Path = "/a/b/c", Value = new JValue("foo") },
                new RemoveOperation { Path = "/a/b/c" },
                new AddOperation { Path = "/a/b/c", Value = new JArray(new JValue("foo"), new JValue("bar")) },
                new ReplaceOperation { Path = "/a/b/c", Value = new JValue(42) },
                new MoveOperation { FromPath = "/a/b/c", Path = "/a/b/d" },
                new CopyOperation { FromPath = "/a/b/d", Path = "/a/b/e" });

            var outputstream = patchDoc.ToStream();
            var output = new StreamReader(outputstream).ReadToEnd();

            var jOutput = JToken.Parse(output);

            Assert.Equal(@"[{""op"":""test"",""path"":""/a/b/c"",""value"":""foo""},{""op"":""remove"",""path"":""/a/b/c""},{""op"":""add"",""path"":""/a/b/c"",""value"":[""foo"",""bar""]},{""op"":""replace"",""path"":""/a/b/c"",""value"":42},{""op"":""move"",""path"":""/a/b/d"",""from"":""/a/b/c""},{""op"":""copy"",""path"":""/a/b/e"",""from"":""/a/b/d""}]",
                jOutput.ToString(Formatting.None));
        }
        public void Move_property() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer = "/books/0/author";
            var topointer = "/books/1/author";

            patchDocument.AddOperation(new MoveOperation { FromPath = frompointer, Path = topointer });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken(topointer).Value<string>();
            Assert.Equal("F. Scott Fitzgerald", result);
        }
        public void Remove_an_array_element() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0";

            patchDocument.AddOperation(new RemoveOperation { Path = pointer });

            var patcher = new JsonPatcher();
            patcher.Patch(ref sample, patchDocument);

            Assert.Null(sample.SelectPatchToken("/books/1"));
        }
        public void Replace_a_property_value_with_a_new_value() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/author";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Bob Brown" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("Bob Brown", sample.SelectPatchToken(pointer).Value<string>());
        }
        public void Replace_non_existant_property() {
            var sample = JToken.Parse(@"{ ""data"": {} }");

            var patchDocument = new PatchDocument();
            var pointer = "/data/author";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Bob Brown" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("Bob Brown", sample.SelectPatchToken(pointer).Value<string>());

            sample = JToken.Parse(@"{}");

            patchDocument = new PatchDocument();
            pointer = "/data/author";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Bob Brown" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("Bob Brown", sample.SelectPatchToken(pointer).Value<string>());

            sample = JToken.Parse(@"{}");

            patchDocument = new PatchDocument();
            pointer = "/";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Bob Brown" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("Bob Brown", sample.SelectPatchToken(pointer).Value<string>());

            sample = JToken.Parse(@"{}");

            patchDocument = new PatchDocument();
            pointer = "/hey/now/0/you";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Bob Brown" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("{}", sample.ToString(Formatting.None));
        }
        public void Replace_a_property_value_with_an_object() {
            var sample = GetSample2();

            var patchDocument = new PatchDocument();
            var pointer = "/books/0/author";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = new JObject(new[] { new JProperty("hello", "world") }) });

            new JsonPatcher().Patch(ref sample, patchDocument);

            var newPointer = "/books/0/author/hello";
            Assert.Equal("world", sample.SelectPatchToken(newPointer).Value<string>());
        }
        public void CreateEmptyPatch() {
            var sample = GetSample2();
            var sampletext = sample.ToString();

            var patchDocument = new PatchDocument();
            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal(sampletext, sample.ToString());
        }
        public void Replace_multiple_property_values_with_jsonpath() {

            var sample = JToken.Parse(@"{
    'books': [
        {
          'title' : 'The Great Gatsby',
          'author' : 'F. Scott Fitzgerald'
        },
        {
          'title' : 'The Grapes of Wrath',
          'author' : 'John Steinbeck'
        },
        {
          'title' : 'Some Other Title',
          'author' : 'John Steinbeck'
        }
    ]
}");

            var patchDocument = new PatchDocument();
            var pointer = "$.books[?(@.author == 'John Steinbeck')].author";

            patchDocument.AddOperation(new ReplaceOperation { Path = pointer, Value = "Eric" });

            new JsonPatcher().Patch(ref sample, patchDocument);

            var newPointer = "/books/1/author";
            Assert.Equal("Eric", sample.SelectPatchToken(newPointer).Value<string>());

            newPointer = "/books/2/author";
            Assert.Equal("Eric", sample.SelectPatchToken(newPointer).Value<string>());
        }