1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

Make tostream much more efficient

(`path(.[])` is a streaming `keys`!)
This commit is contained in:
Muh Muhten
2019-02-26 05:35:59 -05:00
committed by Nico Williams
parent 8c1e7175ec
commit d1a07cbdc1

View File

@ -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