handling infinity values in JSON

笑着哭i 提交于 2021-02-05 11:34:06

问题


I need at one place to have the support for infinity, -infinity and NaN values in my JSON. Now though JSON does have support for these values, but is there any support for specifying about them in the schema of the same JSON instance, I mean the RFC specification says that these values are not supported under "number" type. However, when I validated one such JSON instance having infinity value against a schema with the type "number", it produced no error! Can someone help me to figure out why is this happening, and also if "number" type discards these values, how can I define the type of these values inside JSON schema. Here is my schema and instance which I used to validate.

Schema:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
        "properties": {
        "lower_bound": {"type": "number"}
        }
}

JSON instance being validated:

{
    "lower_bound": Infinity
}

回答1:


JSON does not support neither infinity or NaN values (and never has, since the original json.org grammar). Neither does JSON Schema (because it is only defined for RFC8259-compliant JSON documents or structures).

This is due to interoperability reasons - otherwise each target language that does not support infinities or NaNs would need to always wrap these values.

Whether some validator accepts invalid json documents - does not really matter. They are still invalid.



来源:https://stackoverflow.com/questions/61841001/handling-infinity-values-in-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!