简体   繁体   English

如何在从 .net 后端到 js 前端的 2 个大嵌套 json 之间执行 JSON 差异/补丁?

[英]how to do a JSON diff/patch between 2 big nested json from .net backend to js frontend?

i have a very big json that has a very small changes each second.我有一个非常大的 json,每秒都有非常小的变化。

im using asp.net core 3.1 and signalr core.我使用asp.net core 3.1和signalr core。 and clients are using browsers running pure js and jquery.并且客户正在使用运行纯 js 和 jquery 的浏览器。

right now for every change im re sending all the json again.现在对于每次更改,我都会再次发送所有 json。 i know that its not a good way.我知道这不是一个好方法。

what i want to do is just sending the Changes to client and then patch the changes to the main JSON in javascript.我想要做的只是将更改发送到客户端,然后在 javascript 中将更改修补到主 JSON。

by google search i understood that its a JSON diff/patch or diff/Merge operation, but i did not found anything on how to implement this.通过谷歌搜索,我知道它是一个 JSON diff/patch 或 diff/Merge 操作,但我没有找到任何关于如何实现它的信息。

so, how can i achieve a diff/pach opration in signalr ?那么,我如何才能在 signalr 中实现 diff/pach 操作?

J_m and j_m_lit maybe 100 keys. J_m 和 j_m_lit 可能有 100 个键。

the data struncture数据结构

[
   {
      "id":0,
      "stdid":3808003,
      "lid":10533,
      .
      .
      (40 root keys)
      .
      .
      "J_ps":null,
      "J_ms":null,
      "J_m":{
         "SA_H2 0":{
            "c":"SA_H2",
            "a":"0",
            "b":0,
            "r":"0",
            "w":0,
            "en":"2nd",
            "o":{
               "2:0":{
                  "c":"2:0",
                  "v":1.3,
                  "pv":0.0,
                  "b":0
               },
               "Tie":{
                  "c":"Tie",
                  "v":3.2,
                  "pv":0.0,
                  "b":0
               }
            }
         },....
       
      },
      "J_m_Lit":{
         "OE 0":{
            "c":"OE",
            "a":"0",
            "b":0,
            "r":"0",
            "w":0,
            "en":"Total ",
            "o":{
               "xxx":{
                  "c":"Odd",
                  "v":1.833,
                  "pv":0.0,
                  "b":0
               },
               "zzz":{
                  "c":"Even",
                  "v":1.833,
                  "pv":0.0,
                  "b":0
               }
            }
         },  ....

      },
    
      "J_fre":{
         "tq":[
            0,
            1
         ],
         "m1":[
            23,
            17
         ]
      },
     
   }
]

Yes, I think you are on the right track.是的,我认为你在正确的轨道上。

JSON-Patch (RFC6902) , at its core, tries to solve the very problem you facing here: partial updates. JSON-Patch (RFC6902)的核心是试图解决您在这里面临的问题:部分更新。 JSON Patch offers an atomic update request that patches your JSON using a series of "add", "remove", "replace", "move" and "copy" operations. JSON Patch 提供了一个原子更新请求,它使用一系列“添加”、“删除”、“替换”、“移动”和“复制”操作来修补您的 JSON。 To quote one of the authors of the JSON Patch RFC, Mark Nottingham (IETF):引用JSON Patch RFC 的作者之一 Mark Nottingham (IETF):

This has a bunch of advantages.这有很多优点。 Since it's a generic format, you can write server- and client-side code once, and share it among a number of applications.由于它是一种通用格式,您可以编写一次服务器端和客户端代码,并在多个应用程序之间共享它。 You can do QA once, and make sure you get it right.您可以进行一次 QA,并确保您做对了。 Furthermore, your API will become less complex, because it has less URI conventions, leading to more flexibility and making it easier to approach for new developers.此外,您的 API 将变得不那么复杂,因为它具有更少的 URI 约定,从而带来更大的灵活性并使新开发人员更容易上手。 Using this approach will also make caches operate more correctly;使用这种方法也会使缓存操作更正确; since modifications to a resource will “travel” through its URL, rather than some other one, the right stored responses will get invalidated.由于对资源的修改将通过其 URL 而不是其他 URL 进行“传输”,因此正确存储的响应将失效。

ASP.NET Core offers JSON-Patch support. ASP.NET Core 提供 JSON-Patch 支持。 JSON Patch can be added by installing Microsoft JSON Patch library ( Nuget ) from the Package Manager console:可以通过从包管理器控制台安装 Microsoft JSON 补丁库 ( Nuget ) 添加 JSON 补丁:

Install-Package Microsoft.AspNetCore.JsonPatch

Then you apply patches using the JsonPatchDocument class and its methods like ApplyTo , Add , Copy , Move , Remove , Replace .然后使用JsonPatchDocument类及其方法应用补丁,如ApplyToAddCopyMoveRemoveReplace

In an ASP.NET Core Web API context, we can also make use of the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.在 ASP.NET Core Web API 上下文中,我们还可以使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet包。

And then use the PUT and PATCH methods to update an existing resource with JSON Patch operations.然后使用 PUT 和 PATCH 方法通过 JSON Patch 操作更新现有资源。

This MSDN article has some good explanations and a sample project. 这篇 MSDN文章有一些很好的解释和一个示例项目。


In a JavaScript/Node.js project, you could also choose from a larger list of JS implementations, notably fast-json-patch .在 JavaScript/Node.js 项目中,您还可以从更大的 JS 实现列表中进行选择,尤其是fast-json-patch

Resources:资源:


Side note: Another option is JSON Merge Patch (RFC7396) , which can be used to send partial updates as well but works more like an actual diff/patch that simply contains the changes instead of using mutating operations.旁注:另一个选项是JSON Merge Patch (RFC7396) ,它也可用于发送部分更新,但更像是一个实际的差异/补丁,它只包含更改而不是使用变异操作。 In this respect, JSON Merge Patch is simpler but more limited than JSON Patch, eg you cannot set a key to null (since null means remove), merging only works with objects {..} , arrays [..] can only be replaced as a whole, and merging never fails, could cause side-effects and is therefore not transactional.在这方面,JSON Merge Patch 比 JSON Patch 更简单但更受限制,例如您不能将键设置为null (因为 null 表示删除),合并仅适用于对象{..} ,数组[..]只能被替换作为一个整体,合并永远不会失败,可能会导致副作用,因此不是事务性的。 This renders Merge Patch (overly) simplistic and impractical for certain real-world applications.这使得合并补丁(过度)对于某些实际应用程序来说过于简单和不切实际。 There are more related projects named like JSON-Diffpatch, etc that take similar approaches.还有更多相关的项目,如 JSON-Diffpatch 等,它们采用了类似的方法。 Arguably, JSON-Patch is more flexible but both have advantages and disadvantages.可以说,JSON-Patch 更灵活,但两者都有优点和缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM