JSON属性的类型为对象,允许为空吗?

huangapple go评论49阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 21:52:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定