JSON 是否“官方”?支持原生类型转换吗?

发布于 2024-10-14 09:38:29 字数 837 浏览 6 评论 0原文

在 PHP 中,您可以使用 json_encode 将对象编码为 json 字符串。

$string = json_encode($some_object);

然而,PHP 有一系列标准的数据类型,这些数据类型不被视为对象(整数、字符串等)。如果您将字符串传递给 json_encode,这将返回一个包含 JavaScript 语句的字符串,该语句可以是用于定义字符串。

用不太尴尬的措辞来说,这

echo json_encode("Hello 
world, please don't " . '"' . "misuse quote's for emphasis " .  "or possessive apostrophes' ");

将输出 this (一个 javascript 就绪字符串)

"Hello \n\tworld, please don't \"misuse quote'sor possessive apostrophes' "

这种行为是 JSON 规范的一部分吗?也就是说,JSON 是否定义或建议实现应如何处理本机、非对象、数据类型的转换?或者甚至对转换有什么看法?我对 RFC 的阅读 使这一点含糊不清,但我很糟糕在解释这些事情时。

我问这个问题是因为我对这种行为从该函数的未来版本中消失的可能性感兴趣。也就是说,如果它被编入规范中的某个地方,那么它就不太可能消失,而如果它是有人在开发过程中考虑添加的一次性内容,那么它就不太可能消失。

In PHP, you can use json_encode to encode an object as a json string.

$string = json_encode($some_object);

However, PHP has the standard slew of datatypes which are not considered objects (ints, strings, etc.) If you pass in a string to json_encode, this returns a string that contains a javascript statement that could be used to define the string.

In less awkward phrasing, this

echo json_encode("Hello 
world, please don't " . '"' . "misuse quote's for emphasis " .  "or possessive apostrophes' ");

will output this (a javascript ready string)

"Hello \n\tworld, please don't \"misuse quote'sor possessive apostrophes' "

Is this behavior part of the JSON specification? That is, does JSON define or recommend how an implementation should handle conversion of native, non-object, datatypes? Or even have an opinion on conversion at all? My reading of the RFC left this as ambiguous, but I'm crap at interpreting these things.

I ask because I'm interested in the likelihood of this behavior disappearing from a future version of the function. i.e. If it's codified in a specification somewhere, it's less likely to disappear than if it was a one off someone thought to add on during development.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-10-21 09:38:29

JSON 根本不关心本机类型。如何将 JSON 转换为编程语言可以使用/理解的类型以及如何将 JSON 转换为编程语言可以使用/理解的类型,取决于 JSON 库或功能的开发人员。

JSON doesn't care about native types at all. It is up to the developer of the JSON library or functionality as to how the JSON is translated to and from types that the programming language can use/understand.

成熟稳重的好男人 2024-10-21 09:38:29

你是对的艾伦,RFC 在这个问题上还不够清楚。

一方面,在您提供的 RFC 链接上,它在简介的第二段中写道:

JSON 可以表示四种基本类型(字符串、数字、布尔值和 null)和两种结构化类型(对象和数组)。

但另一方面,当您继续讨论实际 JSON 语法的段落时,它会说:

JSON 文本是序列化的对象或数组。
JSON-文本=对象/数组

因此,根据语法,您可以说 'a string' 不符合有效的 JSON 文本。

就我个人而言,我更愿意看到语法“固定”
JSON-text = value 从而使任何 false / null / true / object / array / number / string 成为有效的 JSON 文本。

如果你现在想严格一点,我会选择语法的语义。

我将向 Douglas Crockford 发送此问题的链接,也许他可以添加一些有用的信息。

You are right alan, the RFC is not clear enough on this issue.

On one hand, on the link to the RFC that you provide it says on the second paragraph of the introduction:

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

But on the other hand when you continue to the paragraph That talks about the actual JSON grammar it says:

A JSON text is a serialized object or array.
JSON-text = object / array

So based on the grammar you can say that 'a string' does not qualify as valid JSON-text.

Personally I would prefer to see the grammar "fixed" to say
JSON-text = value thus making any of false / null / true / object / array / number / string become valid JSON-text.

If you want to be strict for now, I would go with the semantics of the grammar.

I will send Douglas Crockford a link to this question maybe he can add some useful info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文