UNPKG

3.43 kBMarkdownView Raw
1# JSON5 – Modern JSON
2
3JSON is strict. Keys need to be quoted; strings can only be double-quoted;
4objects and arrays can't have trailing commas; and comments aren't allowed.
5
6Using such a strict subset of "JavaScript object notation" was likely for the
7best at the time, but with modern ECMAScript 5 engines like V8 in Chrome and
8Node, these limitations are cumbersome.
9
10JSON5 does for JSON what ES5 did for ES3. It also is to regular ES5 what JSON
11was to ES3 — a pure subset.
12
13This module provides a replacement for ES5's native `JSON.parse()` method that
14understands these additions. The parser is based directly off of Douglas
15Crockford's [json_parse.js][], which avoids `eval()` and validates input as it
16parses 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
63Via npm on Node:
64
65```
66npm install json5
67```
68
69```js
70var JSON5 = require('json5');
71```
72
73Or 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
82var obj = JSON5.parse('{unquoted:"key",trailing:"comma",}');
83var str = JSON5.stringify(obj);
84console.log(obj);
85console.log(str);
86```
87
88`JSON5.stringify()` is currently aliased to the native `JSON.stringify()` in
89order for the output to be fully compatible with all JSON parsers today.
90
91## Development
92
93```
94git clone git://github.com/aseemk/json5.git
95cd json5
96npm link
97npm test
98```
99
100Feel free to [file issues](https://github.com/aseemk/json5/issues) and submit
101[pull requests](https://github.com/aseemk/json5/pulls) — contributions are
102welcome.
103
104If you submit a pull request, please be sure to add or update corresponding
105test cases, and ensure that `npm test` continues to pass.
106
107## License
108
109MIT License. © 2012 Aseem Kishore.
110
111## Credits
112
113[Michael Bolin](http://bolinfest.com/) independently arrived at and published
114some of these same ideas with awesome explanations and detail.
115Recommended 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
119JSON, but his state machine diagrams on the [JSON website](http://json.org/),
120as cheesy as it may sound, gave me motivation and confidence that building a
121new parser to implement these ideas this was within my reach!
122This code is also modeled directly off of Doug's open-source [json_parse.js][]
123parser. 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