public string JsonPatchesWorks(string leftString, string rightString) { var left = JToken.Parse(leftString); var right = JToken.Parse(rightString); var patchDoc = new JsonDiffer().Diff(left, right); var patcher = new JsonPatcher(); patcher.Patch(ref left, patchDoc); Assert.True(JToken.DeepEquals(left, right)); //var patchedLeft = left.ToString(Formatting.None); //var expected = right.ToString(Formatting.None); //Assert.AreEqual(expected, patchedLeft); var patchJson = patchDoc.ToString(Formatting.None); return patchJson; }
public string JsonPatchesWorks(string leftString, string patchString) { var left = JToken.Parse(leftString); var patchDoc = PatchDocument.Parse(patchString); var patcher = new JsonPatcher(); patcher.Patch(ref left, patchDoc); var patchJson = left.ToString(Formatting.None); return patchJson; }
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 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 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.Equal("21123123", result); }
public void Move_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/1"); var topointer = new JsonPointer("/books/0/child"); patchDocument.AddOperation(new MoveOperation() { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = topointer.Find(sample); Assert.IsType(typeof(JObject), result); }
public void Test_a_value() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/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 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 }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)topointer.Find(sample); Assert.Equal("F. Scott Fitzgerald", result); }
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") }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)pointer.Find(sample); Assert.Equal("213324234343", result); }
public void Add_an_non_existing_member_property() { var sample = GetSample2(); var patchDocument = new PatchDocument(); string pointer = "/books/0/SBN"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "213324234343" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); string result = sample.SelectPatchToken(pointer).Value <string>(); Assert.Equal("213324234343", result); }
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); }
public void Add_an_existing_member_property() // Why isn't this replace? { var sample = GetSample2(); var patchDocument = new PatchDocument(); string pointer = "/books/0/title"; patchDocument.AddOperation(new AddOperation { Path = pointer, Value = "Little Red Riding Hood" }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); string 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(); string 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 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 }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); Assert.Throws(typeof(ArgumentException), () => { var x = pointer.Find("/books/1"); }); }
public void ComplexExampleWithDeepArrayChange() { var leftPath = @".\samples\scene{0}a.json"; var rightPath = @".\samples\scene{0}b.json"; var i = 1; while (File.Exists(string.Format(leftPath, i))) { var scene1Text = File.ReadAllText(string.Format(leftPath, i)); var scene1 = JToken.Parse(scene1Text); var scene2Text = File.ReadAllText(string.Format(rightPath, i)); var scene2 = JToken.Parse(scene2Text); var patchDoc = new JsonDiffer().Diff(scene1, scene2, true); //Assert.AreEqual("[{\"op\":\"remove\",\"path\":\"/items/0/entities/1\"}]", var patcher = new JsonPatcher(); patcher.Patch(ref scene1, patchDoc); Assert.True(JToken.DeepEquals(scene1, scene2)); i++; } }
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); }
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 ComplexExampleWithDeepArrayChange() { var leftPath = @".\samples\scene{0}a.json"; var rightPath = @".\samples\scene{0}b.json"; var i = 1; while(File.Exists(string.Format(leftPath, i))) { var scene1Text = File.ReadAllText(string.Format(leftPath, i)); var scene1 = JToken.Parse(scene1Text); var scene2Text = File.ReadAllText(string.Format(rightPath, i)); var scene2 = JToken.Parse(scene2Text); var patchDoc = new JsonDiffer().Diff(scene1, scene2, true); //Assert.AreEqual("[{\"op\":\"remove\",\"path\":\"/items/0/entities/1\"}]", var patcher = new JsonPatcher(); patcher.Patch(ref scene1, patchDoc); Assert.True(JToken.DeepEquals(scene1, scene2)); i++; } }
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 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 }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = new JsonPointer("/books/2").Find(sample); Assert.IsInstanceOf(typeof(JObject), result); }
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 }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); Assert.Throws(typeof(ArgumentException), () => { var x = pointer.Find("/books/1"); }); }
public void Add_a_non_existing_member_property_with_slash_character() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/b~1c"); patchDocument.AddOperation(new AddOperation() { Path = pointer, Value = new JValue("42") }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)pointer.Find(sample); Assert.AreEqual("42", result); }
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") }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)pointer.Find(sample); Assert.Equal("213324234343", result); }
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 }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = (string)topointer.Find(sample); Assert.AreEqual("F. Scott Fitzgerald", result); }
public void Move_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/1"); var topointer = new JsonPointer("/books/0/child"); patchDocument.AddOperation(new MoveOperation() { FromPath = frompointer, Path = topointer }); var patcher = new JsonPatcher(); patcher.Patch(ref sample, patchDocument); var result = topointer.Find(sample); 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 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 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); }
protected async Task <long> PatchAllAsync <TQuery>(TQuery query, object update, bool sendNotifications = true, Action <IEnumerable <string> > updatedIdsCallback = null) where TQuery : IPagableQuery, ISelectedFieldsQuery, IRepositoryQuery { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (update == null) { throw new ArgumentNullException(nameof(update)); } long affectedRecords = 0; var patch = update as PatchDocument; if (patch != null) { var patcher = new JsonPatcher(); affectedRecords += await BatchProcessAsAsync <TQuery, JObject>(query, async results => { var bulkResult = await _client.BulkAsync(b => { foreach (var h in results.Hits) { var target = h.Document as JToken; patcher.Patch(ref target, patch); b.Index <JObject>(i => i .Document(target as JObject) .Id(h.Id) .Index(h.GetIndex()) .Type(h.GetIndexType()) .Version(h.Version.HasValue ? h.Version.ToString() : null)); } return(b); }).AnyContext(); _logger.Trace(() => bulkResult.GetRequest()); if (!bulkResult.IsValid) { _logger.Error() .Exception(bulkResult.ConnectionStatus.OriginalException) .Message($"Error occurred while bulk updating: {bulkResult.GetErrorMessage()}") .Property("Query", query) .Property("Update", update) .Write(); return(false); } var updatedIds = results.Hits.Select(h => h.Id).ToList(); if (IsCacheEnabled) { await Cache.RemoveAllAsync(updatedIds).AnyContext(); } try { updatedIdsCallback?.Invoke(updatedIds); } catch (Exception ex) { _logger.Error(ex, "Error calling updated ids callback."); } return(true); }).AnyContext(); } else { if (!query.SelectedFields.Contains("id")) { query.SelectedFields.Add("id"); } string script = update as string; affectedRecords += await BatchProcessAsync(query, async results => { var bulkResult = await _client.BulkAsync(b => { foreach (var h in results.Hits) { if (script != null) { b.Update <T>(u => u .Id(h.Id) .Index(h.GetIndex()) .Type(h.GetIndexType()) .Script(script) .RetriesOnConflict(10)); } else { b.Update <T, object>(u => u.Id(h.Id) .Index(h.GetIndex()) .Type(h.GetIndexType()) .Doc(update)); } } return(b); }).AnyContext(); _logger.Trace(() => bulkResult.GetRequest()); if (!bulkResult.IsValid) { _logger.Error() .Exception(bulkResult.ConnectionStatus.OriginalException) .Message($"Error occurred while bulk updating: {bulkResult.GetErrorMessage()}") .Property("Query", query) .Property("Update", update) .Write(); return(false); } var updatedIds = results.Hits.Select(h => h.Id).ToList(); if (IsCacheEnabled) { await Cache.RemoveAllAsync(updatedIds).AnyContext(); } try { updatedIdsCallback?.Invoke(updatedIds); } catch (Exception ex) { _logger.Error(ex, "Error calling updated ids callback."); } return(true); }).AnyContext(); } if (affectedRecords > 0 && sendNotifications) { await SendQueryNotificationsAsync(ChangeType.Saved, query).AnyContext(); } return(affectedRecords); }