如果未定义数组或对象,JSON 补丁应该失败吗?
Should JSON patch fail when an array or object isn't defined?
给定 JSON 对象
{
"property": "value"
}
如果像下面这样对该对象执行JSON补丁
[{
"op": "add",
"path": "/otherProperty/property",
"value": "childvalue"
}]
JSON 补丁操作应该因为 otherProperty
未定义而失败,还是应该添加整个路径?
我在这里找不到任何信息。
如评论中所述,JSON 补丁 Internet 草稿声明该操作应导致错误:
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 } }
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 } }
because "a" does not exist.
也就是说您仍然可以做您想做的事,但是您必须通过添加一个包含您想要的 属性 的对象来更改语法。所以根据那个草案的 Appendix 10 你可以做
[{
"op": "add",
"path": "/otherProperty",
"value": { "property" : "childvalue" }
}]
在这种情况下,您将在根级别创建一个字段,该字段的正文是 json 对象:
{
"property": "value",
"otherProperty" : {
"property" : "childvalue"
}
}
我通过在目标资源的 JSON 之前和之后粘贴来测试这个 here,它生成了与我上面介绍的相同的添加语句。
给定 JSON 对象
{
"property": "value"
}
如果像下面这样对该对象执行JSON补丁
[{
"op": "add",
"path": "/otherProperty/property",
"value": "childvalue"
}]
JSON 补丁操作应该因为 otherProperty
未定义而失败,还是应该添加整个路径?
我在这里找不到任何信息。
如评论中所述,JSON 补丁 Internet 草稿声明该操作应导致错误:
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 } }
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 } }
because "a" does not exist.
也就是说您仍然可以做您想做的事,但是您必须通过添加一个包含您想要的 属性 的对象来更改语法。所以根据那个草案的 Appendix 10 你可以做
[{
"op": "add",
"path": "/otherProperty",
"value": { "property" : "childvalue" }
}]
在这种情况下,您将在根级别创建一个字段,该字段的正文是 json 对象:
{
"property": "value",
"otherProperty" : {
"property" : "childvalue"
}
}
我通过在目标资源的 JSON 之前和之后粘贴来测试这个 here,它生成了与我上面介绍的相同的添加语句。