问题描述
我对JSON补丁的解释有疑问( RFC 6902 ).
I have a question about the interpretation of JSON Patch (RFC 6902).
假设我有一个看起来像这样的资源:
Suppose I had a resource that looked like this:
{
"type": "assembly",
"uri": "http://example.com/campSrv/Assembly/18",
"name": "/sample",
"description": "Hello, World Application",
"created": "2013-03-27T16:15Z",
...
}
此资源的定义表明,它可以具有一个称为标签"的值,该值定义为字符串数组.但是,此资源当前没有标签,因此我的服务不会序列化不存在的数组.
The definition of this resource says that it could have a value called "tags" which is defined as being an array of strings. However, this resource currently has no tags, so my service doesn't serialize the non-existent array.
现在假设我提交了以下HTTP PATCH请求:
Now suppose I submit the following HTTP PATCH request:
PATCH http://example.com/campSrv/Assembly/18 HTTP/1.1
Content-Type: application/json-patch
[
{ "op": "add", "path": "/tags/0", "value": "flobbit" }
]
这应该创建'tags'数组并添加'flobbit'作为第一个/唯一元素,还是我的服务器应该返回错误?
Should this create the 'tags' array and add 'flobbit' as the first/only element or should my server return an error?
推荐答案
据我了解...
您的补丁程序将导致错误,因为tags
不存在,并且您无法将其添加到尚不存在的阵列中.
Your patch will result in an error because tags
does not exist, and you can't add to an array that does not yet exist.
...
但是,对象本身或包含对象的数组确实需要
存在,但事实并非如此,这仍然是一个错误.对于
例如,目标位置为"/a/b"的添加"以此开头 文件:
However, the object itself or an array containing it does need to
exist, and it remains an error for that not to be the case. For
example, an "add" with a target location of "/a/b" starting with this document:
{"a":{"foo":1}}
{ "a": { "foo": 1 } }
并非错误,因为存在"a",并且将"b"添加到其
价值.该文档中存在错误:
is not an error, because "a" exists, and "b" will be added to its
value. It is an error in this document:
{"q":{"bar":2}}
{ "q": { "bar": 2 } }
因为"a"不存在.
但是,此修补程序将添加值为"flobbit"
的tags
字符串.
This patch, however, will add a tags
string with a value of "flobbit"
.
[
{ "op": "add", "path": "/tags", "value": "flobbit" }
]
此补丁将添加tags
数组,第一个元素为"fobbit"
.
And this patch will add tags
array with the first element being "fobbit"
.
[
{ "op": "add", "path": "/tags", "value": ["flobbit"] }
]
这篇关于JSON补丁规范的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!