Example #1
0
        public static JsonPatchDocument Read(TextReader r)
        {
            JsonPatchDocument patch = new JsonPatchDocument();

              JsonReader jsr = new JsonReader();
              CompleteOperation[] operations = null;
              try
              {
            operations = jsr.Read<CompleteOperation[]>(r);
              }
              catch (DeserializationException ex)
              {
            throw new JsonPatchParserException(ex.Message, ex);
              }

              foreach (CompleteOperation operation in operations)
              {
            try
            {
              switch (operation.op)
              {
            case "add":
              patch.Add(operation.path, operation.value);
              break;
            case "remove":
              patch.Remove(operation.path);
              break;
            case "replace":
              patch.Replace(operation.path, operation.value);
              break;
            case "move":
              patch.Move(operation.from, operation.path);
              break;
            case "copy":
              patch.Copy(operation.from, operation.path);
              break;
            case "test":
              patch.Test(operation.path, operation.value);
              break;
            case null:
              throw new JsonPatchParserException("No 'op' property found.");
              }
            }
            catch (ArgumentNullException ex)
            {
              throw new JsonPatchParserException(string.Format("Missing parameter '{0}' for op:'{1}'.", ex.ParamName, operation.op), ex);
            }
              }

              return patch;
        }
Example #2
0
        public void CanBuildRemove()
        {
            // Arrange
              JsonPatchDocument patch = new JsonPatchDocument();

              // Act
              patch.Remove("/Title");

              // Assert
              dynamic operations = patch.Operations;
              Assert.IsNotNull(operations);
              Assert.AreEqual(1, operations.Count);
              Assert.AreEqual("remove", operations[0].op);
              Assert.AreEqual("/Title", operations[0].path);

              // Not the best way to test, but some how we need to know that it Builds good JSON
              Assert.AreEqual(@"[{""op"":""remove"",""path"":""/Title""}]", patch.ToString());
        }
Example #3
0
        public void CanBuildAdd()
        {
            // Arrange
              JsonPatchDocument patch = new JsonPatchDocument();

              // Act
              patch.Add("/RelatedParties", new BugReport.Party { Name = "Liza" });

              // Assert
              dynamic operations = patch.Operations;
              Assert.IsNotNull(operations);
              Assert.AreEqual(1, operations.Count);
              Assert.AreEqual("add", operations[0].op);
              Assert.AreEqual("/RelatedParties", operations[0].path);

              // Not the best way to test, but some how we need to know that it Builds good JSON
              Assert.AreEqual(@"[{""value"":{""Name"":""Liza"",""Id"":0},""op"":""add"",""path"":""/RelatedParties""}]", patch.ToString());
        }
Example #4
0
        public void CanPatchDocument()
        {
            // Arrange
              JsonPatchDocument patch = new JsonPatchDocument();
              patch.Add("/X", 10);
              patch.Remove("/Y");
              patch.Add("/øÅ", "üÆ$€");

              Request request = Session.Bind(PatchTemplate);

              // Act
              using (var response = request.Patch<string>(patch))
              {
            // Assert - patch was sent to server and streamed back again
            Assert.IsNotNull(response.Body);
            Assert.AreEqual(@"[{""value"":10,""op"":""add"",""path"":""/X""},{""op"":""remove"",""path"":""/Y""},{""value"":""\u00FC\u00C6$\u20AC"",""op"":""add"",""path"":""/\u00F8\u00C5""}]", response.Body);
              }
        }
Example #5
0
        public void CanChainWithFluentInterface_typed()
        {
            // Arrange
              JsonPatchDocument<BugReport> patch = new JsonPatchDocument<BugReport>();

              // Act
              patch.Test(r => r.Title, "TestValue")
               .Replace(r => r.Description, "...")
               .Add(r => r.RelatedParties, "")
               .Remove(r => r.Responsible)
               .Copy(r => r.Title, r => r.AnotherTitle)
               .Move(r => r.Title, r => r.AnotherTitle);

              Assert.AreEqual(6, patch.Operations.Count);
              Assert.AreEqual("test", patch.Operations[0].op);
              Assert.AreEqual("move", patch.Operations[5].op);
        }
Example #6
0
        public void CanChainWithFluentInterface()
        {
            // Arrange
              JsonPatchDocument patch = new JsonPatchDocument();

              // Act
              patch.Test("Title", "TestValue")
               .Replace("Description", "...")
               .Add("RelatedParties", "")
               .Remove("Responsible")
               .Copy("Title", "AnotherTitle")
               .Move("Title", "AnotherTitle");

              Assert.AreEqual(6, patch.Operations.Count);
              Assert.AreEqual("test", patch.Operations[0].op);
              Assert.AreEqual("move", patch.Operations[5].op);
        }
Example #7
0
        public void CanBuildTypedTest()
        {
            // Arrange
              JsonPatchDocument<BugReport> patch = new JsonPatchDocument<BugReport>();

              // Act
              patch.Test(r => r.Title, "TestValue");

              // Assert
              dynamic operations = patch.Operations;
              Assert.IsNotNull(operations);
              Assert.AreEqual(1, operations.Count);
              Assert.AreEqual("test", operations[0].op);
              Assert.AreEqual("/Title", operations[0].path);
              Assert.AreEqual("TestValue", operations[0].value);

              // Not the best way to test, but some how we need to know that it Builds good JSON
              Assert.AreEqual(@"[{""value"":""TestValue"",""op"":""test"",""path"":""/Title""}]", patch.ToString());
        }
Example #8
0
        public void CanBuildTypedReplaceWithObjectValue()
        {
            // Arrange
              BugReport.Party party = new BugReport.Party { Name = "John" };
              JsonPatchDocument<BugReport> patch = new JsonPatchDocument<BugReport>();

              // Act
              patch.Replace(r => r.Responsible, party);

              // Assert
              dynamic operations = patch.Operations;
              Assert.IsNotNull(operations);
              Assert.AreEqual(1, operations.Count);
              Assert.AreEqual("replace", operations[0].op);
              Assert.AreEqual("/Responsible", operations[0].path);
              Assert.AreEqual(party, operations[0].value);

              // Not the best way to test, but some how we need to know that it Builds good JSON
              Assert.AreEqual(@"[{""value"":{""Name"":""John"",""Id"":0},""op"":""replace"",""path"":""/Responsible""}]", patch.ToString());
        }
Example #9
0
        public void CanBuildTypedReplace()
        {
            // Arrange
              JsonPatchDocument<BugReport> patch = new JsonPatchDocument<BugReport>();

              // Act
              patch.Replace(r => r.Responsible.Name, "Bummer");

              // Assert
              dynamic operations = patch.Operations;
              Assert.IsNotNull(operations);
              Assert.AreEqual(1, operations.Count);
              Assert.AreEqual("replace", operations[0].op);
              Assert.AreEqual("/Responsible/Name", operations[0].path);
              Assert.AreEqual("Bummer", operations[0].value);

              // Not the best way to test, but some how we need to know that it Builds good JSON
              Assert.AreEqual(@"[{""value"":""Bummer"",""op"":""replace"",""path"":""/Responsible/Name""}]", patch.ToString());
        }
Example #10
0
 public object Patch(JsonPatchDocument patch)
 {
   return patch.ToString();
 }
Example #11
0
 public object Patch(JsonPatchDocument patch)
 {
     return Get();
 }