7389f8c2a26ab743b7fb1aeb24f2c27be6138a6b
1 # functions for parsing and generating json
8 eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
12 [ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET"
13 export -- JSON_SEQ
=0 JSON_STACK
= JSON_CUR
="JSON_VAR" JSON_UNSET
="" KEYS_JSON_VAR
= TYPE_JSON_VAR
=
20 local cur
="${4:-$JSON_CUR}"
22 [ "${cur%%[0-9]*}" = "JSON_ARRAY" ] && {
23 eval "local aseq=\"\${SEQ_$cur}\""
24 var
=$
(( ${aseq:-0} + 1 ))
25 export -- "SEQ_$cur=$var"
28 export -- "${cur}_$var=$val"
29 export -- "TYPE_${cur}_$var=$type"
30 jshn_append JSON_UNSET
"${cur}_$var TYPE_${cur}_$var"
31 jshn_append
"KEYS_${cur}" "$var"
36 JSON_SEQ
=$
(($JSON_SEQ + 1))
37 jshn_append JSON_STACK
"$JSON_CUR"
38 local table
="JSON_$TYPE$JSON_SEQ"
39 export -- "UP_$table=$JSON_CUR"
40 export -- "KEYS_$table="
41 jshn_append JSON_UNSET
"KEYS_$table UP_$table"
42 [ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET
"SEQ_$table"
49 json_add_generic object
"$1" "$JSON_CUR" "$cur"
53 local oldstack
="$JSON_STACK"
54 JSON_CUR
="${JSON_STACK##* }"
55 JSON_STACK
="${JSON_STACK% *}"
56 [[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK
=
62 json_add_generic array
"$1" "$JSON_CUR" "$cur"
70 json_add_generic string
"$1" "$2"
74 json_add_generic int
"$1" "$2"
78 json_add_generic boolean
"$1" "$2"
81 # functions read access to json variables
94 eval "export -- \"$dest=\${TYPE_${JSON_CUR}_$var}\""
100 eval "export -- \"$dest=\${${JSON_CUR}_$var}\""
111 [[ "$1" == ".." ]] && {
112 eval "JSON_CUR=\"\${UP_$JSON_CUR}\""
115 json_get_type
type "$target"
118 json_get_var JSON_CUR
"$target"
121 echo "WARNING: Variable '$target' does not exist or is not an array/object"