X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=sh%2Fjshn.sh;h=7f922c4f41a3a62f0ebcef421b4bcf6e62a8c59c;hp=5bd38968b14704d5a08de08b44b0de1eeb06785d;hb=94ff2a0abb2938dc84cc46e513f0d039cacbe40a;hpb=cc2457aa3ee13b155cb095637ab0ee77c366e7d2 diff --git a/sh/jshn.sh b/sh/jshn.sh index 5bd3896..7f922c4 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -10,7 +10,7 @@ jshn_append() { json_init() { [ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET" - export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET= + export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET="" KEYS_JSON_VAR= TYPE_JSON_VAR= } json_add_generic() { @@ -19,6 +19,16 @@ json_add_generic() { local val="$3" local cur="${4:-$JSON_CUR}" + if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then + eval "local aseq=\"\${SEQ_$cur}\"" + var=$(( ${aseq:-0} + 1 )) + export -- "SEQ_$cur=$var" + else + local name="$(echo -n "$var" | tr -C '[a-zA-Z0-9_]' _)" + [[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var" + var="$name" + fi + export -- "${cur}_$var=$val" export -- "TYPE_${cur}_$var=$type" jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var" @@ -26,22 +36,25 @@ json_add_generic() { } json_add_table() { + local TYPE="$1" JSON_SEQ=$(($JSON_SEQ + 1)) jshn_append JSON_STACK "$JSON_CUR" - local table="JSON_TABLE$JSON_SEQ" + local table="JSON_$TYPE$JSON_SEQ" export -- "UP_$table=$JSON_CUR" + export -- "KEYS_$table=" + jshn_append JSON_UNSET "KEYS_$table UP_$table" + [ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET "SEQ_$table" JSON_CUR="$table" } json_add_object() { local cur="$JSON_CUR" - json_add_table + json_add_table TABLE json_add_generic object "$1" "$JSON_CUR" "$cur" } json_close_object() { local oldstack="$JSON_STACK" - export "KEYS_${JSON_CUR}" JSON_CUR="${JSON_STACK##* }" JSON_STACK="${JSON_STACK% *}" [[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK= @@ -49,7 +62,7 @@ json_close_object() { json_add_array() { local cur="$JSON_CUR" - json_add_table + json_add_table ARRAY json_add_generic array "$1" "$JSON_CUR" "$cur" } @@ -76,7 +89,7 @@ json_load() { } json_dump() { - jshn -w + jshn "$@" -w } json_get_type() { @@ -87,10 +100,17 @@ json_get_type() { json_get_var() { local dest="$1" - local var="$2" + local var="$(echo -n "$2" | tr -C '[a-zA-Z0-9_]' _)" eval "export -- \"$dest=\${${JSON_CUR}_$var}\"" } +json_get_vars() { + while [ "$#" -gt 0 ]; do + local _var="$1"; shift + json_get_var "$_var" "$_var" + done +} + json_select() { local target="$1" local type