jshn: reduce the number of appends to the cleanup list to speed up processing of...
[project/libubox.git] / sh / jshn.sh
index e919910..efac0af 100644 (file)
@@ -37,7 +37,6 @@ _json_export() {
        local __val="$2"
 
        export -- "$__var=$__val"
-       _jshn_append "JSON_UNSET" "$__var"
 }
 
 _json_add_key() {
@@ -96,6 +95,7 @@ _json_add_generic() {
 
        _json_export "${cur}_$var" "$val"
        _json_export "TYPE_${cur}_$var" "$type"
+       _jshn_append "JSON_UNSET" "${cur}_$var"
        _json_add_key "$cur" "$var"
 }
 
@@ -114,6 +114,7 @@ _json_add_table() {
        _json_export "KEYS_$table" ""
        [ "$itype" = "ARRAY" ] && _json_export "SEQ_$table" ""
        _json_stack_push "$table"
+       _jshn_append "JSON_UNSET" "$table"
 
        _json_get_var new_cur JSON_CUR
        _json_add_generic "$type" "$1" "$new_cur" "$cur"
@@ -141,15 +142,21 @@ json_cleanup() {
        local unset
 
        _json_get_var unset JSON_UNSET
-       [ -n "$unset" ] && eval "unset $unset"
+       for tmp in $unset JSON_VAR; do
+               unset \
+                       ${JSON_PREFIX}UP_$tmp \
+                       ${JSON_PREFIX}KEYS_$tmp \
+                       ${JSON_PREFIX}SEQ_$tmp \
+                       ${JSON_PREFIX}TYPE_$tmp \
+                       ${JSON_PREFIX}NAME_$tmp \
+                       ${JSON_PREFIX}$tmp
+       done
 
        unset \
                ${JSON_PREFIX}JSON_SEQ \
                ${JSON_PREFIX}JSON_STACK \
                ${JSON_PREFIX}JSON_CUR \
-               ${JSON_PREFIX}JSON_UNSET \
-               ${JSON_PREFIX}KEYS_JSON_VAR \
-               ${JSON_PREFIX}TYPE_JSON_VAR
+               ${JSON_PREFIX}JSON_UNSET
 }
 
 json_init() {
@@ -218,25 +225,35 @@ 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
+       local _v_keys _v_val _select=
 
        unset "$_v_dest"
-       json_get_keys _v_keys "$2"
-       json_select "$2"
+       [ -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
-       json_select ..
+       [ -n "$_select" ] && json_select ..
+
+       return 0
 }
 
 json_get_var() {