summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMuh Muhten <muh.muhten@gmail.com>2019-02-26 05:35:59 -0500
committerNico Williams <nico@cryptonector.com>2019-02-26 11:05:25 -0600
commitd1a07cbdc1363bf2f8846fa5c84a81c6e95435d4 (patch)
tree463d346841ab267db5177df8ef4851f1e0bd1839
parent8c1e7175ec75cc7933d9c2839a684250b4ff5ef4 (diff)
Make tostream much more efficient
(`path(.[])` is a streaming `keys`!)
-rw-r--r--src/builtin.jq19
1 files changed, 3 insertions, 16 deletions
diff --git a/src/builtin.jq b/src/builtin.jq
index 7916d7d8..f45802f6 100644
--- a/src/builtin.jq
+++ b/src/builtin.jq
@@ -215,22 +215,9 @@ def fromstream(i): {x: null, e: false} as $init |
else setpath(["e"]; $i[0]|length==1) end
; if .e then .x else empty end);
def tostream:
- {string:true,number:true,boolean:true,null:true} as $leaf_types |
- . as $dot |
- if $leaf_types[$dot|type] or length==0 then [[],$dot]
- else
- # We really need a _streaming_ form of `keys`.
- # We can use `range` for arrays, but not for objects.
- keys_unsorted as $keys |
- $keys[-1] as $last|
- ((# for each key
- $keys[] | . as $key |
- $dot[$key] | . as $dot |
- # recurse on each key/value
- tostream|.[0]|=[$key]+.),
- # then add the closing marker
- [[$last]])
- end;
+ path(def r: (.[]?|r), .; r) as $p |
+ getpath($p) |
+ reduce path(.[]?) as $q ([$p, .]; [$p+$q]);
# Assuming the input array is sorted, bsearch/1 returns