处理JSON中的无穷大值 [英] handling infinity values in JSON

查看:437
本文介绍了处理JSON中的无穷大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个地方对我的JSON中的infinity,-infinity和NaN值提供支持.现在,尽管JSON确实支持这些值,但是在同一JSON实例的模式中是否有关于它们的指定的支持,我的意思是 RFC 规范指出,在数字"类型下不支持这些值.但是,当我针对类型为"number"的模式验证具有无限值的此类JSON实例时,它没有产生任何错误!有人可以帮我弄清楚为什么会发生这种情况,如果数字"类型放弃了这些值,我该如何在JSON模式中定义这些值的类型.这是我用来验证的架构和实例.

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": "http://json-schema.org/draft-07/schema#",
    "type": "object",
        "properties": {
        "lower_bound": {"type": "number"}
        }
}

正在验证的JSON实例:

JSON instance being validated:

{
    "lower_bound": Infinity
}

推荐答案

JSON不支持无穷或NaN值(并且从不支持,因为原始的json.org语法). JSON Schema也不一样(因为它仅是为符合RFC8259的JSON文档或结构定义的.)

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).

这是由于互操作性原因-否则,每种不支持无限性或NaN的目标语言都必须始终包装这些值.

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

某些验证器是否接受无效的json文档-并不重要.它们仍然无效.

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

这篇关于处理JSON中的无穷大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆