项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # JSON5 – Modern JSON
  2. [![Build Status](https://travis-ci.org/json5/json5.svg)](https://travis-ci.org/json5/json5)
  3. JSON is an excellent data format, but we think it can be better.
  4. **JSON5 is a proposed extension to JSON** that aims to make it easier for
  5. *humans to write and maintain* by hand. It does this by adding some minimal
  6. syntax features directly from ECMAScript 5.
  7. JSON5 remains a **strict subset of JavaScript**, adds **no new data types**,
  8. and **works with all existing JSON content**.
  9. JSON5 is *not* an official successor to JSON, and JSON5 content may *not*
  10. work with existing JSON parsers. For this reason, JSON5 files use a new .json5
  11. extension. *(TODO: new MIME type needed too.)*
  12. The code here is a **reference JavaScript implementation** for both Node.js
  13. and all browsers. It’s based directly off of Douglas Crockford’s own [JSON
  14. implementation][json_parse.js], and it’s both robust and secure.
  15. ## Why
  16. JSON isn’t the friendliest to *write*. Keys need to be quoted, objects and
  17. arrays can’t have trailing commas, and comments aren’t allowed — even though
  18. none of these are the case with regular JavaScript today.
  19. That was fine when JSON’s goal was to be a great data format, but JSON’s usage
  20. has expanded beyond *machines*. JSON is now used for writing [configs][ex1],
  21. [manifests][ex2], even [tests][ex3] — all by *humans*.
  22. [ex1]: http://plovr.com/docs.html
  23. [ex2]: https://www.npmjs.org/doc/files/package.json.html
  24. [ex3]: http://code.google.com/p/fuzztester/wiki/JSONFileFormat
  25. There are other formats that are human-friendlier, like YAML, but changing
  26. from JSON to a completely different format is undesirable in many cases.
  27. JSON5’s aim is to remain close to JSON and JavaScript.
  28. ## Features
  29. The following is the exact list of additions to JSON’s syntax introduced by
  30. JSON5. **All of these are optional**, and **all of these come from ES5**.
  31. ### Objects
  32. - Object keys can be unquoted if they’re valid [identifiers][mdn_variables].
  33. Yes, even reserved keywords (like `default`) are valid unquoted keys in ES5
  34. [[§11.1.5](http://es5.github.com/#x11.1.5), [§7.6](http://es5.github.com/#x7.6)].
  35. ([More info](https://mathiasbynens.be/notes/javascript-identifiers))
  36. *(TODO: Unicode characters and escape sequences aren’t yet supported in this
  37. implementation.)*
  38. - Object keys can also be single-quoted.
  39. - Objects can have trailing commas.
  40. [mdn_variables]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Variables
  41. ### Arrays
  42. - Arrays can have trailing commas.
  43. ### Strings
  44. - Strings can be single-quoted.
  45. - Strings can be split across multiple lines; just prefix each newline with a
  46. backslash. [ES5 [§7.8.4](http://es5.github.com/#x7.8.4)]
  47. ### Numbers
  48. - Numbers can be hexadecimal (base 16).
  49. - Numbers can begin or end with a (leading or trailing) decimal point.
  50. - Numbers can include `Infinity`, `-Infinity`, `NaN`, and `-NaN`.
  51. - Numbers can begin with an explicit plus sign.
  52. ### Comments
  53. - Both inline (single-line) and block (multi-line) comments are allowed.
  54. ## Example
  55. The following is a contrived example, but it illustrates most of the features:
  56. ```js
  57. {
  58. foo: 'bar',
  59. while: true,
  60. this: 'is a \
  61. multi-line string',
  62. // this is an inline comment
  63. here: 'is another', // inline comment
  64. /* this is a block comment
  65. that continues on another line */
  66. hex: 0xDEADbeef,
  67. half: .5,
  68. delta: +10,
  69. to: Infinity, // and beyond!
  70. finally: 'a trailing comma',
  71. oh: [
  72. "we shouldn't forget",
  73. 'arrays can have',
  74. 'trailing commas too',
  75. ],
  76. }
  77. ```
  78. This implementation’s own [package.json5](package.json5) is more realistic:
  79. ```js
  80. // This file is written in JSON5 syntax, naturally, but npm needs a regular
  81. // JSON file, so compile via `npm run build`. Be sure to keep both in sync!
  82. {
  83. name: 'json5',
  84. version: '0.5.0',
  85. description: 'JSON for the ES5 era.',
  86. keywords: ['json', 'es5'],
  87. author: 'Aseem Kishore <aseem.kishore@gmail.com>',
  88. contributors: [
  89. // TODO: Should we remove this section in favor of GitHub's list?
  90. // https://github.com/aseemk/json5/contributors
  91. 'Max Nanasy <max.nanasy@gmail.com>',
  92. 'Andrew Eisenberg <andrew@eisenberg.as>',
  93. 'Jordan Tucker <jordanbtucker@gmail.com>',
  94. ],
  95. main: 'lib/json5.js',
  96. bin: 'lib/cli.js',
  97. files: ["lib/"],
  98. dependencies: {},
  99. devDependencies: {
  100. gulp: "^3.9.1",
  101. 'gulp-jshint': "^2.0.0",
  102. jshint: "^2.9.1",
  103. 'jshint-stylish': "^2.1.0",
  104. mocha: "^2.4.5"
  105. },
  106. scripts: {
  107. build: 'node ./lib/cli.js -c package.json5',
  108. test: 'mocha --ui exports --reporter spec',
  109. // TODO: Would it be better to define these in a mocha.opts file?
  110. },
  111. homepage: 'http://json5.org/',
  112. license: 'MIT',
  113. repository: {
  114. type: 'git',
  115. url: 'https://github.com/aseemk/json5.git',
  116. },
  117. }
  118. ```
  119. ## Community
  120. Join the [Google Group](http://groups.google.com/group/json5) if you’re
  121. interested in JSON5 news, updates, and general discussion.
  122. Don’t worry, it’s very low-traffic.
  123. The [GitHub wiki](https://github.com/aseemk/json5/wiki) is a good place to track
  124. JSON5 support and usage. Contribute freely there!
  125. [GitHub Issues](https://github.com/aseemk/json5/issues) is the place to
  126. formally propose feature requests and report bugs. Questions and general
  127. feedback are better directed at the Google Group.
  128. ## Usage
  129. This JavaScript implementation of JSON5 simply provides a `JSON5` object just
  130. like the native ES5 `JSON` object.
  131. To use from Node:
  132. ```sh
  133. npm install json5
  134. ```
  135. ```js
  136. var JSON5 = require('json5');
  137. ```
  138. To use in the browser (adds the `JSON5` object to the global namespace):
  139. ```html
  140. <script src="json5.js"></script>
  141. ```
  142. Then in both cases, you can simply replace native `JSON` calls with `JSON5`:
  143. ```js
  144. var obj = JSON5.parse('{unquoted:"key",trailing:"comma",}');
  145. var str = JSON5.stringify(obj);
  146. ```
  147. `JSON5.parse` supports all of the JSON5 features listed above (*TODO: except
  148. Unicode*), as well as the native [`reviver` argument][json-parse].
  149. [json-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
  150. `JSON5.stringify` mainly avoids quoting keys where possible, but we hope to
  151. keep expanding it in the future (e.g. to also output trailing commas).
  152. It supports the native [`replacer` and `space` arguments][json-stringify],
  153. as well. *(TODO: Any implemented `toJSON` methods aren’t used today.)*
  154. [json-stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
  155. ### Extras
  156. If you’re running this on Node, you can also register a JSON5 `require()` hook
  157. to let you `require()` `.json5` files just like you can `.json` files:
  158. ```js
  159. require('json5/lib/require');
  160. require('./path/to/foo'); // tries foo.json5 after foo.js, foo.json, etc.
  161. require('./path/to/bar.json5');
  162. ```
  163. This module also provides a `json5` executable (requires Node) for converting
  164. JSON5 files to JSON:
  165. ```sh
  166. json5 -c path/to/foo.json5 # generates path/to/foo.json
  167. ```
  168. ## Development
  169. ```sh
  170. git clone git://github.com/aseemk/json5.git
  171. cd json5
  172. npm install
  173. npm test
  174. ```
  175. As the `package.json5` file states, be sure to run `npm run build` on changes
  176. to `package.json5`, since npm requires `package.json`.
  177. Feel free to [file issues](https://github.com/aseemk/json5/issues) and submit
  178. [pull requests](https://github.com/aseemk/json5/pulls) — contributions are
  179. welcome. If you do submit a pull request, please be sure to add or update the
  180. tests, and ensure that `npm test` continues to pass.
  181. ## License
  182. MIT. See [LICENSE.md](./LICENSE.md) for details.
  183. ## Credits
  184. [Michael Bolin](http://bolinfest.com/) independently arrived at and published
  185. some of these same ideas with awesome explanations and detail.
  186. Recommended reading:
  187. [Suggested Improvements to JSON](http://bolinfest.com/essays/json.html)
  188. [Douglas Crockford](http://www.crockford.com/) of course designed and built
  189. JSON, but his state machine diagrams on the [JSON website](http://json.org/),
  190. as cheesy as it may sound, gave me motivation and confidence that building a
  191. new parser to implement these ideas this was within my reach!
  192. This code is also modeled directly off of Doug’s open-source [json_parse.js][]
  193. parser. I’m super grateful for that clean and well-documented code.
  194. [json_parse.js]: https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
  195. [Max Nanasy](https://github.com/MaxNanasy) has been an early and prolific
  196. supporter, contributing multiple patches and ideas. Thanks Max!
  197. [Andrew Eisenberg](https://github.com/aeisenberg) has contributed the
  198. `stringify` method.
  199. [Jordan Tucker](https://github.com/jordanbtucker) has aligned JSON5 more closely
  200. with ES5 and is actively maintaining this project.