json-patch 了解

摘要:
什么是JSONPatch?
What is JSON Patch?

JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. When used in combination with the HTTP PATCH method, it allows partial updates for HTTP APIs in a standards compliant way.

The patch documents are themselves JSON documents.

JSON Patch is specified in RFC 6902 from the IETF.

Simple example

The original document

{
  "baz": "qux",
  "foo": "bar"
}

The patch

[
  { "op": "replace", "path": "/baz", "value": "boo" },
  { "op": "add", "path": "/hello", "value": ["world"] },
  { "op": "remove", "path": "/foo" }
]

The result

{
  "baz": "boo",
  "hello": ["world"]
}
How it works

A JSON Patch document is just a JSON file containing an array of patch operations. The patch operations supported by JSON Patch are “add”, “remove”, “replace”, “move”, “copy” and “test”. The operations are applied in order: if any of them fail then the whole patch operation should abort.

JSON Pointer

JSON Pointer (IETF RFC 6901) defines a string format for identifying a specific value within a JSON document. It is used by all operations in JSON Patch to specify the part of the document to operate on.

A JSON Pointer is a string of tokens separated by / characters, these tokens either specify keys in objects or indexes into arrays. For example, given the JSON

{
  "biscuits": [
    { "name": "Digestive" },
    { "name": "Choco Leibniz" }
  ]
}

/biscuits would point to the array of biscuits and /biscuits/1/name would point to "Choco Leibniz".

To point to the root of the document use an empty string for the pointer. The pointer / doesn’t point to the root, it points to a key of "" on the root (which is totally valid in JSON).

If you need to refer to a key with ~ or / in its name, you must escape the characters with ~0 and ~1 respectively. For example, to get "baz" from { "foo/bar~": "baz" } you’d use the pointer /foo~1bar~0.

Finally, if you need to refer to the end of an array you can use - instead of an index. For example, to refer to the end of the array of biscuits above you would use /biscuits/-. This is useful when you need to insert a value at the end of an array.

Operations

Add

{ "op": "add", "path": "/biscuits/1", "value": { "name": "Ginger Nut" } }

Adds a value to an object or inserts it into an array. In the case of an array, the value is inserted before the given index. The -character can be used instead of an index to insert at the end of an array.

Remove

{ "op": "remove", "path": "/biscuits" }

Removes a value from an object or array.

{ "op": "remove", "path": "/biscuits/0" }

Removes the first element of the array at biscuits (or just removes the “0” key if biscuits is an object)

Replace

{ "op": "replace", "path": "/biscuits/0/name", "value": "Chocolate Digestive" }

Replaces a value. Equivalent to a “remove” followed by an “add”.

Copy

{ "op": "copy", "from": "/biscuits/0", "path": "/best_biscuit" }

Copies a value from one location to another within the JSON document. Both from and path are JSON Pointers.

Move

{ "op": "move", "from": "/biscuits", "path": "/cookies" }

Moves a value from one location to the other. Both from and path are JSON Pointers.

Test

{ "op": "test", "path": "/best_biscuit/name", "value": "Choco Leibniz" }

Tests that the specified value is set in the document. If the test fails, then the patch as a whole should not apply.

Libraries

Libraries are available for a range of languages currently. You should check that the library you wish to use supports the RFC version of JSON Patch as there have been changes from the earlier draft versions and at the time of writing, not all libraries have been updated.

If we’re missing a library please let us know (see below)!

JavaScript

Python

PHP

Ruby

Perl

C

  • cJSON (JSON library in C, includes JSON Patch support in cJSON_Utils)

Java

Scala

C++

C#

  • Asp.Net Core JsonPatch (Microsoft official implementation)
  • Ramone (a framework for consuming REST services, includes a JSON Patch implementation)
  • JsonPatch (Adds JSON Patch support to ASP.NET Web API)
  • Starcounter (In-memory Application Engine, uses JSON Patch with OT for client-server sync)
  • Nancy.JsonPatch (Adds JSON Patch support to NancyFX)
  • Manatee.Json (JSON-everything, including JSON Patch)

Go

Haskell

Erlang

Elm

Test Suite

A collection of conformance tests for JSON Patch are maintained on Github:

github.com/json-patch/json-patch-tests

Tools JSON Schema

JSON Schema is a way to describe JSON data formats like JSON Patch. Supporting tools and libraries can use these schemas to provide auto-completion, validation and tooltips to help JSON file authors.

http://json.schemastore.org/json-patch

 

免责声明:文章转载自《json-patch 了解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Halcon 学习笔记--仿射变换与车牌定位(6)Leaflet 百度、高德地图瓦片坐标 偏移 纠偏下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

WebView2简单试用(二)—— 基本操作

页面跳转可以通过webview实现interface://www.cnblogs.com/tianfang/“);其他操作其他常见操作已经很好地封装在WebView2控件中。网页跳转事件WebView2常见的网页跳转事件包括:更多事件可以参考API文档:快捷键F12。快捷键Ctrl+Shift+I。右键菜单中的“检查”,这为调试提供了极大的便利。...

git 系列4(文件提交历史)

1查看文件提交历史记录。如果gitlog命令默认不使用任何参数,gitlog将提交时间中列出的所有更新,最新的更新位于顶部;每次更新都有一个SHA-1校验和、作者的姓名和电子邮件地址以及提交时间。提交说明通过在末尾缩进一段来显示——Stat显示每次更新的文件修改统计信息。您还可以给出几个搜索条件并列出符合条件的提交——提交者只显示与指定提交者相关的提交。...

Cesium深入浅出之视频投影【转】

通常,我们使用矩形,因为视频形状是方形的。据怀疑,视频标签隐藏了这段关系。如果再次显示,视频将再次移动。此处使用VideoSynchronizer。它可以使视频元素与铯的模拟时钟同步。让我们看看它的构造函数:name type description optionsObject option子属性:name type默认值description用于驱动视频的...

页面加载时自动执行(加载)js的几种方法

Js调用onload方法window.onload=function(){func1();func2();func3();}二、JQ方法1.整个页面的document全部加载完成以后执行。不幸的这种方式不仅要求页面的DOMtree全部加载完成,而且要求所有的外部图片和资源全部加载完成。更不幸的是,如果外部资源,例如图片需要很长时间来加载,那么这个js方法执行...

Nohup后台运行程序

场景:我现在需要跑脚本批量处理一些数据,但是我又不想盯着控制台看这个脚本的输出结果,想把这些输出结果记录到一个日志文件里面方案:可以使用Linux的nohup命令,把进程挂起,后台执行用法:$nohupXXXXXX.sh˃˃/runtime/deletedata.log&运行结果(这个数字是进程号):˃˃[1]13120有时候可能会报一个提示:$no...

MongoDB用户与角色管理

MongoDB默认不启用访问控制。管理员可以在配置文件授权参数中使用--auth-in restart或security来启用访问控制。(4) MongoDB在每个数据库上提供内置的DatabaseUserRoles和DatabaseAdministrationRoles。MongoDB仅为管理数据库提供所有内置角色。此角色没有用户和角色管理权限。(4.4)...