本文整理汇总了C#中JsonPatchDocument.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# JsonPatchDocument.Replace方法的具体用法?C# JsonPatchDocument.Replace怎么用?C# JsonPatchDocument.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonPatchDocument
的用法示例。
在下文中一共展示了JsonPatchDocument.Replace方法的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: 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.");
}
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}
示例7: ReplacePropertyInNestedObject
public void ReplacePropertyInNestedObject()
{
var doc = new SimpleDTOWithNestedDTO()
{
IntegerValue = 1
};
// create patch
JsonPatchDocument<SimpleDTOWithNestedDTO> patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<string>(o => o.NestedDTO.StringProperty, "B");
patchDoc.ApplyTo(doc);
Assert.Equal("B", doc.NestedDTO.StringProperty);
}
示例8: ExampleDoc
public async static void ExampleDoc()
{
// Authenticate
var identity = new CloudIdentity
{
APIKey = "{apikey}",
Username = "{username}"
};
var authProvider = new CloudIdentityProvider(identity);
var cdnService = new ContentDeliveryNetworkService(authProvider, "{region}");
// List Flavors
IEnumerable<Flavor> flavors = await cdnService.ListFlavorsAsync();
// Get Flavor
Flavor flavor = await cdnService.GetFlavorAsync("{flavorId}");
// Create Service
ServiceDefinition serviceDefinition = new ServiceDefinition("example_site", "{flavorId}",
domains: new[] {new ServiceDomain("www.example.com")},
origins: new[] {new ServiceOrigin("example.com")});
string serviceId = await cdnService.CreateServiceAsync(serviceDefinition);
await cdnService.WaitForServiceDeployedAsync(serviceId);
// List Services
IPage<Service> services = await cdnService.ListServicesAsync();
// Get Service
Service service = await cdnService.GetServiceAsync("{serviceId}");
// Purge All Service Assets
await cdnService.PurgeCachedAssetsAsync("{serviceId}");
// Purge a Specific Service Asset
await cdnService.PurgeCachedAssetAsync("{serviceId}", "{relativeUrlOfAsset}");
// Update Service
var patch = new JsonPatchDocument<ServiceDefinition>();
patch.Replace(svc => svc.Name, "newServiceName");
await cdnService.UpdateServiceAsync("{serviceId}", patch);
await cdnService.WaitForServiceDeployedAsync("{serviceId}");
// Delete Service
await cdnService.DeleteServiceAsync("{serviceId}");
await cdnService.WaitForServiceDeletedAsync("{serviceId}");
}
示例9: ReplaceInList
public void ReplaceInList()
{
var doc = new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/0", 5);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 2, 3 }, doc.IntegerList);
}
示例10: ReplaceGuidTestExpandoObject
public void ReplaceGuidTestExpandoObject()
{
dynamic doc = new ExpandoObject();
doc.GuidValue = Guid.NewGuid();
var newGuid = Guid.NewGuid();
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
patchDoc.Replace("GuidValue", newGuid);
// serialize & deserialize
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserizalized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserizalized.ApplyTo(doc);
Assert.Equal(newGuid, doc.GuidValue);
}
示例11: ReplaceNestedObject
public void ReplaceNestedObject()
{
// Arrange
var doc = new SimpleDTOWithNestedDTO()
{
IntegerValue = 1
};
var newNested = new NestedDTO() { StringProperty = "B" };
// create patch
var patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<NestedDTO>(o => o.NestedDTO, newNested);
// Act
patchDoc.ApplyTo(doc);
// Assert
Assert.Equal("B", doc.NestedDTO.StringProperty);
}
示例12: ReplacePropertyInNestedObjectWithSerialization
public void ReplacePropertyInNestedObjectWithSerialization()
{
// Arrange
var doc = new SimpleDTOWithNestedDTO()
{
IntegerValue = 1
};
// create patch
var patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<string>(o => o.NestedDTO.StringProperty, "B");
// serialize & deserialize
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
// Act
deserialized.ApplyTo(doc);
// Assert
Assert.Equal("B", doc.NestedDTO.StringProperty);
}
示例13: ReplaceInListInvalidInvalidPositionTooLargeWithSerialization
public void ReplaceInListInvalidInvalidPositionTooLargeWithSerialization()
{
var doc = new SimpleDTOWithNestedDTO()
{
SimpleDTO = new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
JsonPatchDocument<SimpleDTOWithNestedDTO> patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, 3);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
Assert.Throws<JsonPatchException>(() => { deserialized.ApplyTo(doc); });
}
示例14: ReplaceAtEndOfListWithSerialization
public void ReplaceAtEndOfListWithSerialization()
{
var doc = new SimpleDTOWithNestedDTO()
{
SimpleDTO = new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
JsonPatchDocument<SimpleDTOWithNestedDTO> patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleDTOWithNestedDTO>>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 5 }, doc.SimpleDTO.IntegerList);
}
示例15: ReplaceInListInvalidInvalidPositionTooLarge
public void ReplaceInListInvalidInvalidPositionTooLarge()
{
var doc = new SimpleDTOWithNestedDTO()
{
SimpleDTO = new SimpleDTO()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
JsonPatchDocument<SimpleDTOWithNestedDTO> patchDoc = new JsonPatchDocument<SimpleDTOWithNestedDTO>();
patchDoc.Replace<int>(o => o.SimpleDTO.IntegerList, 5, 3);
Assert.Throws<JsonPatchException>(() => { patchDoc.ApplyTo(doc); });
}