json-patch 了解
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
- zjsonpatch
- json-patch
- bsonpatch (port of zjsonpatch that uses BSON as document model)
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 了解的更多相关文章
- 【ASP.NET Core】JSON Patch 使用简述
JSON Patch 是啥玩意儿?不知道,直接翻译吧,就叫它“Json 补丁”吧.干吗用的呢?当然是用来修改 JSON 文档的了.那咋修改呢?比较常见有四大操作:AMRR. 咋解释呢? A—— Add ...
- JSON Patch
1.前言 可以这么说的是,任何一种非强制性约束同时也没有"标杆"工具支持的开发风格或协议(仅靠文档是远远不够的),最终的实现上都会被程序员冠上"务实"的名头,而 ...
- 如何在ASP.NET Core中使用JSON Patch
原文: JSON Patch With ASP.NET Core 作者:.NET Core Tutorials 译文:如何在ASP.NET Core中使用JSON Patch 地址:https://w ...
- [译] 在Web API 2 中实现带JSON的Patch请求
原文链接:The Patch Verb in Web API 2 with JSON 我想在.NET4.6 Web API 2 项目中使用Patch更新一个大对象中的某个字断,这才意识到我以前都没有用 ...
- 用ASP.NET Core 2.0 建立规范的 REST API -- DELETE, UPDATE, PATCH 和 Log
本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...
- streamsets http client && json parse && local fs 使用
streamsets 包含了丰富的组件,origin processer destination 测试例子为集成了http client 以及json 处理 启动服务 使用docker 创建pipel ...
- kubectl 之 patch 命令
patch命令 kubectl patch — Update field(s) of a resource using strategic merge patch Synopsis kubectl p ...
- Kubernetes官方java客户端之七:patch操作
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Json文件解析(下)
Json文件解析(下) 代码地址:https://github.com/nlohmann/json 从STL容器转换 任何序列容器(std::array,std::vector,std::dequ ...
- javaScript系列 [09]-javaScript和JSON (拓展)
本文输出JSON搜索和JSON转换相关的内容,是对前两篇文章的补充. JSON搜索 在特定的开发场景中,如果服务器端返回的JSON数据异常复杂(可能超过上万行),那么必然就有对JSON文档进行搜索的需 ...
随机推荐
- 前端优化点(此文转载 http://mp.weixin.qq.com/s/6mVVKmqDL_xYl15AeoJTWg)
此文转载自:http://mp.weixin.qq.com/s/6mVVKmqDL_xYl15AeoJTWg (原文地址) 围绕前端的性能多如牛毛,涉及到方方面面,以下我们将围绕PC浏览器和移动端浏览 ...
- C#——图片操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- HBuilder 获取通讯录
代码: var content=""; function getCallLog() { try{ plus.contacts.getAddressBook(plus.contact ...
- POJ 3268 Silver Cow Party(最短路&Dijkstra)题解
题意:有n个地点,有m条路,问从所有点走到指定点x再走回去的最短路中的最长路径 思路:用Floyd超时的,这里用的Dijkstra. Dijkstra感觉和Prim和Kruskal的思路很像啊.我们把 ...
- POJ - 1463 Strategic game (树状动态规划)
这题做的心塞... 整个思路非常清晰,d[i][0]表示第i个结点不设置监察的情况下至少需要的数量:d[i][1]表示第i个结点设置检查的情况下的最小需要的数量. 状态转移方程见代码. 但是万万没想到 ...
- Spring 中好用的泛型操作API
随着泛型用的越来越多,获取泛型实际类型信息的需求也会出现,如果用原生API,需要很多步操作才能获取到泛型,比如: ParameterizedType parameterizedType = (Para ...
- 【联网】虚拟机下Linux(终端)配置网络的方法
这几天在虚拟机vmware上部署centos系统,想通过内部联网用yum命令安装必需的软件,但是一直不能静态地址联网,今天终于找到一个方法centos内部设置IP,对外联网.设置过程如下: 1.首先是 ...
- python 获取列表的键值对
nums = [, , , , ] for num_index, num_val in enumerate(nums): print(num_index, num_val)
- Java Spring-注解进行属性注入
2017-11-06 21:19:43 一.Spring的注解装配BeanSpring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供 ...
- MQ是什么 RabbitMQ
一.rabbitMQ是什么: RabbitMQ,遵循AMQP协议,由内在高并发的erlanng语言开发,用在实时的对可靠性要求比较高的消息传递上. 学过websocket的来理解rabbitMQ应该是 ...