• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# JsonPatchDocument类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中JsonPatchDocument的典型用法代码示例。如果您正苦于以下问题:C# JsonPatchDocument类的具体用法?C# JsonPatchDocument怎么用?C# JsonPatchDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



JsonPatchDocument类属于命名空间,在下文中一共展示了JsonPatchDocument类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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");
            }
        }
开发者ID:sgccarey,项目名称:ExpensesTracker,代码行数:35,代码来源:ExpensesController.cs


示例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);
        }
开发者ID:leloulight,项目名称:JsonPatch,代码行数:30,代码来源:ReplaceTypedOperationTests.cs


示例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, ...
            }
            
        }
开发者ID:Raiegh,项目名称:ExpenseTracker,代码行数:32,代码来源:EditExpenseViewModel.cs


示例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);
        }
开发者ID:aspnet,项目名称:JsonPatch,代码行数:27,代码来源:AddTypedOperationTests.cs


示例5: Remove_InvalidPath_ThrowsJsonPatchParseException

        public void Remove_InvalidPath_ThrowsJsonPatchParseException()
        {
            //Arrange
            var patchDocument = new JsonPatchDocument<SimpleEntity>();

            //Act
            patchDocument.Remove("FooMissing");
        }
开发者ID:bradodarb,项目名称:JsonPatch,代码行数:8,代码来源:JsonPatchDocumentTests.cs


示例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.");
            }
        }
开发者ID:ravikamachi,项目名称:openstack.net,代码行数:58,代码来源:ServiceTests.cs


示例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
 }
开发者ID:rhwy,项目名称:streamdataio.Net,代码行数:11,代码来源:EnsureJsonPatchWorksWithTheModel.cs


示例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);
 }
开发者ID:aspnet,项目名称:JsonPatch,代码行数:11,代码来源:PatchDocumentTests.cs


示例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);
 }
开发者ID:rhwy,项目名称:streamdataio.Net,代码行数:11,代码来源:EnsureJsonPatchWorksWithTheModel.cs


示例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);
 }
开发者ID:rhwy,项目名称:streamdataio.Net,代码行数:12,代码来源:EnsureJsonPatchWorksWithTheModel.cs


示例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);
        }
开发者ID:bradodarb,项目名称:JsonPatch,代码行数:12,代码来源:JsonPatchDocumentTests.cs


示例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);
        }
开发者ID:Drakraq,项目名称:JsonPatch,代码行数:15,代码来源:ObjectAdapterTests.cs


示例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);
        }
开发者ID:leloulight,项目名称:JsonPatch,代码行数:15,代码来源:CopyOperationTests.cs


示例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);
        }
开发者ID:aspnet,项目名称:JsonPatch,代码行数:15,代码来源:ReplaceOperationTests.cs


示例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);
        }
开发者ID:aspnet,项目名称:JsonPatch,代码行数:15,代码来源:ReplaceOperationTests.cs


示例16: Copy

        public void Copy()
        {
            dynamic doc = new ExpandoObject();

            doc.StringProperty = "A";
            doc.AnotherStringProperty = "B";

            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Copy("StringProperty", "AnotherStringProperty");

            var serialized = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
            deserialized.ApplyTo(doc);

            Assert.Equal("A", doc.AnotherStringProperty);
        }
开发者ID:leloulight,项目名称:JsonPatch,代码行数:16,代码来源:CopyOperationTests.cs


示例17: AddNewProperty

        public void AddNewProperty()
        {
            dynamic obj = new ExpandoObject();
            obj.Test = 1;

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Add("NewInt", 1);

            var serialized = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);

            deserialized.ApplyTo(obj);

            Assert.Equal(1, obj.NewInt);
            Assert.Equal(1, obj.Test);
        }
开发者ID:aspnet,项目名称:JsonPatch,代码行数:17,代码来源:AddOperationTests.cs


示例18: MoveFromListToEndOfList

        public void MoveFromListToEndOfList()
        {
            var doc = new SimpleDTO()
            {
                IntegerList = new List<int>() { 1, 2, 3 }
            };

            // create patch
            JsonPatchDocument patchDoc = new JsonPatchDocument();
            patchDoc.Move("IntegerList/0", "IntegerList/-");

            var serialized = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
            deserialized.ApplyTo(doc);

            Assert.Equal(new List<int>() { 2, 3, 1 }, doc.IntegerList);
        }
开发者ID:leloulight,项目名称:JsonPatch,代码行数:17,代码来源:MoveTypedOperationTests.cs


示例19: 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}");
        }
开发者ID:openstacknetsdk,项目名称:Demos,代码行数:46,代码来源:Example.cs


示例20: 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);

        }
开发者ID:Drakraq,项目名称:JsonPatch,代码行数:17,代码来源:ObjectAdapterTestsOnNested.cs



注:本文中的JsonPatchDocument类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# JsonPropertyCollection类代码示例发布时间:2022-05-24
下一篇:
C# JsonObject类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap