CSharpJsonPatchGenerator:建立在Microsoft.AspNetCore.JsonPatch上,通过比较

  • j8_962192
    了解作者
  • 16KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-23 03:21
    上传日期
JsonPatch生成扩展 基于Microsoft.AspNetCore.JsonPatch构建,以通过比较或观察对C#对象的更改来生成JSON补丁文档。 通过比较两个对象来生成JSON补丁 观察对象实例的更改并生成代表这些更改的JSON补丁 安装 从程序包管理器控制台: PM> Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate 或仅在包管理器UI中搜索Palit.AspNetCore.JsonPatch.Extensions.Generate 。 使用方法 如果要从C#生成JSON补丁文档,请使用此库。 我已经找不到用于此的库,并且它没有内置,所以我自己写了一个库。 它可以通过比较两个对象或观察实例的更改来生成补丁文档。 陷阱 该库目前相当简单。 它不一定会生成最佳的JSON修补程序文档。 而是生成专门包含
CSharpJsonPatchGenerator-master.zip
  • CSharpJsonPatchGenerator-master
  • Palit.AspNetCore.JsonPatch.Extensions.Generate
  • JsonPatchDocumentGenerator.cs
    5.1KB
  • JsonPatchDocumentDiffObserver.cs
    2.2KB
  • Palit.AspNetCore.JsonPatch.Extensions.Generate.csproj
    1001B
  • Palit.AspNetCore.JsonPatch.Extensions.Generate.Test
  • Comparers
  • GenericDeepEqualityComparer.cs
    3.1KB
  • TestModels
  • NestedClass.cs
    1.2KB
  • TestClass.cs
    411B
  • InheritedClass.cs
    251B
  • JsonPatchDocumentGeneratorTests.cs
    7.3KB
  • JsonPatchDocumentDiffObserverTests.cs
    2KB
  • Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.csproj
    801B
  • .gitattributes
    2.5KB
  • README.md
    3KB
  • Palit.AspNetCore.JsonPatch.Extensions.Generate.sln
    1.8KB
  • .gitignore
    4.2KB
  • LICENSE
    1KB
内容介绍
# JsonPatch Generate Extension Builds on Microsoft.AspNetCore.JsonPatch to generate JSON patch documents by comparing or observing changes to C# objects. - Generate JSON patch by comparing two objects - Watch an instance of an object for changes and generate a JSON patch representing those changes ## Installation From the package manager console: PM> Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate or by simply searching for `Palit.AspNetCore.JsonPatch.Extensions.Generate` in the package manager UI. ## How it's used Use this library if you want to generate JSON patch documents from C#. I couldn't find a library for this already and it is not built in so I wrote one myself. It can generate the patch document by comparing two objects or by watching changes to an instance. ## Gotchas This library is currently fairly simple. It will not necessarily generate optimal JSON patch documents. Instead, it generates patch documents containing Add, Remove and Replace commands exclusively. In the future it would be nice to work in all the different operations in a more optimal way. Internally, this library relies on JSON.net serializer pretty heavily. You can pass in custom serializer settings, but definitely do thorough testing with your unique circumstances. The current tests are pretty simple and do not cover complex situations (like deep nesting). ## How-To generate a JSON patch by comparison ```csharp var original = new TestClass() { Id = "id", Message = "message", DecimalValue = 1.43m, GuidValue = Guid.Empty, IntList = new List<int>() { 1, 2, 3 } }; var modified = new TestClass() { Id = "new-id", Message = null, DecimalValue = 1.68m, GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), IntList = new List<int>() { 1, 3, 2 } }; var generator = new JsonPatchDocumentGenerator(); var patch = generator.Generate(original, modified); // Modify original with patch. patch.ApplyTo(original); Assert.NotNull(patch); Assert.Equal(5, patch.Operations.Count); Assert.Equal(original, modified, new GenericDeepEqualityComparer<TestClass>()); ``` Alternatively, you can pass in your own custom JsonSerializer like so: ```csharp var jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore }; var patch = generator.Generate(original, modified, jsonSerializer); ``` ## How to generate a patch document with an observer ```csharp var instance = new TestClass { Id = "id", Message = "message", GuidValue = Guid.Empty, DecimalValue = 1.23m, IntList = new List<int> { 1, 2, 3 } }; var observer = new JsonPatchDocumentDiffObserver<TestClass>(instance); instance.Id = "new-id"; instance.Message = "new-message"; instance.GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"); instance.DecimalValue = 1.89m; instance.IntList = new List<int> { 3, 2, 1 }; // Point in time snapshot of the changes. var patch = observer.Generate(); Assert.NotNull(patch); Assert.Equal(5, patch.Operations.Count); ``` License ---- MIT
评论
    相关推荐
    • json
      使用NodeJS和Microsoft Bot Framework的#codepunk实验机器人 该机器人解析了codepunk.io网站的RSS feed,然后将回答以下问题: “什么是新的?” “最近的博客文章是什么?” “最后一个播客是什么?” 它以链接,...
    • json
      json
    • Newtonsoft_Json
      使用方法在我博客里有说 Json的序列和反序列化
    • JSONClass代码
      使用宏来描述类的结构,简化开发流程,后面完善为JSON生成
    • JSON序列化
      JSON序列化
    • CoreHal.Json.Microsoft:扩展CoreHal以使用Microsoft的System.Text.Json库创建
      微软微软 扩展CoreHal以使用Microsoft的System.Text.Json库创建Json
    • SkypeChatExtractor:Microsoft提供的Json聊天记录的提取器
      SkypeChatExtractor Microsoft提供的Json聊天提取器
    • jsonc:带有注释的JSON
      Microsoft VS Code在其配置文件(例如settings.json , keybindings.json , launch.json等)中也使用此格式。 该套餐提供什么 JSONC for Go提供了将JSONC转换和解组为纯JSON的功能。 它还提供了从磁盘读取JSONC...
    • Json Read tool
      C#读取json格式,并写成提取csv文件, 支持的格式如下: { "type": "String", "compoperator": "LOG", "expectedvalue": "", "value": "3", "name": "L002", "description": "Log Hardware ID", "result": ...
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档