From 35fd653f87d20a864b51d83cbc78d8fda81d77d9 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Sun, 23 Jul 2023 00:01:52 -0500 Subject: [PATCH] Fix --stream-errors w/o --stream, and doc+test (close #2748) --- docs/content/manual/manual.yml | 10 ++++++++++ jq.1.prebuilt | 9 +++++++++ src/main.c | 2 +- tests/shtest | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/content/manual/manual.yml b/docs/content/manual/manual.yml index ff68482a3d..073e983fe2 100644 --- a/docs/content/manual/manual.yml +++ b/docs/content/manual/manual.yml @@ -130,6 +130,16 @@ sections: conjunction with filtering and the `reduce` and `foreach` syntax to reduce large inputs incrementally. + * `--stream-errors`: + + Like `--stream`, but invalid JSON inputs yield array calues + where the first element is the error and the second is a path. + For example, `["a",n]` produces ["Invalid literal at line 1, + column 9",[1]]`. + + Implies `--stream`. Invalid JSON inputs produce no error values + when `--stream` w/o `--stream-errors`. + * `--slurp`/`-s`: Instead of running the filter for each JSON object in the diff --git a/jq.1.prebuilt b/jq.1.prebuilt index 80933f748e..f7fb8e2895 100644 --- a/jq.1.prebuilt +++ b/jq.1.prebuilt @@ -73,6 +73,15 @@ Parse the input in streaming fashion, outputting arrays of path and leaf values This is useful for processing very large inputs\. Use this in conjunction with filtering and the \fBreduce\fR and \fBforeach\fR syntax to reduce large inputs incrementally\. . .TP +\fB\-\-stream\-errors\fR: +. +.IP +Like \fB\-\-stream\fR, but invalid JSON inputs yield array calues where the first element is the error and the second is a path\. For example, \fB["a",n]\fR produces ["Invalid literal at line 1, column 9",[1]]`\. +. +.IP +Implies \fB\-\-stream\fR\. Invalid JSON inputs produce no error values when \fB\-\-stream\fR w/o \fB\-\-stream\-errors\fR\. +. +.TP \fB\-\-slurp\fR/\fB\-s\fR: . .IP diff --git a/src/main.c b/src/main.c index 48af5a31c1..1e85f9e360 100644 --- a/src/main.c +++ b/src/main.c @@ -455,7 +455,7 @@ int main(int argc, char* argv[]) { continue; } if (isoption(argv[i], 0, "stream-errors", &short_opts)) { - parser_flags |= JV_PARSE_STREAM_ERRORS; + parser_flags |= JV_PARSE_STREAMING | JV_PARSE_STREAM_ERRORS; continue; } if (isoption(argv[i], 'e', "exit-status", &short_opts)) { diff --git a/tests/shtest b/tests/shtest index b2b8571d86..1ee84d3ac4 100755 --- a/tests/shtest +++ b/tests/shtest @@ -180,6 +180,11 @@ $VALGRIND $Q $JQ -c '. as $d|path(..) as $p|$d|getpath($p)|select((type|. != "ar $VALGRIND $Q $JQ --stream -c '.|select(length==2)' < "$JQTESTDIR/torture/input0.json" > $d/out1 diff $d/out0 $d/out1 +printf '["Unfinished JSON term at EOF at line 1, column 1",[0]]\n' > $d/expected +printf '[' | $VALGRIND $Q $JQ --stream-errors -c . > $d/out 2>&1 +diff $d/out $d/expected + + ## XXX This test can be moved to tests/all.test _now_ clean=false if which seq > /dev/null 2>&1; then