Skip to content

Commit 9aa09fb

Browse files
committed
feat: Add "mode" parameter to set flags for a typical formt type easier
Recognize "json" (default), "cjson" and "json5". Parsing support for this feature was added by integrating the JJU parser (a4a606c).
1 parent 83cd33c commit 9aa09fb

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

.vscode/launch.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"name": "cli",
88
"program": "${workspaceRoot}/lib/cli.js",
99
"args": [
10-
"-C",
11-
"test/passes/1.json"
10+
"-M",
11+
"json5",
12+
"test/passes/json5.txt"
1213
],
1314
"skipFiles": [
1415
"<node_internals>/**/*.js"

README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
[![devDependency Status](https://david-dm.org/prantlf/jsonlint/dev-status.svg)](https://david-dm.org/prantlf/jsonlint#info=devDependencies)
88
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
99

10-
A JSON parser and validator with a command-line client. A [pure JavaScript version](http://prantlf.github.com/jsonlint/) of the service provided at [jsonlint.com](http://jsonlint.com).
10+
A [JSON]/[JSON5] parser and validator with a command-line client. A [pure JavaScript version] of the service provided at [jsonlint.com].
1111

1212
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).
1616
* Provides 100% compatible interface to the native `JSON.parse` method.
1717
* Optionally recognizes JavaScript-style comments and single quoted strings.
18-
* Supports JSON Schema drafts 04, 06 and 07.
19-
* Prefers the native JSON parser to gain the [best performance](./benchmarks#json-parser-comparison), while showing error messages of the same quality.
20-
* Implements JavaScript modules using [UMD](https://github.com/umdjs/umd) to work everywhere.
18+
* Supports [JSON Schema] drafts 04, 06 and 07.
19+
* Prefers the native JSON parser to gain the [best performance], while showing error messages of the same quality.
20+
* Implements JavaScript modules using [UMD] to work everywhere.
2121
* Depends on up-to-date npm modules with no installation warnings.
2222
* Small size - 17.6 kB minified, 6.1 kB gzipped.
2323

@@ -76,16 +76,18 @@ By default, `jsonlint` will either report a syntax error with details or pretty-
7676
-i, --in-place overwrite the input files
7777
-t, --indent [char] characters to use for indentation (default: " ")
7878
-c, --compact compact error display
79+
-M, --mode set other parsing flags according to a format type
7980
-C, --comments recognize and ignore JavaScript-style comments
8081
-S, --single-quoted-strings support single quotes as string delimiters
8182
-V, --validate [file] JSON schema file to use for validation
82-
-e, --environment [env] which specification of JSON Schema the validation
83-
file uses
83+
-e, --environment [env] which specification of JSON Schema
84+
the validation file uses
8485
-q, --quiet do not print the parsed json to stdin
8586
-p, --pretty-print force pretty-printing even for invalid input
8687
-v, --version output the version number
8788
-h, --help output usage information
8889

90+
Parsing mode can be "cjson" or "json5" to enable other flags automatically.
8991
If no files or directories are specified, stdin will be parsed. Environments
9092
for JSON schema validation are "json-schema-draft-04", "json-schema-draft-06"
9193
or "json-schema-draft-07". If not specified, it will be auto-detected.
@@ -125,8 +127,17 @@ The `parse` method offers more detailed [error information](#error-handling), th
125127

126128
| Option | Description |
127129
| -------------------------- | --------------------------- |
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+
| `ignoreComments` | ignores single-line and multi-line JavaScript-style comments during parsing as another "whitespace" (boolean) |
131+
| `allowSingleQuotedStrings` | accepts strings delimited by single-quotes too (boolean) |
132+
| `mode` | sets multiple options according to the type of input data (string) |
133+
134+
The `mode` parameter (string) sets parsing options to match a common format of input data:
135+
136+
| Mode | Description |
137+
| ------- | --------------------------- |
138+
| `json` | complies to the pure standard [JSON] (default if not set) |
139+
| `cjson` | JSON with comments (sets `ignoreComments`) |
140+
| `json5` | complies to [JSON5] (sets `ignoreComments`, `allowSingleQuotedStrings` and other flags) |
130141

131142
### Schema Validation
132143

@@ -150,13 +161,13 @@ const validate = compile('string with JSON schema', {
150161

151162
### Performance
152163

153-
This is a part of an output from the [parser benchmark](./benchmarks#json-parser-comparison), when parsing a 4.2 KB formatted string ([package.json](./package.json)) with Node.js 10.15.3:
164+
This is a part of an output from the [parser benchmark], when parsing a 4.2 KB formatted string ([package.json](./package.json)) with Node.js 10.15.3:
154165

155166
the built-in parser x 61,588 ops/sec ±0.75% (80 runs sampled)
156167
the pure jju parser x 11,396 ops/sec ±1.05% (86 runs sampled)
157168
the extended jju parser x 8,221 ops/sec ±0.99% (87 runs sampled)
158169

159-
A custom JSON parser is [a lot slower](./benchmarks/results/performance.md#results) than the built-in one. However, it is more important to have a [clear error reporting](./benchmarks/results/errorReportingQuality.md#results) than the highest speed in scenarios like parsing configuration files. Extending the parser with the support for comments and single-quoted strings does not affect significantly the performance.
170+
A custom JSON parser is [a lot slower] than the built-in one. However, it is more important to have a [clear error reporting] than the highest speed in scenarios like parsing configuration files. Extending the parser with the support for comments and single-quoted strings does not affect significantly the performance.
160171

161172
### Error Handling
162173

@@ -201,3 +212,14 @@ ${reason}`)
201212
Copyright (C) 2012-2019 Zachary Carter, Ferdinand Prantl
202213

203214
Licensed under the MIT license.
215+
216+
[pure JavaScript version]: http://prantlf.github.com/jsonlint/
217+
[jsonlint.com]: http://jsonlint.com
218+
[JSON]: https://tools.ietf.org/html/rfc8259
219+
[JSON5]: https://spec.json5.org
220+
[JSON Schema]: https://json-schema.org
221+
[UMD]: https://github.com/umdjs/umd
222+
[best performance]: ./benchmarks#json-parser-comparison
223+
[parser benchmark]: ./benchmarks#json-parser-comparison
224+
[a lot slower]: ./benchmarks/results/performance.md#results
225+
[clear error reporting]: ./benchmarks/results/errorReportingQuality.md#results

lib/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var options = require('commander')
2121
.option('-i, --in-place', 'overwrite the input files')
2222
.option('-t, --indent [char]', 'characters to use for indentation', ' ')
2323
.option('-c, --compact', 'compact error display')
24+
.option('-M, --mode [mode]', 'set other parsing flags according to a format type', 'json')
2425
.option('-C, --comments', 'recognize and ignore JavaScript-style comments')
2526
.option('-S, --single-quoted-strings', 'support single quotes as string delimiters')
2627
.option('-V, --validate [file]', 'JSON schema file to use for validation')
@@ -30,6 +31,7 @@ var options = require('commander')
3031
.version(pkg.version, '-v, --version')
3132
.on('--help', () => {
3233
console.log()
34+
console.log('Parsing mode can be "cjson" or "json5" to enable other flags automatically.')
3335
console.log('If no files or directories are specified, stdin will be parsed. Environments')
3436
console.log('for JSON schema validation are "json-schema-draft-04", "json-schema-draft-06"')
3537
console.log('or "json-schema-draft-07". If not specified, it will be auto-detected.')
@@ -50,6 +52,7 @@ function parse (source, file) {
5052
var parserOptions, parsed, formatted
5153
try {
5254
parserOptions = {
55+
mode: options.mode,
5356
ignoreComments: options.comments,
5457
allowSingleQuotedStrings: options.singleQuotedStrings
5558
}

lib/validator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
var validate
5555
try {
5656
schema = jsonlint.parse(schema, {
57+
mode: options.mode,
5758
ignoreComments: options.ignoreComments,
5859
allowSingleQuotedStrings: options.allowSingleQuotedStrings
5960
})

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"prepare": "npm run lint && npm run build",
3636
"lint": "standard --fix -v",
3737
"build": "node scripts/bundle-jsonlint && uglifyjs -o web/jsonlint.min.js --source-map \"filename='jsonlint.js',url='jsonlint.min.js.map',includeSources=true\" lib/jsonlint.js && uglifyjs -o web/validator.min.js --source-map \"filename='validator.js',url='validator.min.js.map',includeSources=true\" lib/validator.js && uglifyjs -o web/formatter.min.js --source-map \"filename='formatter.js',url='formatter.min.js.map',includeSources=true\" lib/formatter.js && uglifyjs -o web/sorter.min.js --source-map \"filename='sorter.js',url='sorter.min.js.map',includeSources=true\" lib/sorter.js && node scripts/bundle-schema-drafts | uglifyjs -o web/schema-drafts.min.js --source-map \"filename='schema-drafts.js',url='schema-drafts.min.js.map',includeSources=true\" && uglifyjs -o web/ajv.min.js --source-map \"filename='ajv.js',url='ajv.min.js.map',includeSources=true\" node_modules/ajv/dist/ajv.bundle.js",
38-
"test": "nyc --silent node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -S test/passes/strings.txt && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -pc test/fails/10.json || nyc report",
39-
"test:old": "node test/parse1 && node test/parse1 --native-parser && node test/parse2 && node test/parse3 && node test/parse4 && node test/parse5 && node test/portable && node lib/cli package.json test/recursive && node lib/cli -sq test/passes/hasOwnProperty.json && node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && node lib/cli -C test/passes/comments.txt && node lib/cli -S test/passes/strings.txt && node lib/cli -v && node lib/cli -h",
38+
"test": "nyc --silent node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -S test/passes/strings.txt && nyc --silent --no-clean node lib/cli -M json5 test/passes/json5.text && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -pc test/fails/10.json || nyc report",
39+
"test:old": "node test/parse1 && node test/parse1 --native-parser && node test/parse2 && node test/parse3 && node test/parse4 && node test/parse5 && node test/portable && node lib/cli package.json test/recursive && node lib/cli -sq test/passes/hasOwnProperty.json && node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && node lib/cli -C test/passes/comments.txt && node lib/cli -S test/passes/strings.txt && node lib/cli -M json5 test/passes/json5.text && node lib/cli -v && node lib/cli -h",
4040
"coverage": "cat coverage/lcov.info | npx coveralls",
4141
"start": "python -m SimpleHTTPServer",
4242
"web": "npm run web:sync && npm run web:deploy",

test/passes/json5.text

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
// String parameter
3+
"key": 'value',
4+
}

0 commit comments

Comments
 (0)