英文:
Null allowed for JSON attribute of type object?
问题
If I have a JSON schema which defines an optional property of type object
is it allowed to have null
as a value? Would null
be logically equivalent to the empty object {}
? I would like to understand if a consumer should expect to get and be able to handle a null
value in such cases...
我有一个JSON模式,定义了一个可选属性的类型为object
,是否允许将null
作为值?null
是否在逻辑上等同于空对象{}
?我想了解在这种情况下,消费者是否应该预期并能够处理null
值...
I also saw the following schema but now I'm confused what's best practice:
我还看到了以下模式,但现在我对最佳实践感到困惑:
{
"type": ["object", "null"],
...
}
So I guess my question comes down to is it required to have the "null" in the type definition above or would just "object" already allow to have null
as a value?
所以我猜我的问题归结为是否需要在上面的类型定义中包含"null",还是只有"object"已经允许使用null
作为值?
英文:
If I have a JSON schema which defines an optional property of type object
is it allowed to have null
as a value? Would null
be logically equivalent to the empty object {}
? I would like to understand if a consumer should expect to get and be able to handle a null
value in such cases...
I also saw the following schema but now I'm confused what's best practice:
{
"type": ["object", "null"],
...
}
So I guess my question comes down to is it required to have the "null"
in the type definition above or would just object
already allow to have null
as a value?
答案1
得分: 0
JSON值null
是一个独立的值。如果您希望接受这样的值,您需要像您所示的那样将它包含在type
中。
英文:
The JSON value null
is a value into its own. If you want to accept such a value, you'll need to include it in type
as you have shown.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论