summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-12-22 23:06:27 -0600
committerNicolas Williams <nico@cryptonector.com>2014-12-26 23:05:56 -0600
commit5bfb9781f7e5ba2ad99b71150bccc559d53b928f (patch)
treea6c1a446f802ebdaa7c505340d2573d888402f73 /builtin.c
parent906d2537b9d0384985ed6259ef77ca1bd3d503ed (diff)
Add Streaming parser (--stream)
Streaming means that outputs are produced as soon as possible. With the `foreach` syntax one can write programs which reduce portions of the streaming parse of a large input (reduce into proper JSON values, for example), and discard the rest, processing incrementally. This: $ jq -c --stream . should produce the same output as this: $ jq -c '. as $dot | path(..) as $p | $dot | getpath($p) | [$p,.]' The output of `jq --stream .` should be a sequence of`[[<path>],<leaf>]` and `[[<path>]]` values. The latter indicate that the array/object at that path ended. Scalars and empty arrays and objects are leaf values for this purpose. For example, a truncated input produces a path as soon as possible, then later the error: $ printf '[0,\n'|./jq -c --stream . [[0],0] parse error: Unfinished JSON term at EOF at line 3, column 0 $
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 4ffdc3bf..9a5660a7 100644
--- a/builtin.c
+++ b/builtin.c
@@ -862,6 +862,18 @@ static jv f_modulemeta(jq_state *jq, jv a) {
return load_module_meta(jq, a);
}
+static jv f_input(jq_state *jq, jv input) {
+ jv_free(input);
+ jq_input_cb cb;
+ void *data;
+ jq_get_input_cb(jq, &cb, &data);
+ jv v = cb(jq, data);
+ if (jv_is_valid(v) || jv_invalid_has_msg(jv_copy(v)))
+ return v;
+ return jv_invalid_with_msg(jv_string("break"));
+}
+
+
#define LIBM_DD(name) \
{(cfunction_ptr)f_ ## name, "_" #name, 1},
@@ -912,6 +924,7 @@ static const struct cfunction function_list[] = {
{(cfunction_ptr)f_env, "env", 1},
{(cfunction_ptr)f_match, "_match_impl", 4},
{(cfunction_ptr)f_modulemeta, "modulemeta", 1},
+ {(cfunction_ptr)f_input, "_input", 1},
};
#undef LIBM_DD
@@ -1014,6 +1027,7 @@ static const char* const jq_builtins[] = {
"def nulls: select(type == \"null\");",
"def values: select(. != null);",
"def scalars: select(. == null or . == true or . == false or type == \"number\" or type == \"string\");",
+ "def scalars_or_empty: select(. == null or . == true or . == false or type == \"number\" or type == \"string\" or ((type==\"array\" or type==\"object\") and length==0));",
"def leaf_paths: paths(scalars);",
"def join($x): reduce .[] as $i (\"\"; . + (if . == \"\" then $i else $x + $i end));",
"def flatten: reduce .[] as $i ([]; if $i | type == \"array\" then . + ($i | flatten) else . + [$i] end);",