sourav-das/stem-separator
3
1# JSON5 – JSON for Humans2 3[][Build4Status] [][Coverage6Status]7 8JSON5 is an extension to the popular [JSON] file format that aims to be9easier to **write and maintain _by hand_ (e.g. for config files)**.10It is _not intended_ to be used for machine-to-machine communication.11(Keep using JSON or other file formats for that. 🙂)12 13JSON5 was started in 2012, and as of 2022, now gets **[>65M downloads/week](https://www.npmjs.com/package/json5)**,14ranks in the **[top 0.1%](https://gist.github.com/anvaka/8e8fa57c7ee1350e3491)** of the most depended-upon packages on npm,15and has been adopted by major projects like16**[Chromium](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/runtime_enabled_features.json5;drc=5de823b36e68fd99009a29281b17bc3a1d6b329c),17[Next.js](https://github.com/vercel/next.js/blob/b88f20c90bf4659b8ad5cb2a27956005eac2c7e8/packages/next/lib/find-config.ts#L43-L46),18[Babel](https://babeljs.io/docs/en/config-files#supported-file-extensions),19[Retool](https://community.retool.com/t/i-am-attempting-to-append-several-text-fields-to-a-google-sheet-but-receiving-a-json5-invalid-character-error/7626),20[WebStorm](https://www.jetbrains.com/help/webstorm/json.html),21and [more](https://github.com/json5/json5/wiki/In-the-Wild)**.22It's also natively supported on **[Apple platforms](https://developer.apple.com/documentation/foundation/jsondecoder/3766916-allowsjson5)**23like **MacOS** and **iOS**.24 25Formally, the **[JSON5 Data Interchange Format](https://spec.json5.org/)** is a superset of JSON26(so valid JSON files will always be valid JSON5 files)27that expands its syntax to include some productions from [ECMAScript 5.1] (ES5).28It's also a strict _subset_ of ES5, so valid JSON5 files will always be valid ES5.29 30This JavaScript library is a reference implementation for JSON5 parsing and serialization,31and is directly used in many of the popular projects mentioned above32(where e.g. extreme performance isn't necessary),33but others have created [many other libraries](https://github.com/json5/json5/wiki/In-the-Wild)34across many other platforms.35 36[Build Status]: https://app.travis-ci.com/json5/json537 38[Coverage Status]: https://coveralls.io/github/json5/json539 40[JSON]: https://tools.ietf.org/html/rfc715941 42[ECMAScript 5.1]: https://www.ecma-international.org/ecma-262/5.1/43 44## Summary of Features45The following ECMAScript 5.1 features, which are not supported in JSON, have46been extended to JSON5.47 48### Objects49- Object keys may be an ECMAScript 5.1 _[IdentifierName]_.50- Objects may have a single trailing comma.51 52### Arrays53- Arrays may have a single trailing comma.54 55### Strings56- Strings may be single quoted.57- Strings may span multiple lines by escaping new line characters.58- Strings may include character escapes.59 60### Numbers61- Numbers may be hexadecimal.62- Numbers may have a leading or trailing decimal point.63- Numbers may be [IEEE 754] positive infinity, negative infinity, and NaN.64- Numbers may begin with an explicit plus sign.65 66### Comments67- Single and multi-line comments are allowed.68 69### White Space70- Additional white space characters are allowed.71 72[IdentifierName]: https://www.ecma-international.org/ecma-262/5.1/#sec-7.673 74[IEEE 754]: http://ieeexplore.ieee.org/servlet/opac?punumber=461093375 76## Example77Kitchen-sink example:78 79```js80{81 // comments82 unquoted: 'and you can quote me on that',83 singleQuotes: 'I can use "double quotes" here',84 lineBreaks: "Look, Mom! \85No \\n's!",86 hexadecimal: 0xdecaf,87 leadingDecimalPoint: .8675309, andTrailing: 8675309.,88 positiveSign: +1,89 trailingComma: 'in objects', andIn: ['arrays',],90 "backwardsCompatible": "with JSON",91}92```93 94A more real-world example is [this config file](https://github.com/chromium/chromium/blob/feb3c9f670515edf9a88f185301cbd7794ee3e52/third_party/blink/renderer/platform/runtime_enabled_features.json5)95from the Chromium/Blink project.96 97## Specification98For a detailed explanation of the JSON5 format, please read the [official99specification](https://json5.github.io/json5-spec/).100 101## Installation and Usage102### Node.js103```sh104npm install json5105```106 107#### CommonJS108```js109const JSON5 = require('json5')110```111 112#### Modules113```js114import JSON5 from 'json5'115```116 117### Browsers118#### UMD119```html120<!-- This will create a global `JSON5` variable. -->121<script src="https://unpkg.com/json5@2/dist/index.min.js"></script>122```123 124#### Modules125```html126<script type="module">127 import JSON5 from 'https://unpkg.com/json5@2/dist/index.min.mjs'128</script>129```130 131## API132The JSON5 API is compatible with the [JSON API].133 134[JSON API]:135https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON136 137### JSON5.parse()138Parses a JSON5 string, constructing the JavaScript value or object described by139the string. An optional reviver function can be provided to perform a140transformation on the resulting object before it is returned.141 142#### Syntax143 JSON5.parse(text[, reviver])144 145#### Parameters146- `text`: The string to parse as JSON5.147- `reviver`: If a function, this prescribes how the value originally produced by148 parsing is transformed, before being returned.149 150#### Return value151The object corresponding to the given JSON5 text.152 153### JSON5.stringify()154Converts a JavaScript value to a JSON5 string, optionally replacing values if a155replacer function is specified, or optionally including only the specified156properties if a replacer array is specified.157 158#### Syntax159 JSON5.stringify(value[, replacer[, space]])160 JSON5.stringify(value[, options])161 162#### Parameters163- `value`: The value to convert to a JSON5 string.164- `replacer`: A function that alters the behavior of the stringification165 process, or an array of String and Number objects that serve as a whitelist166 for selecting/filtering the properties of the value object to be included in167 the JSON5 string. If this value is null or not provided, all properties of the168 object are included in the resulting JSON5 string.169- `space`: A String or Number object that's used to insert white space into the170 output JSON5 string for readability purposes. If this is a Number, it171 indicates the number of space characters to use as white space; this number is172 capped at 10 (if it is greater, the value is just 10). Values less than 1173 indicate that no space should be used. If this is a String, the string (or the174 first 10 characters of the string, if it's longer than that) is used as white175 space. If this parameter is not provided (or is null), no white space is used.176 If white space is used, trailing commas will be used in objects and arrays.177- `options`: An object with the following properties:178 - `replacer`: Same as the `replacer` parameter.179 - `space`: Same as the `space` parameter.180 - `quote`: A String representing the quote character to use when serializing181 strings.182 183#### Return value184A JSON5 string representing the value.185 186### Node.js `require()` JSON5 files187When using Node.js, you can `require()` JSON5 files by adding the following188statement.189 190```js191require('json5/lib/register')192```193 194Then you can load a JSON5 file with a Node.js `require()` statement. For195example:196 197```js198const config = require('./config.json5')199```200 201## CLI202Since JSON is more widely used than JSON5, this package includes a CLI for203converting JSON5 to JSON and for validating the syntax of JSON5 documents.204 205### Installation206```sh207npm install --global json5208```209 210### Usage211```sh212json5 [options] <file>213```214 215If `<file>` is not provided, then STDIN is used.216 217#### Options:218- `-s`, `--space`: The number of spaces to indent or `t` for tabs219- `-o`, `--out-file [file]`: Output to the specified file, otherwise STDOUT220- `-v`, `--validate`: Validate JSON5 but do not output JSON221- `-V`, `--version`: Output the version number222- `-h`, `--help`: Output usage information223 224## Contributing225### Development226```sh227git clone https://github.com/json5/json5228cd json5229npm install230```231 232When contributing code, please write relevant tests and run `npm test` and `npm233run lint` before submitting pull requests. Please use an editor that supports234[EditorConfig](http://editorconfig.org/).235 236### Issues237To report bugs or request features regarding the JSON5 **data format**,238please submit an issue to the official239**[_specification_ repository](https://github.com/json5/json5-spec)**.240 241Note that we will never add any features that make JSON5 incompatible with ES5;242that compatibility is a fundamental premise of JSON5.243 244To report bugs or request features regarding this **JavaScript implementation**245of JSON5, please submit an issue to **_this_ repository**.246 247### Security Vulnerabilities and Disclosures248To report a security vulnerability, please follow the follow the guidelines249described in our [security policy](./SECURITY.md).250 251## License252MIT. See [LICENSE.md](./LICENSE.md) for details.253 254## Credits255[Aseem Kishore](https://github.com/aseemk) founded this project.256He wrote a [blog post](https://aseemk.substack.com/p/ignore-the-f-ing-haters-json5)257about the journey and lessons learned 10 years in.258 259[Michael Bolin](http://bolinfest.com/) independently arrived at and published260some of these same ideas with awesome explanations and detail. Recommended261reading: [Suggested Improvements to JSON](http://bolinfest.com/essays/json.html)262 263[Douglas Crockford](http://www.crockford.com/) of course designed and built264JSON, but his state machine diagrams on the [JSON website](http://json.org/), as265cheesy as it may sound, gave us motivation and confidence that building a new266parser to implement these ideas was within reach! The original267implementation of JSON5 was also modeled directly off of Doug’s open-source268[json_parse.js] parser. We’re grateful for that clean and well-documented269code.270 271[json_parse.js]:272https://github.com/douglascrockford/JSON-js/blob/03157639c7a7cddd2e9f032537f346f1a87c0f6d/json_parse.js273 274[Max Nanasy](https://github.com/MaxNanasy) has been an early and prolific275supporter, contributing multiple patches and ideas.276 277[Andrew Eisenberg](https://github.com/aeisenberg) contributed the original278`stringify` method.279 280[Jordan Tucker](https://github.com/jordanbtucker) has aligned JSON5 more closely281with ES5, wrote the official JSON5 specification, completely rewrote the282codebase from the ground up, and is actively maintaining this project.283 