예제 #1
0
        public void Copy_property()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer   = new JsonPointer("/books/0/ISBN");
            var topointer     = new JsonPointer("/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 = new JsonPointer("/books/1/ISBN").Find(sample);

            Assert.AreEqual("21123123", result.Value <string>());
        }
예제 #2
0
        public void Insert_array_elements()
        {
            var sample = PatchTests.GetSample2();

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

            patchDocument.AddOperation(new AddEachOperation()
            {
                Path  = pointer,
                Value = new JArray()
                {
                    new JObject(new[] { new JProperty("author", "James Brown") }),
                    new JObject(new[] { new JProperty("cat", "Garfield") }),
                    new JObject(new[] { new JProperty("producer", "Kingston") }),
                }
            });

            patchDocument.ApplyTo(sample);

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

            Assert.Equal(5, list.Count);
            Assert.Equal(list[0]["author"].ToString(), "James Brown");
            Assert.Equal(list[1]["cat"].ToString(), "Garfield");
            Assert.Equal(list[2]["producer"].ToString(), "Kingston");
        }
예제 #3
0
        public void Remove_array_item_by_matching()
        {
            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();
            string pointer       = "$.books[?(@.author == 'John Steinbeck')]";

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

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

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

            Assert.Single(list);
        }
예제 #4
0
        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>());
        }
예제 #5
0
        public void Remove_an_array_element_with_numbered_custom_fields()
        {
            var sample = JToken.Parse(@"{
    'data': {
        '2017PropertyOne' : '2017 property one value',
        '2017PropertyTwo' : '2017 property two value',
        '2017Properties' : ['First value from 2017','Second value from 2017'],
        '2018PropertyOne' : '2018 property value',
        '2018PropertyTwo' : '2018 property two value',
        '2018Properties' : ['First value from 2018','Second value from 2018']
    }
}");

            Assert.NotNull(sample.SelectPatchToken("/data/2017Properties/1"));

            var    patchDocument = new PatchDocument();
            string pointer       = "/data/2017Properties/0";

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

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            Assert.Null(sample.SelectPatchToken("/data/2017Properties/1"));
        }
예제 #6
0
        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));
        }
예제 #7
0
        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);
        }
예제 #8
0
        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));
        }
예제 #9
0
        public void Replace_a_property_value_with_a_new_value()
        {
            var sample = PatchTests.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>());
        }
예제 #10
0
        public void Test_a_value()
        {
            var sample = PatchTests.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);
            });
        }
예제 #11
0
        public void Remove_a_property()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            Assert.Throws(typeof(ArgumentException), () => { pointer.Find(sample); });
        }
예제 #12
0
        public void Replace_a_property_value_with_a_new_value()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(new JsonNetTargetAdapter(sample));

            Assert.Equal("Bob Brown", (string)pointer.Find(sample));
        }
예제 #13
0
        public void Remove_array_item_by_value()
        {
            var sample = JToken.Parse(@"{ 'tags': [ 'tag1', 'tag2', 'tag3' ] }");

            var    patchDocument = new PatchDocument();
            string pointer       = "$.tags[?(@ == 'tag2')]";

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

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

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

            Assert.Equal(2, list.Count);
        }
예제 #14
0
        public void Replace_a_property_value_with_an_object()
        {
            var sample = PatchTests.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>());
        }
예제 #15
0
        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"));
        }
예제 #16
0
        public void Append_array_elements_wrongDataType()
        {
            var sample = PatchTests.GetSample2();

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

            patchDocument.AddOperation(new AddEachOperation()
            {
                Path  = pointer,
                Value = new JObject(new[] { new JProperty("producer", "Kingston") })
            });

            Assert.Throws(typeof(ArgumentException), () => {
                patchDocument.ApplyTo(sample);
            });
        }
예제 #17
0
        public void Add_an_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

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

            Assert.Equal(3, list.Count);
        }
예제 #18
0
        public void Replace_a_property_value_with_an_object()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(new JsonNetTargetAdapter(sample));

            var newPointer = new JsonPointer("/books/0/author/hello");

            Assert.Equal("world", (string)newPointer.Find(sample));
        }
예제 #19
0
        public void Can_replace_existing_boolean()
        {
            var sample = JToken.FromObject(new MyConfigClass {
                RequiresConfiguration = true
            });

            var patchDocument = new PatchDocument();

            patchDocument.AddOperation(new ReplaceOperation {
                Path = "/RequiresConfiguration", Value = new JValue(false)
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            Assert.False(sample.ToObject <MyConfigClass>().RequiresConfiguration);
        }
예제 #20
0
        public void Remove_an_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            Assert.Throws(typeof(PathNotFoundException), () =>
            {
                var x = pointer.Find("/books/1");
            });
        }
예제 #21
0
        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);
        }
예제 #22
0
        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);
        }
예제 #23
0
        public void Add_an_non_existing_member_property()  // Why isn't this replace?
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);


            var result = (string)pointer.Find(sample);

            Assert.Equal("213324234343", result);
        }
예제 #24
0
        public void Add_an_array_element_to_non_existent_property()
        {
            var sample = GetSample2();

            var    patchDocument = new PatchDocument();
            string pointer       = "/someobject/somearray/-";

            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["someobject"]["somearray"] as JArray;

            Assert.Single(list);
        }
예제 #25
0
        public void Add_an_non_existing_member_property()  // Why isn't this replace?
        {
            var sample = PatchTests.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);
        }
예제 #26
0
        public void Copy_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            var result = new JsonPointer("/books/2").Find(sample);

            Assert.IsType(typeof(JObject), result);
        }
예제 #27
0
        public void Move_array_element()
        {
            var sample = GetSample2();

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

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

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken(topointer);

            Assert.IsType <JObject>(result);
        }
예제 #28
0
        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);
        }
예제 #29
0
        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);
        }
예제 #30
0
        public void Move_property()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);


            var result = (string)topointer.Find(sample);

            Assert.Equal("F. Scott Fitzgerald", result);
        }