Permalink
Browse files

'json -f FILE ...' for processing given file instead of stdin

  • Loading branch information...
1 parent 6abf34a commit 3cb7b211c10244fcc30010b0f93345306324c231 @trentm committed Jul 24, 2012
View
@@ -3,6 +3,16 @@
## json 4.0.0 (not yet released)
+- Add `-f FILE` option for specifying an input file (or files) instead of
+ stdin:
+
+ $ json -f foo.json
+ {
+ "foo": "bar"
+ }
+ $ json -f foo.json foo
+ bar
+
- [Backward incompatible] Move "auto-arrayification" to require explicitly
using the "-g" or "--group" option:
View
@@ -63,6 +63,7 @@ MIT (see the fine LICENSE.txt file).
# Command-Line Usage
<something generating JSON on stdout> | json [OPTIONS] [LOOKUPS...]
+ json -f FILE [OPTIONS] [LOOKUPS...]
See `json --help` output for full details.
View
@@ -9,6 +9,9 @@
.SH "SYNOPSIS"
something\-generating\-JSON\-on\-stdout | \fBjson\fR [OPTIONS] [LOOKUPS]
.
+.P
+\fBjson\fR \-f FILE [OPTIONS] [LOOKUPS\.\.\.]
+.
.SH "DESCRIPTION"
Pipe in your JSON for pretty\-printing, JSON validation, filtering, and modification\. Supply one or more \fBLOOKUPS\fR to extract a subset of the JSON\. HTTP header blocks are skipped by default\.
.
@@ -261,7 +264,7 @@ Shortcut for \fB\-o inspect\fR\.
Shortcut for \fB\-o json\fR\.
.
.SH "EXAMPLES"
-Let\'s use the Github API to look at the node \fIhttps://github/joyent/node\fR project:
+A typical JSON REST API response:
.
.IP "" 4
.
@@ -310,6 +313,18 @@ Say you just want to \fBextract one value\fR:
$ curl \-s http://ifconfig\.me/all\.json | json ip_addr
201\.73\.103\.12
+.
+.fi
+.
+.IP "" 0
+.
+.P
+Or, looking at the node\.js project \fIhttps://github\.com/joyent/node\fR using the Github API:
+.
+.IP "" 4
+.
+.nf
+
$ curl \-s https://api\.github\.com/repos/joyent/node | json open_issues
517
.
@@ -501,7 +516,7 @@ $ curl \-s https://api\.github\.com/repos/joyent/node/issues?state=open \e
.IP "" 0
.
.SS "Grouping"
-You can use the \'\-g\' or \'\-\-group\' option to group adjacent objects into an array of those objects; or to concatenate adjacent arrays into a single array\. To attempt to avoid false positives inside JSON strings, \fIadjacent\fR elements must have either no whitespace separation or at least a newline separation\. Examples:
+You can use the \fB\'\-g\' or \'\-\-group\' option to group adjacent objects\fR into an array of those objects; or to concatenate adjacent arrays into a single array\. To attempt to avoid false positives inside JSON strings, \fIadjacent\fR elements must have either no whitespace separation or at least a newline separation\. Examples:
.
.IP "" 4
.
View
@@ -85,6 +85,8 @@ <h2 id="SYNOPSIS">SYNOPSIS</h2>
<p>something-generating-JSON-on-stdout | <code>json</code> [OPTIONS] [LOOKUPS]</p>
+<p><code>json</code> -f FILE [OPTIONS] [LOOKUPS...]</p>
+
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p>Pipe in your JSON for pretty-printing, JSON validation, filtering,
@@ -274,8 +276,7 @@ <h2 id="OPTIONS">OPTIONS</h2>
<h2 id="EXAMPLES">EXAMPLES</h2>
-<p>Let's use the Github API to look at the <a href="https://github/joyent/node">node</a>
-project:</p>
+<p>A typical JSON REST API response:</p>
<pre><code>$ curl -s http://ifconfig.me/all.json
{"connection":"","ip_addr":"216.57.203.67","lang":"","remote_host":...
@@ -304,7 +305,12 @@ <h2 id="EXAMPLES">EXAMPLES</h2>
<pre><code>$ curl -s http://ifconfig.me/all.json | json ip_addr
201.73.103.12
-$ curl -s https://api.github.com/repos/joyent/node | json open_issues
+</code></pre>
+
+<p>Or, looking at the <a href="https://github.com/joyent/node">node.js project</a> using
+the Github API:</p>
+
+<pre><code>$ curl -s https://api.github.com/repos/joyent/node | json open_issues
517
</code></pre>
@@ -432,8 +438,8 @@ <h3 id="Array-processing-with-a">Array processing with -a</h3>
<h3 id="Grouping">Grouping</h3>
-<p>You can use the '-g' or '--group' option to group adjacent objects into an
-array of those objects; or to concatenate adjacent arrays into a single
+<p>You can use the <strong>'-g' or '--group' option to group adjacent objects</strong> into
+an array of those objects; or to concatenate adjacent arrays into a single
array. To attempt to avoid false positives inside JSON strings, <em>adjacent</em>
elements must have either no whitespace separation or at least a newline
separation. Examples:</p>
View
@@ -5,13 +5,16 @@
something-generating-JSON-on-stdout | `json` \[OPTIONS\] \[LOOKUPS\]
+`json` -f FILE \[OPTIONS\] \[LOOKUPS...\]
+
## DESCRIPTION
Pipe in your JSON for pretty-printing, JSON validation, filtering,
and modification. Supply one or more `LOOKUPS` to extract a
subset of the JSON. HTTP header blocks are skipped by default.
+
### Grouping
Use '-g' or '--group' to group objects or arrays separated
@@ -198,8 +201,7 @@ Other output formats are supported:
## EXAMPLES
-Let's use the Github API to look at the [node](https://github/joyent/node)
-project:
+A typical JSON REST API response:
$ curl -s http://ifconfig.me/all.json
{"connection":"","ip_addr":"216.57.203.67","lang":"","remote_host":...
@@ -226,6 +228,10 @@ Say you just want to **extract one value**:
$ curl -s http://ifconfig.me/all.json | json ip_addr
201.73.103.12
+
+Or, looking at the [node.js project](https://github.com/joyent/node) using
+the Github API:
+
$ curl -s https://api.github.com/repos/joyent/node | json open_issues
517
@@ -347,8 +353,8 @@ The **`-d` option can be used to specify a delimiter**:
### Grouping
-You can use the '-g' or '--group' option to group adjacent objects into an
-array of those objects; or to concatenate adjacent arrays into a single
+You can use the **'-g' or '--group' option to group adjacent objects** into
+an array of those objects; or to concatenate adjacent arrays into a single
array. To attempt to avoid false positives inside JSON strings, *adjacent*
elements must have either no whitespace separation or at least a newline
separation. Examples:
View
@@ -10,6 +10,7 @@ var VERSION = "4.0.0";
var util = require('util');
var pathlib = require('path');
var vm = require('vm');
+var fs = require('fs');
var warn = console.warn;
@@ -122,6 +123,7 @@ var json_parse=function(){"use strict";var a,b,c={'"':'"',"\\":"\\","/":"/",b:"\
function printHelp() {
util.puts("Usage:");
util.puts(" <something generating JSON on stdout> | json [OPTIONS] [LOOKUPS...]");
+ util.puts(" json -f FILE [OPTIONS] [LOOKUPS...]");
util.puts("");
util.puts("Pipe in your JSON for pretty-printing, JSON validation, filtering, ");
util.puts("and modification. Supply one or more `LOOKUPS` to extract a ");
@@ -185,6 +187,9 @@ function printHelp() {
util.puts(" --version Print version of this command and exit.");
util.puts(" -q, --quiet Don't warn if input isn't valid JSON.");
util.puts("");
+ util.puts(" -f FILE Path to a file to process. If not given, then");
+ util.puts(" stdin is used.");
+ util.puts("");
util.puts(" -H Drop any HTTP header block (as from `curl -i ...`).");
util.puts(" -g, --group Group adjacent objects or arrays into an array.");
util.puts(" -a, --array Process input as an array of separate inputs");
@@ -245,7 +250,8 @@ function parseArgv(argv) {
delim: ' ',
lookupDelim: '.',
outputKeys: false,
- group: false
+ group: false,
+ inputFiles: []
};
// Turn '-iH' into '-i -H', except for argument-accepting options.
@@ -349,6 +355,9 @@ function parseArgv(argv) {
case "--group":
parsed.group = true;
break;
+ case "-f":
+ parsed.inputFiles.push(args.shift());
+ break;
default: // arguments
if (!endOfOptions && arg.length > 0 && arg[0] === '-') {
throw new Error("unknown option '"+arg+"'");
@@ -366,6 +375,43 @@ function parseArgv(argv) {
}
+/**
+ * Get input from either given file paths or stdin.
+ *
+ * @param opts {Object} Parsed options.
+ * @param callback {Function} `function (err, callback)` where err is an
+ * error string if there was a problem.
+ */
+function getInput(opts, callback) {
+ if (opts.inputFiles.length === 0) {
+ // Read from stdin.
+ var chunks = [];
+
+ var stdin = process.openStdin();
+ stdin.setEncoding('utf8');
+ stdin.on('data', function (chunk) {
+ chunks.push(chunk);
+ });
+
+ stdin.on('end', function () {
+ callback(null, chunks.join(''));
+ });
+ } else {
+ // Read input files.
+ var i = 0;
+ var chunks = [];
+ try {
+ for (; i < opts.inputFiles.length; i++) {
+ chunks.push(fs.readFileSync(opts.inputFiles[i], 'utf8'));
+ }
+ } catch (e) {
+ return callback(
+ format('could not read "%s": %s', opts.inputFiles[i], e));
+ }
+ callback(null, chunks.join(''));
+ }
+}
+
function isInteger(s) {
return (s.search(/^-?[0-9]+$/) == 0);
@@ -689,6 +735,7 @@ function drainStdoutAndExit(code) {
+
//---- mainline
function main(argv) {
@@ -710,15 +757,12 @@ function main(argv) {
}
var lookupStrs = opts.args;
- var buffer = "";
-
- var stdin = process.openStdin();
- stdin.setEncoding('utf8');
- stdin.on('data', function (chunk) {
- buffer += chunk;
- });
+ getInput(opts, function (err, buffer) {
+ if (err) {
+ warn("json: error: %s", err)
+ return drainStdoutAndExit(1);
+ }
- stdin.on('end', function () {
// Take off a leading HTTP header if any and pass it through.
while (true) {
if (buffer.slice(0,5) === "HTTP/") {
View
@@ -1 +1,4 @@
-cat input | ../../lib/jsontool.js
+JSON=../../lib/jsontool.js
+
+cat input | $JSON
+$JSON -f input
@@ -1,3 +1,6 @@
{
"foo": "bar"
}
+{
+ "foo": "bar"
+}
View
@@ -3,23 +3,40 @@ JSON="node ../../lib/jsontool.js -q -o json-0 --group"
echo "# simple"
cat objects.input | $JSON
echo ""
+echo "# simple (-f)"
+$JSON -f objects.input
+
+echo ""
+echo "# separate"
+cat one.input two.input | $JSON
+echo ""
+echo "# separate (-f)"
+$JSON -f one.input -f two.input
+
+echo ""
echo "# on same line"
cat objects-on-same-line.input | $JSON
+
echo ""
echo "# more complex"
cat objects-complex.input | $JSON
+
echo ""
echo "# simple arrays"
cat arrays.input | $JSON
+
echo ""
echo "# on same line"
cat arrays-on-same-line.input | $JSON
+
echo ""
echo "# more complex"
cat arrays-complex.input | $JSON
+
echo ""
echo "# cat json files"
cat dir/*.json | $JSON
+
echo ""
echo "# objects on same line (space FAIL)"
cat objects-on-same-line-fail.input | $JSON
@@ -1,6 +1,15 @@
# simple
[{"one":1},{"two":2}]
+# simple (-f)
+[{"one":1},{"two":2}]
+
+# separate
+[{"one":1},{"two":2}]
+
+# separate (-f)
+[{"one":1},{"two":2}]
+
# on same line
[{"one":1},{"two":2}]
@@ -0,0 +1 @@
+{"one": 1}
@@ -0,0 +1 @@
+{"two": 2}

0 comments on commit 3cb7b21

Please sign in to comment.