X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=sh%2Fjshn.sh;h=a3f6d1135e1d65d4a77d56409860ae2472df9979;hb=b7e61837e7269a8eafca280f904ab4a4e1425840;hp=39d53318fe5d5f4901a627021f1bc30b2b91a084;hpb=0505bf5f424cb5ba607c06cd821c1593fd7a2bef;p=project%2Flibubox.git diff --git a/sh/jshn.sh b/sh/jshn.sh index 39d5331..a3f6d11 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -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" @@ -103,7 +112,7 @@ _json_add_table() { local table="JSON_$itype$seq" _json_export "UP_$table" "$cur" _json_export "KEYS_$table" "" - [ "$TYPE" = "ARRAY" ] && _json_export "SEQ_$table" "" + [ "$itype" = "ARRAY" ] && _json_export "SEQ_$table" "" _json_stack_push "$table" _json_get_var new_cur JSON_CUR @@ -182,6 +191,10 @@ json_add_boolean() { _json_add_generic boolean "$1" "$2" } +json_add_double() { + _json_add_generic double "$1" "$2" +} + # functions read access to json variables json_load() { @@ -193,12 +206,47 @@ json_dump() { } json_get_type() { - local dest="$1" - local cur + local __dest="$1" + local __cur - _json_get_var cur JSON_CUR - local var="${JSON_PREFIX}TYPE_${cur}_$2" - eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]" + _json_get_var __cur JSON_CUR + local __var="${JSON_PREFIX}TYPE_${__cur}_${2//[^a-zA-Z0-9_]/_}" + eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" +} + +json_get_keys() { + local __dest="$1" + local _tbl_cur + + 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() {