jshn: only keep UP_* variables around while they are needed
[project/libubox.git] / sh / jshn.sh
index 85c4151..78414f0 100644 (file)
@@ -42,7 +42,7 @@ _json_inc() {
        # var=$1
        # dest=$2
 
-       eval "${JSON_PREFIX}$1=\$(( \${${JSON_PREFIX}$1:-0} + 1))${2:+; $2=\"\$${JSON_PREFIX}$1\"}"
+       let "${JSON_PREFIX}$1 += 1" "$2 = ${JSON_PREFIX}$1"
 }
 
 _json_add_generic() {
@@ -92,6 +92,7 @@ _json_close_table() {
 
        _json_get_var _s_cur JSON_CUR
        _json_get_var "${JSON_PREFIX}JSON_CUR" "UP_$_s_cur"
+       unset "${JSON_PREFIX}UP_$_s_cur"
 }
 
 json_set_namespace() {
@@ -103,7 +104,7 @@ json_set_namespace() {
 }
 
 json_cleanup() {
-       local unset
+       local unset tmp
 
        _json_get_var unset JSON_UNSET
        for tmp in $unset JSON_VAR; do
@@ -174,7 +175,7 @@ json_add_double() {
 # functions read access to json variables
 
 json_load() {
-       eval `jshn -r "$1"`
+       eval "`jshn -r "$1"`"
 }
 
 json_dump() {
@@ -206,6 +207,7 @@ json_get_keys() {
 json_get_values() {
        local _v_dest="$1"
        local _v_keys _v_val _select=
+       local _json_no_warning=1
 
        unset "$_v_dest"
        [ -n "$2" ] && {
@@ -257,6 +259,7 @@ json_select() {
        [[ "$1" == ".." ]] && {
                _json_get_var cur JSON_CUR
                _json_get_var cur "UP_$cur"
+               unset "${JSON_PREFIX}UP_$cur"
                _json_set_var JSON_CUR "$cur"
                return 0
        }
@@ -264,10 +267,12 @@ json_select() {
        case "$type" in
                object|array)
                        json_get_var cur "$target"
+                       _json_get_var "${JSON_PREFIX}UP_$cur" JSON_CUR
                        _json_set_var JSON_CUR "$cur"
                ;;
                *)
-                       echo "WARNING: Variable '$target' does not exist or is not an array/object"
+                       [ -n "$_json_no_warning" ] || \
+                               echo "WARNING: Variable '$target' does not exist or is not an array/object"
                        return 1
                ;;
        esac