You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+53-14Lines changed: 53 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -13,33 +13,53 @@ This is a fork of the original package with the following enhancements:
13
13
14
14
* Handles multiple files on the command line (by Greg Inman).
15
15
* 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.
16
18
* 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.
19
20
* 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.
21
21
* Depends on up-to-date npm modules with no installation warnings.
22
22
* Small size - 17.6 kB minified, 6.1 kB gzipped.
23
23
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
+
constdata=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
+
24
44
## Command-line Interface
25
45
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:
27
47
28
48
npm i @prantlf/jsonlint -g
29
49
30
-
Validate a file like so:
50
+
Validate a single file:
31
51
32
52
jsonlint myfile.json
33
53
34
-
or pipe the input into stdin:
54
+
or pipe the JSON input into `stdin`:
35
55
36
56
cat myfile.json | jsonlint
37
57
38
58
or process all `.json` files in a directory:
39
59
40
60
jsonlint mydir
41
61
42
-
`jsonlint` will either report a syntax error with details or prettyprint 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.
43
63
44
64
### Options
45
65
@@ -74,24 +94,43 @@ or process all `.json` files in a directory:
74
94
75
95
Install `jsonlint` with `npm` locally to be able to use the module programmatically:
76
96
77
-
npm i @prantlf/jsonlint -D
97
+
npm i @prantlf/jsonlint -S
78
98
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:
80
100
81
101
```js
82
102
const { parse } =require('@prantlf/jsonlint')
83
103
// Fails at the position of the character "?".
84
-
parse('{"creative": ?}') // throws an error
104
+
constdata2=parse('{"creative": ?}') // throws an error
85
105
// Succeeds returning the parsed JSON object.
86
-
parse('{"creative": false}')
106
+
constdata3=parse('{"creative": false}')
87
107
// Recognizes comments and single-quoted strings.
88
-
parse("{'creative': true /* for creativity */}", {
108
+
constdata3=parse("{'creative': true /* for creativity */}", {
89
109
ignoreComments:true,
90
110
allowSingleQuotedStrings:true
91
111
})
92
112
```
93
113
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:
|`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:
|`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:
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:
105
144
106
145
```js
107
146
constvalidate=compile('string with JSON schema', {
0 commit comments