JSON 的規格書又被更新了 XD
在「The Last JSON Spec」這邊,Tim Bray 寫了這篇關於新的 RFC 8259 跟之前的差異,以及大家對於雙重標準的顧慮。
最大的差異在於,在 RFC 8259 規定了「如果 JSON 被用在非封閉的系統交換資料,必須使用 UTF-8」:
8259 contains one new sentence: “JSON text exchanged between systems that are not part of a closed ecosystem MUST be encoded using UTF-8 [RFC3629].” Given that, by 2017, an attempt to exchange JSON encoded in anything but UTF-8 would be irrational, this hardly needs saying; but its absence felt like an omission.
而關於 ECMA-404 與 RFC 8259 都定義了 JSON 的問題他也說明了,因為很多人花了很多力氣在確保這兩份文件的正確性上,所以應該不會有問題 (i.e. 衝突):
The reason 8259 exists is that the ECMAScript gang went and wrote their own extremely minimal spec, Standard ECMA-404: The JSON Data Interchange Syntax, and there was reason for concern over dueling standards. But, after a certain amount of standards-org elephant-gavotte, each of ECMA 404 and RFC 8259 normatively references the other and contains a commitment to keep them consistent in case any errors turn up. Which is a good thing, but this text has been re-examined and re-polished so many times that I doubt either side will ever revisit the territory, thank goodness.
另外他也提到了對於不同情境下可以看不同的文件。像是要了解 JSON 的話,可以看當初發明 JSON 的 Doug Crockford 所設立的網站 (在「JSON」這邊);而在交換時應該參考 I-JSON (Internet JSON,RFC 7493):
Which spec should you use? · If you want to understand JSON syntax, you still can’t beat Doug Crockford’s original formulation at JSON.org. If you want to use an RFC as foundation for a REST API or some other Internet protocol, I actually don’t recommend 8259, I recommend I-JSON, RFC 7493, which describes exactly the same syntax as all the other specs (by referencing 7159), but explicitly rules out some legal-but-dumbass things you could do that might break your protocol, for example using anything but UTF-8 or having duplicate member names in your objects.
I-JSON 是 JSON 的子集合,比較重要的:
- (MUST) 使用 UTF-8。
- (SHOULD NOT) 浮點數的部份,不得超過 IEEE 754-2008 binary64 (double precision) 的範圍。
- (SHOULD NOT) 整數的部份,不得超過 [-(2**53)+1, (2**53)-1]) 的範圍。
- (RECOMMEND) 有超過的需求使用字串表示。
- (MUST NOT) JSON object 內不得有重複的 name。
- (SHOULD NOT) 最上層的型態不得使用字串,只能使用 object 或是 array。
- (MUST NOT) 遇到先前沒有定義過的元素不得視為錯誤。(像是新版 API 內會在 object 裡增加元素)
- (RECOMMEND) 時間使用 ISO 8601 表示 (在 RFC 3339 有提到),英文字的部份全部使用大寫,一定要標上時區,而秒數的 0 一定要加上去 (也就是 00 秒)。
- (RECOMMEND) 時間長度也建議依照 RFC 3339 處理。
- (RECOMMEND) Binary 資料用 base64url 傳 (RFC 4648)。