11 Commits

Author SHA1 Message Date
Zachary Carter
fca1f5f846 1.6.0 2013-04-02 10:44:25 -07:00
Zach Carter
f019341e15 Merge pull request #30 from teknogeek0/85bf286c795eeef6d17f1c89b646a94b5623e385
Add in the -q/--quiet option to the README.md so folks know its there.
2013-04-01 21:43:59 -07:00
Christopher Munns
85bf286c79 Adding in the -q/--quiet option to the Readme.md 2013-04-01 21:39:15 -04:00
Zach Carter
79b553fb65 Merge pull request #27 from lroggendorff/add-forceformat-option
Add forceformat option
2012-12-16 11:04:07 -08:00
Lance Roggendorff
59c36ad370 Merge branch 'fix-inplace-overwriting' into add-forceformat-option
* fix-inplace-overwriting:
  Fixing variable name when overwriting input file.
2012-09-12 23:12:25 -05:00
Lance Roggendorff
92ac817d17 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.
2012-09-12 23:11:11 -05:00
Lance Roggendorff
0f3202370f Fixing variable name when overwriting input file.
It looks like at one time the variable referencing the input file was
'path', and later on it was changed to 'json', but the if
(options.inplace) block was never updated.
2012-09-12 22:55:26 -05:00
Zachary Carter
1d7ed8ab13 1.5.1 2012-08-22 10:37:25 -07:00
Zach Carter
5e02f8b8e0 Merge pull request #26 from gregglind/quiet25
added quiet mode #25
2012-08-22 10:34:16 -07:00
Gregg Lind
f2e1de3039 added quiet mode 2012-08-22 09:10:54 -05:00
Zach Carter
cdb7737033 Update master 2012-06-19 20:44:54 -07:00
4 changed files with 150 additions and 15 deletions

View File

@@ -21,15 +21,21 @@ jsonlint will either report a syntax error with details or pretty print the sour
### Options
$ jsonlint -h
Usage: jsonlint <file> [options]
<file> file to parse; otherwise uses stdin
usage: jsonlint <file> [options]
file file to parse; otherwise uses stdin
options:
-v, --version print version and exit
-s, --sort-keys sort object keys
-i, --in-place overwrite the file
-t CHAR, --indent CHAR character(s) to use for indentation
-v, --version print version and exit
-s, --sort-keys sort object keys
-i, --in-place overwrite the file
-t CHAR, --indent CHAR character(s) to use for indentation
-c, --compact compact error display
-V, --validate a JSON schema to use for validation
-e, --environment which specification of JSON Schema the validation file uses
-q, --quiet do not print the parsed json to STDOUT
## Module interface

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")
@@ -48,6 +49,18 @@ var options = require("nomnom")
string: '-e, --environment',
"default": "json-schema-draft-03",
help: 'which specification of JSON Schema the validation file uses'
},
quiet: {
flag: true,
key: "value",
string: '-q, --quiet',
"default": false,
help: 'do not print the parsed json to STDOUT'
},
forcePrettyPrint: {
flag: true,
string: '-p, --pretty-print',
help: 'force pretty printing even if invalid'
}
}).parse();
@@ -60,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);
@@ -76,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);
}
}
@@ -98,9 +135,9 @@ function main (args) {
var json = path.normalize(options.file);
source = parse(fs.readFileSync(json, "utf8"));
if (options.inplace) {
fs.writeSync(fs.openSync(path,'w+'), source, 0, "utf8");
fs.writeSync(fs.openSync(json,'w+'), source, 0, "utf8");
} else {
console.log(source);
if (! options.quiet) { console.log(source)};
}
} else {
var stdin = process.openStdin();
@@ -110,7 +147,7 @@ function main (args) {
source += chunk.toString('utf8');
});
stdin.on('end', function () {
console.log(parse(source));
if (! options.quiet) {console.log(parse(source))};
});
}
}

92
lib/formatter.js Normal file
View File

@@ -0,0 +1,92 @@
#!/usr/bin/env node
/**
* Manual formatter taken straight from https://github.com/umbrae/jsonlintdotcom
**/
/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: false, bitwise: true, newcap: true, maxerr: 50, indent: 4 */
/**
* jsl.format - Provide json reformatting in a character-by-character approach, so that even invalid JSON may be reformatted (to the best of its ability).
*
**/
var formatter = (function () {
function repeat(s, count) {
return new Array(count + 1).join(s);
}
function formatJson(json, indentChars) {
var i = 0,
il = 0,
tab = (typeof indentChars !== "undefined") ? indentChars : " ",
newJson = "",
indentLevel = 0,
inString = false,
currentChar = null;
for (i = 0, il = json.length; i < il; i += 1) {
currentChar = json.charAt(i);
switch (currentChar) {
case '{':
case '[':
if (!inString) {
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
indentLevel += 1;
} else {
newJson += currentChar;
}
break;
case '}':
case ']':
if (!inString) {
indentLevel -= 1;
newJson += "\n" + repeat(tab, indentLevel) + currentChar;
} else {
newJson += currentChar;
}
break;
case ',':
if (!inString) {
newJson += ",\n" + repeat(tab, indentLevel);
} else {
newJson += currentChar;
}
break;
case ':':
if (!inString) {
newJson += ": ";
} else {
newJson += currentChar;
}
break;
case ' ':
case "\n":
case "\t":
if (inString) {
newJson += currentChar;
}
break;
case '"':
if (i > 0 && json.charAt(i - 1) !== '\\') {
inString = !inString;
}
newJson += currentChar;
break;
default:
newJson += currentChar;
break;
}
}
return newJson;
}
return { "formatJson": formatJson };
}());
if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
exports.formatter = formatter;
}

View File

@@ -8,7 +8,7 @@
"lint",
"jsonlint"
],
"version": "1.5.0",
"version": "1.6.0",
"preferGlobal": true,
"repository": {
"type": "git",