jshn: allow json_get_keys/values to refer to the currently selected array/table if...
[project/libubox.git] / sh / jshn.sh
index 110892cba29ba2d9ee60e7742851cf37040a685b..a3f6d1135e1d65d4a77d56409860ae2472df9979 100644 (file)
@@ -12,6 +12,15 @@ _json_set_var() {
        eval "${JSON_PREFIX}$___var=\"\$___val\""
 }
 
+__jshn_raw_append() {
+       local var="$1"
+       local value="$2"
+       local sep="${3:- }"
+
+       eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
+}
+
+
 _jshn_append() {
        local __var="$1"
        local __value="$2"
@@ -209,11 +218,37 @@ json_get_keys() {
        local __dest="$1"
        local _tbl_cur
 
-       json_get_var _tbl_cur "$2"
+       if [ -n "$2" ]; then
+               json_get_var _tbl_cur "$2"
+       else
+               _json_get_var _tbl_cur JSON_CUR
+       fi
        local __var="${JSON_PREFIX}KEYS_${_tbl_cur}"
        eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]"
 }
 
+json_get_values() {
+       local _v_dest="$1"
+       local _v_keys _v_val _select=
+
+       unset "$_v_dest"
+       [ -n "$2" ] && {
+               json_select "$2"
+               _select=1
+       }
+
+       json_get_keys _v_keys
+       set -- $_v_keys
+       while [ "$#" -gt 0 ]; do
+               json_get_var _v_val "$1"
+               __jshn_raw_append "$_v_dest" "$_v_val"
+               shift
+       done
+       [ -n "$_select" ] && json_select ..
+
+       return 0
+}
+
 json_get_var() {
        local __dest="$1"
        local __cur