|
| 1 | +# jsonc |
| 2 | + |
| 3 | +[](https://travis-ci.org/onury/jsonc) |
| 4 | +[](https://www.npmjs.com/package/jsonc) |
| 5 | +[](https://github.com/onury/jsonc) |
| 6 | +[](https://david-dm.org/onury/jsonc) |
| 7 | +[](https://github.com/onury/jsonc/blob/master/LICENSE) |
| 8 | +[](https://github.com/onury/jsonc/graphs/punch-card) |
| 9 | + |
| 10 | +> © 2018, Onur Yıldırım ([@onury](https://github.com/onury)). MIT License. |
| 11 | +
|
| 12 | +Everything you need in JSON land. |
| 13 | + |
| 14 | +`npm i jsonc` |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Parse JSON with comments. |
| 19 | +- Stringify objects with circular references. |
| 20 | +- Safely parse / stringify without try/catch blocks. |
| 21 | +- Read and auto-parse JSON files gracefully, sync or async (with promises). |
| 22 | +- Auto-stringify and write JSON files gracefully, sync or async (with promises). |
| 23 | +- Strips UTF-8 BOM. |
| 24 | +- Log objects as JSON (without worrying about errors). |
| 25 | +- Uglify/beautify JSON strings. |
| 26 | +- More helpful JSON errors. |
| 27 | +- Friendly API. |
| 28 | +- TypeScript support. |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +See the concise [API reference][docs-api]. |
| 33 | + |
| 34 | +```js |
| 35 | +const jsonc = require('jsonc'); |
| 36 | +// or |
| 37 | +import { jsonc } from 'jsonc'; |
| 38 | +``` |
| 39 | + |
| 40 | +This is safe for JSON with comments: |
| 41 | +```js |
| 42 | +jsonc.parse('// comment\n{"data": /* comment */ "value"}\n'); // » { data: 'value' } |
| 43 | +``` |
| 44 | + |
| 45 | +And this is safe for circular references: |
| 46 | +```js |
| 47 | +const obj = { x: 1 }; |
| 48 | +obj.y = obj; // circular |
| 49 | +jsonc.stringify(obj); // » { x: 1, y: '[Circular]' } |
| 50 | +``` |
| 51 | + |
| 52 | +But this is seriously safe: |
| 53 | +```js |
| 54 | +// safe version of every method |
| 55 | +const jsonc = require('jsonc').safe; |
| 56 | +// or |
| 57 | +import { safe as jsonc } from 'jsonc'; |
| 58 | + |
| 59 | +const [err, result] = jsonc.parse('[invalid JSON}'); |
| 60 | +if (err) { |
| 61 | + console.log(`Failed to parse JSON: ${err.message}`); |
| 62 | +} else { |
| 63 | + console.log(result); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Documentation |
| 68 | + |
| 69 | +See the concise [API reference][docs-api]. |
| 70 | + |
| 71 | +## Change Log |
| 72 | + |
| 73 | +- **v1.0.0** (2018-10-10) |
| 74 | + + Initial release. |
| 75 | + |
| 76 | + |
| 77 | +## License |
| 78 | +MIT. |
| 79 | + |
| 80 | + |
| 81 | +[docs-api]:https://onury.io/jsonc/api |
| 82 | +[strip-json-comments]:https://github.com/sindresorhus/strip-json-comments |
| 83 | +[json-stringify-safe]:https://github.com/isaacs/json-stringify-safe |
| 84 | +[parse-json]:https://github.com/sindresorhus/parse-json |
| 85 | +[fs-extra]:https://www.npmjs.com/package/fs-extra |
0 commit comments