本文整理汇总了C#中JsonPatchDocument类的典型用法代码示例。如果您正苦于以下问题:C# JsonPatchDocument类的具体用法?C# JsonPatchDocument怎么用?C# JsonPatchDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonPatchDocument类属于命名空间,在下文中一共展示了JsonPatchDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Edit
public async Task<ActionResult> Edit(int id, Expense expense)
{
try
{
var client = ExpenseTrackerHttpClient.GetClient();
// create a JSON Patch Document
JsonPatchDocument<DTO.Expense> patchDoc = new JsonPatchDocument<DTO.Expense>();
patchDoc.Replace(e => e.Amount, expense.Amount);
patchDoc.Replace(e => e.Date, expense.Date);
patchDoc.Replace(e => e.Description, expense.Description);
// serialize and PATCH
var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc);
var response = await client.PatchAsync("api/expenses/" + id,
new StringContent(serializedItemToUpdate,
System.Text.Encoding.Unicode, "application/json"));
if (response.IsSuccessStatusCode)
{
return RedirectToAction("Details", "ExpenseGroups", new { id = expense.ExpenseGroupId });
}
else
{
return Content("An error occurred");
}
}
catch
{
return Content("An error occurred");
}
}
示例2: SerializeAndReplaceNestedObjectTest
public void SerializeAndReplaceNestedObjectTest()
{
var doc = new SimpleDTOWithNestedDTO()
{
SimpleDTO = new SimpleDTO()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var newDTO = new SimpleDTO()
{
DoubleValue = 1
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleDTO", newDTO);
// serialize & deserialize
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.SimpleDTO.DoubleValue);
Assert.Equal(0, doc.SimpleDTO.IntegerValue);
Assert.Equal(null, doc.SimpleDTO.IntegerList);
}
示例3: SaveExpense
private async Task SaveExpense()
{
// partial update
var client = ExpenseTrackerHttpClient.GetClient();
// create a patch document, containing the (possible) changes to the
// expense DTO
JsonPatchDocument<DTO.Expense> patchDoc = new JsonPatchDocument<DTO.Expense>();
patchDoc.Replace(e => e.Description, Expense.Description);
// serialize & PATCH
var serializedItemToUpdate = JsonConvert.SerializeObject(patchDoc);
var response = client.PatchAsync("api/expenses/" + Expense.Id,
new StringContent(serializedItemToUpdate,
System.Text.Encoding.Unicode, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
// return to the expense list
App.RootFrame.Navigate(typeof(ExpensesView), true);
}
else
{
// error: handle, log, ...
}
}
示例4: AddToListInListInvalidPositionTooLarge
public void AddToListInListInvalidPositionTooLarge()
{
var doc = new SimpleDTOWithNestedDTO()
{
ListOfSimpleDTO = new List<SimpleDTO>()
{
new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
}
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleDTO/20/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
"The property at path '/ListOfSimpleDTO/20/IntegerList/0' could not be added.",
exception.Message);
}
示例5: Remove_InvalidPath_ThrowsJsonPatchParseException
public void Remove_InvalidPath_ThrowsJsonPatchParseException()
{
//Arrange
var patchDocument = new JsonPatchDocument<SimpleEntity>();
//Act
patchDocument.Remove("FooMissing");
}
示例6: CreateAServiceOnAkamai_UsingDefaults
public async Task CreateAServiceOnAkamai_UsingDefaults()
{
Trace.WriteLine("Looking for a CDN flavor provided by Akamai...");
var flavors = await _cdnService.ListFlavorsAsync();
var flavor = flavors.FirstOrDefault(x => x.Providers.Any(p => string.Equals(p.Name, "Akamai", StringComparison.OrdinalIgnoreCase)));
Assert.NotNull(flavor);
var akamaiFlavor = flavor.Id;
Trace.WriteLine(string.Format("Found the {0} flavor", akamaiFlavor));
Trace.WriteLine("Creating a CDN service using defaults for anything I can omit...");
var domains = new[] {new ServiceDomain("mirror.example.com")};
var origins = new[] {new ServiceOrigin("example.com")};
var serviceDefinition = new ServiceDefinition("ci-test", akamaiFlavor, domains, origins);
var serviceId = await _cdnService.CreateServiceAsync(serviceDefinition);
Trace.WriteLine(string.Format("Service was created: {0}", serviceId));
try
{
Trace.WriteLine("Waiting for the service to be deployed...");
var service = await _cdnService.WaitForServiceDeployedAsync(serviceId, progress: new Progress<bool>(x => Trace.WriteLine("...")));
Trace.WriteLine("Verifying service matches the requested definition...");
Assert.Equal("ci-test", service.Name);
Assert.Equal(serviceDefinition.FlavorId, service.FlavorId);
Assert.Equal(serviceDefinition.Origins.Count, service.Origins.Count());
Assert.Equal(serviceDefinition.Origins.First().Origin, service.Origins.First().Origin);
Assert.Equal(serviceDefinition.Domains.Count, service.Domains.Count());
Assert.Equal(serviceDefinition.Domains.First().Domain, service.Domains.First().Domain);
Trace.WriteLine("Updating the service...");
var patch = new JsonPatchDocument<ServiceDefinition>();
patch.Replace(x => x.Name, "ci-test2");
var intranetOnly = new ServiceRestriction("intranet", new[] {new ServiceRestrictionRule("intranet", "intranet.example.com")});
patch.Add(x => x.Restrictions, intranetOnly, 0);
await _cdnService.UpdateServiceAsync(serviceId, patch);
Trace.WriteLine("Waiting for the service changes to be deployed...");
service = await _cdnService.WaitForServiceDeployedAsync(serviceId, progress: new Progress<bool>(x => Trace.WriteLine("...")));
Trace.WriteLine("Verifying service matches updated definition...");
Assert.Equal("ci-test2", service.Name);
Assert.Equal(JsonConvert.SerializeObject(intranetOnly), JsonConvert.SerializeObject(service.Restrictions.First()));
Trace.WriteLine("Purging all assets on service");
await _cdnService.PurgeCachedAssetsAsync(serviceId);
}
finally
{
Trace.WriteLine("Cleaning up any test data...");
Trace.WriteLine("Removing the service...");
_cdnService.DeleteService(serviceId);
_cdnService.WaitForServiceDeleted(serviceId);
Trace.WriteLine("The service was cleaned up sucessfully.");
}
}
示例7: WHEN_Patch2_is_applied_with_multiples_changes_THEN_sum_change_to_correct_value
public void WHEN_Patch2_is_applied_with_multiples_changes_THEN_sum_change_to_correct_value()
{
var basket = JsonConvert.DeserializeObject<StockMarketOrders>(TestData.ORIGINAL_DATA);
var patch = JsonConvert.DeserializeObject<List<Operation<StockMarketOrders>>>(TestData.PATCH_2);
JsonPatchDocument<StockMarketOrders> doc = new JsonPatchDocument<StockMarketOrders>(patch);
doc.ApplyTo(basket);
var sum = basket.Select(x => x.Price).Sum();
//expected = original sum + sum(- item source price + item new price)
var expectedSum = 718 + (-78 + 0) + (-77 + 13) + (-22 + 27);
Check.That(sum).IsEqualTo(expectedSum);//=581
}
示例8: InvalidPathWithDotShouldThrowException
public void InvalidPathWithDotShouldThrowException()
{
JsonPatchDocument patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("NewInt.Test", 1);
});
Assert.Equal(
"The provided string 'NewInt.Test' is an invalid path.",
exception.Message);
}
示例9: WHEN_Patch1_is_applied_THEN_sum_change_to_correct_value
public void WHEN_Patch1_is_applied_THEN_sum_change_to_correct_value()
{
var basket = JsonConvert.DeserializeObject<StockMarketOrders>(TestData.ORIGINAL_DATA);
var patch = JsonConvert.DeserializeObject<List<Operation<StockMarketOrders>>>(TestData.PATCH_1);
JsonPatchDocument<StockMarketOrders> doc = new JsonPatchDocument<StockMarketOrders>(patch);
doc.ApplyTo(basket);
var sum = basket.Select(x => x.Price).Sum();
var expectedSum = 718 - 60 + 95;
//on patch one only one value changed from 60 to 95
Check.That(sum).IsEqualTo(expectedSum);
}
示例10: WHEN_Two_patches_are_applied_THEN_sum_change_to_correct_value
public void WHEN_Two_patches_are_applied_THEN_sum_change_to_correct_value()
{
var basket = JsonConvert.DeserializeObject<StockMarketOrders>(TestData.ORIGINAL_DATA);
var patch1 = JsonConvert.DeserializeObject<List<Operation<StockMarketOrders>>>(TestData.PATCH_1);
var patch2 = JsonConvert.DeserializeObject<List<Operation<StockMarketOrders>>>(TestData.PATCH_2);
JsonPatchDocument<StockMarketOrders> documentPatcher1 = new JsonPatchDocument<StockMarketOrders>(patch1);
JsonPatchDocument<StockMarketOrders> documentPatcher2 = new JsonPatchDocument<StockMarketOrders>(patch2);
documentPatcher1.ApplyTo(basket);
documentPatcher2.ApplyTo(basket);
var sum = basket.Select(x => x.Price).Sum();
Check.That(sum).IsEqualTo(616);
}
示例11: Replace_ValidPath_OperationAdded
public void Replace_ValidPath_OperationAdded()
{
//Arrange
var patchDocument = new JsonPatchDocument<SimpleEntity>();
//Act
patchDocument.Replace("Foo", "bar");
//Assert
Assert.AreEqual(1, patchDocument.Operations.Count);
Assert.AreEqual(JsonPatchOperationType.replace, patchDocument.Operations.Single().Operation);
}
示例12: AddToList
public void AddToList()
{
var doc = new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
JsonPatchDocument<SimpleDTO> patchDoc = new JsonPatchDocument<SimpleDTO>();
patchDoc.Add<int>(o => o.IntegerList, 4, 0);
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList);
}
示例13: CopyFromListToEndOfList
public void CopyFromListToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.IntegerList);
}
示例14: ReplaceAtEndOfList
public void ReplaceAtEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/-", 5);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 5 }, doc.IntegerList);
}
示例15: ReplaceFullListFromEnumerable
public void ReplaceFullListFromEnumerable()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}