Skip to content

Commit 83cd33c

Browse files
committed
feat: Support reviver from the native JSON.parse method
Documentation only. The feature was added by integrating the JJU parser (a4a606c).
1 parent a4a606c commit 83cd33c

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

README.md

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,53 @@ This is a fork of the original package with the following enhancements:
1313

1414
* Handles multiple files on the command line (by Greg Inman).
1515
* Walks directories recursively (by Paul Vollmer).
16+
* Provides 100% compatible interface to the native `JSON.parse` method.
17+
* Optionally recognizes JavaScript-style comments and single quoted strings.
1618
* Supports JSON Schema drafts 04, 06 and 07.
17-
* Can parse and skip JavaScript-style comments.
18-
* Can accept single quotes (apostrophes) as string delimiters.
19+
* Prefers the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
1920
* Implements JavaScript modules using [UMD](https://github.com/umdjs/umd) to work everywhere.
20-
* Prefers using the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
2121
* Depends on up-to-date npm modules with no installation warnings.
2222
* Small size - 17.6 kB minified, 6.1 kB gzipped.
2323

24+
## Synopsis
25+
26+
Check syntax of JSON files:
27+
28+
jsonlint -q data/*.json
29+
30+
Parse a JSON string:
31+
32+
```js
33+
const { parse } = require('@prantlf/jsonlint')
34+
const data = parse('{"creative": false}')
35+
```
36+
37+
Example of an error message:
38+
39+
Parse error on line 1, column 14:
40+
{"creative": ?}
41+
-------------^
42+
Unexpected token ?
43+
2444
## Command-line Interface
2545

26-
Install `jsonlint` with `npm`` globally to be able to use the command-line interface:
46+
Install `jsonlint` with `npm`` globally to be able to use the command-line interface in any directory:
2747

2848
npm i @prantlf/jsonlint -g
2949

30-
Validate a file like so:
50+
Validate a single file:
3151

3252
jsonlint myfile.json
3353

34-
or pipe the input into stdin:
54+
or pipe the JSON input into `stdin`:
3555

3656
cat myfile.json | jsonlint
3757

3858
or process all `.json` files in a directory:
3959

4060
jsonlint mydir
4161

42-
`jsonlint` will either report a syntax error with details or pretty print the source if it is valid.
62+
By default, `jsonlint` will either report a syntax error with details or pretty-print the source if it is valid.
4363

4464
### Options
4565

@@ -74,24 +94,43 @@ or process all `.json` files in a directory:
7494

7595
Install `jsonlint` with `npm` locally to be able to use the module programmatically:
7696

77-
npm i @prantlf/jsonlint -D
97+
npm i @prantlf/jsonlint -S
7898

79-
You might prefer methods this module to the built-in `JSON.parse` method because of a better error reporting or support for JavaScript-like comments:
99+
The only exported item is the `parse` method, which parses a string in the JSON format to a JavaScript object, array, or value:
80100

81101
```js
82102
const { parse } = require('@prantlf/jsonlint')
83103
// Fails at the position of the character "?".
84-
parse('{"creative": ?}') // throws an error
104+
const data2 = parse('{"creative": ?}') // throws an error
85105
// Succeeds returning the parsed JSON object.
86-
parse('{"creative": false}')
106+
const data3 = parse('{"creative": false}')
87107
// Recognizes comments and single-quoted strings.
88-
parse("{'creative': true /* for creativity */}", {
108+
const data3 = parse("{'creative': true /* for creativity */}", {
89109
ignoreComments: true,
90110
allowSingleQuotedStrings: true
91111
})
92112
```
93113

94-
The parsing method returns the parsed object or throws an error. If the parsing succeeds, you can to validate the input against a JSON schema too:
114+
The exported `parse` method is compatible with the native `JSON.parse` method. The second parameter provides the additional functionality:
115+
116+
parse(input, [reviver|options])
117+
118+
| Parameter | Description |
119+
| ---------- | ------------------------------------------- |
120+
| `input` | text in the JSON format (string) |
121+
| `reviver` | converts object and array values (function) |
122+
| `options` | customize parsing options (object) |
123+
124+
The `parse` method offers more detailed [error information](#error-handling), than the native `JSON.parse` method and it supports additional parsing options:
125+
126+
| Option | Description |
127+
| -------------------------- | --------------------------- |
128+
| `ignoreComments` | ignores single-line and multi-line JavaScript-style comments during parsing as another "whitespace" |
129+
| `allowSingleQuotedStrings` | accepts strings delimited by single-quotes too |
130+
131+
### Schema Validation
132+
133+
The parsing method returns the parsed object or throws an error. If the parsing succeeds, you can validate the input against a JSON schema using the `lib/validator` module:
95134

96135
```js
97136
const { parse } = require('@prantlf/jsonlint')
@@ -101,7 +140,7 @@ const validate = compile('string with JSON schema')
101140
validate(parse('string with JSON data'))
102141
```
103142

104-
Compiling JSON schema supports the same options as parsing JSON data (`ignoreComments`, `allowSingleQuotedStrings`). They can be passed as the second (object) parameter. The optional second `environment` parameter can be passed as an additional property in the options object too:
143+
Compiling JSON schema supports the same options as parsing JSON data (except for `reviver`). They can be passed as the second (object) parameter. The optional second `environment` parameter can be passed either as a string or as an additional property in the options object too:
105144

106145
```js
107146
const validate = compile('string with JSON schema', {

0 commit comments

Comments
 (0)