JSON5

JSON5 (https://spec.json5.org)

https://github.com/json5/json5

nodejs讀取設定檔常使用json格式(http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf, https://tools.ietf.org/html/rfc8259), 但是json在描述時限制較多,例如在key要雙引號,字串要用雙引號不能用單引號,不能有註解等。

因此可讀性變差,JSON5主要將ECMAScript 5.1語法引入,整體看來,syntax對於熟悉javascript的使用者更友善

幾個比較大的差異

  • key不一定需要引號(根據ECMAScript 5.1 IdentifierName規則)
  • Objects may have a single trailing comma.
  • Arrays may have a single trailing comma.
  • Strings may be single quoted/支援多行/支援 \x的escape (U+0000 through U+00FF)
  • String更寬容的escape字元包含
    • JSON:All Unicode characters may be placed within the
      quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
    • JSON5:All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: the quotation mark used to begin and end the string, reverse solidus, and line terminators.
  • Number支援更廣泛的表示方式(eg. hex, + sign), inf, NaN
  • Single and multi-line comments are allowed
  • 更寬容的whitespace支援 (JSON只支援0x20, 0x09, 0x0A, 0x0D)
     
This entry was posted in nodejs. Bookmark the permalink.

Leave a Reply