最小的有效 JSON 是多少? [英] What is the minimum valid JSON?

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

问题描述

我已经仔细阅读了 JSON 描述 http://json.org/ 但我不确定我知道答案简单的问题.哪些字符串是最小可能的有效 JSON?

  • "string" 字符串是有效的 JSON 吗?
  • 42 是简单的有效 JSON 数字吗?
  • true 布尔值是否为有效的 JSON?
  • {} 空对象是有效的 JSON 吗?
  • [] 空数组是有效的 JSON 吗?

解决方案

在撰写本文时,JSON 仅在 RFC4627.它将 JSON 文本(在2"开头)描述为序列化对象或数组.

这意味着 只有 {}[] 在符合该标准的解析器和字符串化器中是有效的、完整的 JSON 字符串.

然而,ECMA-404 的引入改变了这一点,以及更新的建议 可以在这里阅读.我也写了一篇博文 关于这个问题.


然而,为了进一步混淆问题,JSON 对象(例如 JSON.parse()JSON.stringify())在Web 浏览器是在 ES5 中标准化,并且明确定义了可接受的 JSON 文本,如下所示:><块引用>

本规范中使用的 JSON 交换格式与 RFC 4627 所描述的完全相同,但有两个例外:

  • ECMAScript JSON 语法的顶级 JSONText 生成可能包含任何 JSONValue,而不是被限制为 RFC 4627 指定的 JSONObject 或 JSONArray.

  • 剪断

这意味着 所有 JSON 值(包括字符串、空值和数字)都被 JSON 对象接受,即使 JSON 对象在技术上符合 RFC 4627.

请注意,因此您可以通过 JSON.stringify(5) 在符合标准的浏览器中对数字进行字符串化,这将被另一个符合 RFC4627 的解析器拒绝,但它没有特定的上面列出的例外.例如,Ruby,似乎就是这样的接受对象和数组作为根的示例.PHP,另一方面,专门添加了例外,即它还将编码和解码标量类型和 NULL".

I've carefully read the JSON description http://json.org/ but I'm not sure I know the answer to the simple question. What strings are the minimum possible valid JSON?

  • "string" is the string valid JSON?
  • 42 is the simple number valid JSON?
  • true is the boolean value a valid JSON?
  • {} is the empty object a valid JSON?
  • [] is the empty array a valid JSON?

解决方案

At the time of writing, JSON was solely described in RFC4627. It describes (at the start of "2") a JSON text as being a serialized object or array.

This means that only {} and [] are valid, complete JSON strings in parsers and stringifiers which adhere to that standard.

However, the introduction of ECMA-404 changes that, and the updated advice can be read here. I've also written a blog post on the issue.


To confuse the matter further however, the JSON object (e.g. JSON.parse() and JSON.stringify()) available in web browsers is standardised in ES5, and that clearly defines the acceptable JSON texts like so:

The JSON interchange format used in this specification is exactly that described by RFC 4627 with two exceptions:

  • The top level JSONText production of the ECMAScript JSON grammar may consist of any JSONValue rather than being restricted to being a JSONObject or a JSONArray as specified by RFC 4627.

  • snipped

This would mean that all JSON values (including strings, nulls and numbers) are accepted by the JSON object, even though the JSON object technically adheres to RFC 4627.

Note that you could therefore stringify a number in a conformant browser via JSON.stringify(5), which would be rejected by another parser that adheres to RFC4627, but which doesn't have the specific exception listed above. Ruby, for example, would seem to be one such example which only accepts objects and arrays as the root. PHP, on the other hand, specifically adds the exception that "it will also encode and decode scalar types and NULL".

这篇关于最小的有效 JSON 是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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