1
0
mirror of synced 2025-12-17 03:57:28 +08:00

Adding forceformat option: format invalid output just like jsonlint.com

When using jsonlint.com, even invalid JSON is pretty printed. It seemed
like a good option to have in the cli version as well. This is simply
inserting the char-by-char formatter from
https://github.com/umbrae/jsonlintdotcom and adding an option to cli.js.
This commit is contained in:
Lance Roggendorff
2012-09-12 23:11:11 -05:00
parent 1d7ed8ab13
commit 92ac817d17
2 changed files with 130 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ var fs = require("fs");
var path = require("path");
var parser = require("./jsonlint").parser;
var JSV = require("JSV").JSV;
var formatter = require("./formatter.js").formatter;
var options = require("nomnom")
.script("jsonlint")
@@ -50,10 +51,16 @@ var options = require("nomnom")
help: 'which specification of JSON Schema the validation file uses'
},
quiet: {
flag: true,key: "value",
flag: true,
key: "value",
string: '-q, --quiet',
"default": false,
help : 'do not print the parsed json to STDOUT'
help: 'do not print the parsed json to STDOUT'
},
forcePrettyPrint: {
flag: true,
string: '-p, --pretty-print',
help: 'force pretty printing even if invalid'
}
}).parse();
@@ -66,10 +73,13 @@ if (options.compact) {
}
function parse (source) {
var parsed,
formatted;
try {
var parsed = options.sort ?
sortObject(parser.parse(source)) :
parser.parse(source);
parsed = options.sort ?
sortObject(parser.parse(source)) :
parser.parse(source);
if (options.validate) {
var env = JSV.createEnvironment(options.env);
@@ -82,10 +92,31 @@ function parse (source) {
return JSON.stringify(parsed,null,options.indent);
} catch (e) {
if (! options.compact) {
if ( options.forcePrettyPrint ) {
/* From https://github.com/umbrae/jsonlintdotcom:
* If we failed to validate, run our manual formatter and then re-validate so that we
* can get a better line number. On a successful validate, we don't want to run our
* manual formatter because the automatic one is faster and probably more reliable.
*/
try {
formatted = formatter.formatJson(source, options.indent);
// Re-parse so exception output gets better line numbers
parsed = parser.parse(formatted);
} catch (e) {
if (! options.compact) {
console.error(e);
}
return formatted;
process.exit(1);
}
} else {
if (! options.compact) {
console.error(e);
}
process.exit(1);
}
process.exit(1);
}
}