1 | # JSON5 – Modern JSON
|
2 |
|
3 | JSON is strict. Keys need to be quoted; strings can only be double-quoted;
|
4 | objects and arrays can't have trailing commas; and comments aren't allowed.
|
5 |
|
6 | Using such a strict subset of "JavaScript object notation" was likely for the
|
7 | best at the time, but with modern ECMAScript 5 engines like V8 in Chrome and
|
8 | Node, these limitations are cumbersome.
|
9 |
|
10 | JSON5 does for JSON what ES5 did for ES3. It also is to regular ES5 what JSON
|
11 | was to ES3 — a pure subset.
|
12 |
|
13 | This module provides a replacement for ES5's native `JSON.parse()` method that
|
14 | understands these additions. The parser is based directly off of Douglas
|
15 | Crockford's [json_parse.js][], which avoids `eval()` and validates input as it
|
16 | parses it, making it secure and safe to use today.
|
17 |
|
18 | ## Features
|
19 |
|
20 | - Object keys don't need to be quoted if they contain no special characters.
|
21 | Yes, even reserved keywords are valid unquoted keys in ES5.
|
22 |
|
23 | *[TODO: Unicode characters and escape sequences aren't yet supported in
|
24 | unquoted keys.]*
|
25 |
|
26 | - Strings can be single-quoted.
|
27 |
|
28 | - Strings can be multi-line; just prefix the newline with a backslash.
|
29 |
|
30 | - Objects and arrays can have trailing commas.
|
31 |
|
32 | - Both inline (single-line) and block (multi-line) comments are allowed.
|
33 |
|
34 | - *[IDEA: Allow octal and hexadecimal numbers.]*
|
35 |
|
36 | ## Example
|
37 |
|
38 | ```js
|
39 | {
|
40 | foo: 'bar',
|
41 | while: true,
|
42 |
|
43 | this: 'is a\
|
44 | multi-line string',
|
45 |
|
46 | // this is an inline comment
|
47 | here: 'is another', // inline comment
|
48 |
|
49 | /* this is a block comment
|
50 | it continues on another line */
|
51 |
|
52 | finally: 'a trailing comma',
|
53 | oh: [
|
54 | 'we shouldn\'t forget',
|
55 | 'arrays can have',
|
56 | 'trailing commas too',
|
57 | ],
|
58 | }
|
59 | ```
|
60 |
|
61 | ## Installation
|
62 |
|
63 | Via npm on Node:
|
64 |
|
65 | ```
|
66 | npm install json5
|
67 | ```
|
68 |
|
69 | ```js
|
70 | var JSON5 = require('json5');
|
71 | ```
|
72 |
|
73 | Or in the browser (adds the `JSON5` object to the global namespace):
|
74 |
|
75 | ```html
|
76 | <script src="json5.js"></script>
|
77 | ```
|
78 |
|
79 | ## Usage
|
80 |
|
81 | ```js
|
82 | var obj = JSON5.parse('{unquoted:"key",trailing:"comma",}');
|
83 | var str = JSON5.stringify(obj);
|
84 | console.log(obj);
|
85 | console.log(str);
|
86 | ```
|
87 |
|
88 | `JSON5.stringify()` is currently aliased to the native `JSON.stringify()` in
|
89 | order for the output to be fully compatible with all JSON parsers today.
|
90 |
|
91 | ## Development
|
92 |
|
93 | ```
|
94 | git clone git://github.com/aseemk/json5.git
|
95 | cd json5
|
96 | npm link
|
97 | npm test
|
98 | ```
|
99 |
|
100 | Feel free to [file issues](https://github.com/aseemk/json5/issues) and submit
|
101 | [pull requests](https://github.com/aseemk/json5/pulls) — contributions are
|
102 | welcome.
|
103 |
|
104 | If you submit a pull request, please be sure to add or update corresponding
|
105 | test cases, and ensure that `npm test` continues to pass.
|
106 |
|
107 | ## License
|
108 |
|
109 | MIT License. © 2012 Aseem Kishore.
|
110 |
|
111 | ## Credits
|
112 |
|
113 | [Michael Bolin](http://bolinfest.com/) independently arrived at and published
|
114 | some of these same ideas with awesome explanations and detail.
|
115 | Recommended reading:
|
116 | [Suggested Improvements to JSON](http://bolinfest.com/essays/json.html)
|
117 |
|
118 | [Douglas Crockford](http://www.crockford.com/) of course designed and built
|
119 | JSON, but his state machine diagrams on the [JSON website](http://json.org/),
|
120 | as cheesy as it may sound, gave me motivation and confidence that building a
|
121 | new parser to implement these ideas this was within my reach!
|
122 | This code is also modeled directly off of Doug's open-source [json_parse.js][]
|
123 | parser. I'm super grateful for that clean and well-documented code.
|
124 |
|
125 | [json_parse.js]: https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js
|