Skip to content

Commit 7d521fb

Browse files
committed
feat: Add an option for ignoring trailing commas in object and arrays
A feature included in the JSON5 support. It was added by integrating the JJU parser (a4a606c).
1 parent 9aa09fb commit 7d521fb

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This is a fork of the original package with the following enhancements:
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+
* Optionally ignores trailing commas in objects and arrays.
1819
* Supports [JSON Schema] drafts 04, 06 and 07.
1920
* Prefers the native JSON parser to gain the [best performance], while showing error messages of the same quality.
2021
* Implements JavaScript modules using [UMD] to work everywhere.
@@ -79,6 +80,7 @@ By default, `jsonlint` will either report a syntax error with details or pretty-
7980
-M, --mode set other parsing flags according to a format type
8081
-C, --comments recognize and ignore JavaScript-style comments
8182
-S, --single-quoted-strings support single quotes as string delimiters
83+
-T, --trailing-commas' ignore trailing commas in objects and arrays
8284
-V, --validate [file] JSON schema file to use for validation
8385
-e, --environment [env] which specification of JSON Schema
8486
the validation file uses
@@ -128,6 +130,7 @@ The `parse` method offers more detailed [error information](#error-handling), th
128130
| Option | Description |
129131
| -------------------------- | --------------------------- |
130132
| `ignoreComments` | ignores single-line and multi-line JavaScript-style comments during parsing as another "whitespace" (boolean) |
133+
| `ignoreTrailingCommas` | ignores trailing commas in objects and arrays (boolean) |
131134
| `allowSingleQuotedStrings` | accepts strings delimited by single-quotes too (boolean) |
132135
| `mode` | sets multiple options according to the type of input data (string) |
133136

@@ -137,7 +140,7 @@ The `mode` parameter (string) sets parsing options to match a common format of i
137140
| ------- | --------------------------- |
138141
| `json` | complies to the pure standard [JSON] (default if not set) |
139142
| `cjson` | JSON with comments (sets `ignoreComments`) |
140-
| `json5` | complies to [JSON5] (sets `ignoreComments`, `allowSingleQuotedStrings` and other flags) |
143+
| `json5` | complies to [JSON5] (sets `ignoreComments`, `allowSingleQuotedStrings`, `ignoreTrailingCommas` and enables other JSON5 features) |
141144

142145
### Schema Validation
143146

lib/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var options = require('commander')
2424
.option('-M, --mode [mode]', 'set other parsing flags according to a format type', 'json')
2525
.option('-C, --comments', 'recognize and ignore JavaScript-style comments')
2626
.option('-S, --single-quoted-strings', 'support single quotes as string delimiters')
27+
.option('-T, --trailing-commas', 'ignore trailing commas in objects and arrays')
2728
.option('-V, --validate [file]', 'JSON schema file to use for validation')
2829
.option('-e, --environment [env]', 'which specification of JSON Schema the validation file uses')
2930
.option('-q, --quiet', 'do not print the parsed json to stdin')
@@ -54,6 +55,7 @@ function parse (source, file) {
5455
parserOptions = {
5556
mode: options.mode,
5657
ignoreComments: options.comments,
58+
ignoreTrailingCommas: options.trailingCommas,
5759
allowSingleQuotedStrings: options.singleQuotedStrings
5860
}
5961
parsed = parser.parse(source, parserOptions)

lib/validator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
schema = jsonlint.parse(schema, {
5757
mode: options.mode,
5858
ignoreComments: options.ignoreComments,
59+
ignoreTrailingCommas: options.ignoreTrailingCommas,
5960
allowSingleQuotedStrings: options.allowSingleQuotedStrings
6061
})
6162
validate = ajv.compile(schema)

src/configurable-parser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function processOptions (options) {
1414
changed.ignoreComments = this.ignoreComments
1515
this.ignoreComments = options.ignoreComments
1616
}
17+
if (options.ignoreTrailingCommas !== undefined) {
18+
changed.ignoreTrailingCommas = this.ignoreTrailingCommas
19+
this.ignoreTrailingCommas = options.ignoreTrailingCommas
20+
}
1721
if (options.allowSingleQuotedStrings !== undefined) {
1822
changed.allowSingleQuotedStrings = this.allowSingleQuotedStrings
1923
this.allowSingleQuotedStrings = options.allowSingleQuotedStrings
@@ -44,7 +48,8 @@ var oldNode = typeof process !== 'undefined' && process.version.startsWith('v4.'
4448

4549
function needsCustomParser () {
4650
return this.ignoreComments || this.allowSingleQuotedStrings ||
47-
this.mode === 'cjson' || this.mode === 'json5' || isSafari || oldNode
51+
this.ignoreTrailingCommas || this.mode === 'cjson' || this.mode === 'json5' ||
52+
isSafari || oldNode
4853
}
4954

5055
function getReviver (options) {

src/custom-parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
4444

4545
var json5 = options.mode === 'json5'
4646
var ignoreComments = options.ignoreComments || options.mode === 'cjson' || json5
47+
var ignoreTrailingCommas = options.ignoreTrailingCommas || json5
4748
var allowSingleQuotedStrings = options.allowSingleQuotedStrings || json5
4849
var reviver = options.reviver
4950

@@ -230,7 +231,7 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
230231

231232
var char = input[position++]
232233
if (char === '}' && key === undefined) {
233-
if (!json5 && isNotEmpty) {
234+
if (!ignoreTrailingCommas && isNotEmpty) {
234235
--position
235236
fail('Trailing comma in object')
236237
}
@@ -305,7 +306,7 @@ function parseCustom (input, options) { // eslint-disable-line no-unused-vars
305306
fail('Elisions are not supported')
306307
}
307308
} else if (char === ']') {
308-
if (!json5 && item === undefined && result.length) {
309+
if (!ignoreTrailingCommas && item === undefined && result.length) {
309310
--position
310311
fail('Trailing comma in array')
311312
}

web/jsonlint.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ <h1>JSON Lint</h1>
9696
<input type="checkbox" id="comments">
9797
<label for="comments">Ignore JavaScript-style comments</label>
9898
</div>
99+
<div>
100+
<input type="checkbox" id="trailing-commas">
101+
<label for="trailing-commas">Ignore trailing commas</label>
102+
</div>
99103
<div>
100104
<input type="checkbox" id="single-quoted-strings">
101105
<label for="single-quoted-strings">Recognize single-quoted strings</label>
@@ -139,6 +143,7 @@ <h2>Result</h2>
139143
var parser = jsonlint.parser
140144
var parserOptions = {
141145
ignoreComments: document.getElementById('comments').checked,
146+
ignoreTrailingCommas: document.getElementById('trailing-commas').checked,
142147
allowSingleQuotedStrings: document.getElementById('single-quoted-strings').checked
143148
}
144149
var parsed = parser.parse(document.getElementById('data').value, parserOptions)

0 commit comments

Comments
 (0)