You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.5 KiB

4 years ago
  1. # JSON5 – JSON for Humans
  2. [![Build Status](https://travis-ci.org/json5/json5.svg)][Build Status]
  3. [![Coverage
  4. Status](https://coveralls.io/repos/github/json5/json5/badge.svg)][Coverage
  5. Status]
  6. The JSON5 Data Interchange Format (JSON5) is a superset of [JSON] that aims to
  7. alleviate some of the limitations of JSON by expanding its syntax to include
  8. some productions from [ECMAScript 5.1].
  9. This JavaScript library is the official reference implementation for JSON5
  10. parsing and serialization libraries.
  11. [Build Status]: https://travis-ci.org/json5/json5
  12. [Coverage Status]: https://coveralls.io/github/json5/json5
  13. [JSON]: https://tools.ietf.org/html/rfc7159
  14. [ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/
  15. ## Summary of Features
  16. The following ECMAScript 5.1 features, which are not supported in JSON, have
  17. been extended to JSON5.
  18. ### Objects
  19. - Object keys may be an ECMAScript 5.1 _[IdentifierName]_.
  20. - Objects may have a single trailing comma.
  21. ### Arrays
  22. - Arrays may have a single trailing comma.
  23. ### Strings
  24. - Strings may be single quoted.
  25. - Strings may span multiple lines by escaping new line characters.
  26. - Strings may include character escapes.
  27. ### Numbers
  28. - Numbers may be hexadecimal.
  29. - Numbers may have a leading or trailing decimal point.
  30. - Numbers may be [IEEE 754] positive infinity, negative infinity, and NaN.
  31. - Numbers may begin with an explicit plus sign.
  32. ### Comments
  33. - Single and multi-line comments are allowed.
  34. ### White Space
  35. - Additional white space characters are allowed.
  36. [IdentifierName]: https://www.ecma-international.org/ecma-262/5.1/#sec-7.6
  37. [IEEE 754]: http://ieeexplore.ieee.org/servlet/opac?punumber=4610933
  38. ## Short Example
  39. ```js
  40. {
  41. // comments
  42. unquoted: 'and you can quote me on that',
  43. singleQuotes: 'I can use "double quotes" here',
  44. lineBreaks: "Look, Mom! \
  45. No \\n's!",
  46. hexadecimal: 0xdecaf,
  47. leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  48. positiveSign: +1,
  49. trailingComma: 'in objects', andIn: ['arrays',],
  50. "backwardsCompatible": "with JSON",
  51. }
  52. ```
  53. ## Specification
  54. For a detailed explanation of the JSON5 format, please read the [official
  55. specification](https://json5.github.io/json5-spec/).
  56. ## Installation
  57. ### Node.js
  58. ```sh
  59. npm install json5
  60. ```
  61. ```js
  62. const JSON5 = require('json5')
  63. ```
  64. ### Browsers
  65. ```html
  66. <script src="https://unpkg.com/json5@^1.0.0"></script>
  67. ```
  68. This will create a global `JSON5` variable.
  69. ## API
  70. The JSON5 API is compatible with the [JSON API].
  71. [JSON API]:
  72. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
  73. ### JSON5.parse()
  74. Parses a JSON5 string, constructing the JavaScript value or object described by
  75. the string. An optional reviver function can be provided to perform a
  76. transformation on the resulting object before it is returned.
  77. #### Syntax
  78. JSON5.parse(text[, reviver])
  79. #### Parameters
  80. - `text`: The string to parse as JSON5.
  81. - `reviver`: If a function, this prescribes how the value originally produced by
  82. parsing is transformed, before being returned.
  83. #### Return value
  84. The object corresponding to the given JSON5 text.
  85. ### JSON5.stringify()
  86. Converts a JavaScript value to a JSON5 string, optionally replacing values if a
  87. replacer function is specified, or optionally including only the specified
  88. properties if a replacer array is specified.
  89. #### Syntax
  90. JSON5.stringify(value[, replacer[, space]])
  91. JSON5.stringify(value[, options])
  92. #### Parameters
  93. - `value`: The value to convert to a JSON5 string.
  94. - `replacer`: A function that alters the behavior of the stringification
  95. process, or an array of String and Number objects that serve as a whitelist
  96. for selecting/filtering the properties of the value object to be included in
  97. the JSON5 string. If this value is null or not provided, all properties of the
  98. object are included in the resulting JSON5 string.
  99. - `space`: A String or Number object that's used to insert white space into the
  100. output JSON5 string for readability purposes. If this is a Number, it
  101. indicates the number of space characters to use as white space; this number is
  102. capped at 10 (if it is greater, the value is just 10). Values less than 1
  103. indicate that no space should be used. If this is a String, the string (or the
  104. first 10 characters of the string, if it's longer than that) is used as white
  105. space. If this parameter is not provided (or is null), no white space is used.
  106. If white space is used, trailing commas will be used in objects and arrays.
  107. - `options`: An object with the following properties:
  108. - `replacer`: Same as the `replacer` parameter.
  109. - `space`: Same as the `space` parameter.
  110. - `quote`: A String representing the quote character to use when serializing
  111. strings.
  112. #### Return value
  113. A JSON5 string representing the value.
  114. ### Node.js `require()` JSON5 files
  115. When using Node.js, you can `require()` JSON5 files by adding the following
  116. statement.
  117. ```js
  118. require('json5/lib/register')
  119. ```
  120. Then you can load a JSON5 file with a Node.js `require()` statement. For
  121. example:
  122. ```js
  123. const config = require('./config.json5')
  124. ```
  125. ## CLI
  126. Since JSON is more widely used than JSON5, this package includes a CLI for
  127. converting JSON5 to JSON and for validating the syntax of JSON5 documents.
  128. ### Installation
  129. ```sh
  130. npm install --global json5
  131. ```
  132. ### Usage
  133. ```sh
  134. json5 [options] <file>
  135. ```
  136. If `<file>` is not provided, then STDIN is used.
  137. #### Options:
  138. - `-s`, `--space`: The number of spaces to indent or `t` for tabs
  139. - `-o`, `--out-file [file]`: Output to the specified file, otherwise STDOUT
  140. - `-v`, `--validate`: Validate JSON5 but do not output JSON
  141. - `-V`, `--version`: Output the version number
  142. - `-h`, `--help`: Output usage information
  143. ## Contibuting
  144. ### Development
  145. ```sh
  146. git clone https://github.com/json5/json5
  147. cd json5
  148. npm install
  149. ```
  150. When contributing code, please write relevant tests and run `npm test` and `npm
  151. run lint` before submitting pull requests. Please use an editor that supports
  152. [EditorConfig](http://editorconfig.org/).
  153. ### Issues
  154. To report bugs or request features regarding the JSON5 data format, please
  155. submit an issue to the [official specification
  156. repository](https://github.com/json5/json5-spec).
  157. To report bugs or request features regarding the JavaScript implentation of
  158. JSON5, please submit an issue to this repository.
  159. ## License
  160. MIT. See [LICENSE.md](./LICENSE.md) for details.
  161. ## Credits
  162. [Assem Kishore](https://github.com/aseemk) founded this project.
  163. [Michael Bolin](http://bolinfest.com/) independently arrived at and published
  164. some of these same ideas with awesome explanations and detail. Recommended
  165. reading: [Suggested Improvements to JSON](http://bolinfest.com/essays/json.html)
  166. [Douglas Crockford](http://www.crockford.com/) of course designed and built
  167. JSON, but his state machine diagrams on the [JSON website](http://json.org/), as
  168. cheesy as it may sound, gave us motivation and confidence that building a new
  169. parser to implement these ideas was within reach! The original
  170. implementation of JSON5 was also modeled directly off of Doug’s open-source
  171. [json_parse.js] parser. We’re grateful for that clean and well-documented
  172. code.
  173. [json_parse.js]:
  174. https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
  175. [Max Nanasy](https://github.com/MaxNanasy) has been an early and prolific
  176. supporter, contributing multiple patches and ideas.
  177. [Andrew Eisenberg](https://github.com/aeisenberg) contributed the original
  178. `stringify` method.
  179. [Jordan Tucker](https://github.com/jordanbtucker) has aligned JSON5 more closely
  180. with ES5, wrote the official JSON5 specification, completely rewrote the
  181. codebase from the ground up, and is actively maintaining this project.