2011-05-23 22:21:27 +02:00
|
|
|
# functions for parsing and generating json
|
|
|
|
|
2011-05-23 23:09:19 +02:00
|
|
|
jshn_append() {
|
2011-05-23 22:21:27 +02:00
|
|
|
local var="$1"
|
|
|
|
local value="$2"
|
|
|
|
local sep="${3:- }"
|
|
|
|
|
2011-05-23 23:09:19 +02:00
|
|
|
eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_init() {
|
|
|
|
[ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET"
|
2011-08-11 23:29:23 +02:00
|
|
|
export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET="" KEYS_JSON_VAR= TYPE_JSON_VAR=
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_generic() {
|
|
|
|
local type="$1"
|
|
|
|
local var="$2"
|
|
|
|
local val="$3"
|
|
|
|
local cur="${4:-$JSON_CUR}"
|
|
|
|
|
2011-09-11 11:40:22 +02:00
|
|
|
if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then
|
2011-08-11 23:29:23 +02:00
|
|
|
eval "local aseq=\"\${SEQ_$cur}\""
|
|
|
|
var=$(( ${aseq:-0} + 1 ))
|
|
|
|
export -- "SEQ_$cur=$var"
|
2011-09-11 11:40:22 +02:00
|
|
|
else
|
2012-05-29 02:06:39 +02:00
|
|
|
local name="${var//[^a-zA-Z0-9_]/_}"
|
2011-09-11 11:40:22 +02:00
|
|
|
[[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var"
|
|
|
|
var="$name"
|
|
|
|
fi
|
2011-08-11 23:29:23 +02:00
|
|
|
|
2011-05-23 23:09:19 +02:00
|
|
|
export -- "${cur}_$var=$val"
|
|
|
|
export -- "TYPE_${cur}_$var=$type"
|
|
|
|
jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var"
|
|
|
|
jshn_append "KEYS_${cur}" "$var"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_table() {
|
2011-08-11 23:29:23 +02:00
|
|
|
local TYPE="$1"
|
2011-05-23 22:21:27 +02:00
|
|
|
JSON_SEQ=$(($JSON_SEQ + 1))
|
2011-05-23 23:09:19 +02:00
|
|
|
jshn_append JSON_STACK "$JSON_CUR"
|
2011-08-11 23:29:23 +02:00
|
|
|
local table="JSON_$TYPE$JSON_SEQ"
|
2011-05-23 23:09:19 +02:00
|
|
|
export -- "UP_$table=$JSON_CUR"
|
2011-05-23 23:15:33 +02:00
|
|
|
export -- "KEYS_$table="
|
|
|
|
jshn_append JSON_UNSET "KEYS_$table UP_$table"
|
2011-08-11 23:29:23 +02:00
|
|
|
[ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET "SEQ_$table"
|
2011-05-23 22:21:27 +02:00
|
|
|
JSON_CUR="$table"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_object() {
|
|
|
|
local cur="$JSON_CUR"
|
2011-08-11 23:29:23 +02:00
|
|
|
json_add_table TABLE
|
2011-05-23 22:21:27 +02:00
|
|
|
json_add_generic object "$1" "$JSON_CUR" "$cur"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_close_object() {
|
|
|
|
local oldstack="$JSON_STACK"
|
|
|
|
JSON_CUR="${JSON_STACK##* }"
|
|
|
|
JSON_STACK="${JSON_STACK% *}"
|
|
|
|
[[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK=
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_array() {
|
|
|
|
local cur="$JSON_CUR"
|
2011-08-11 23:29:23 +02:00
|
|
|
json_add_table ARRAY
|
2011-05-23 22:21:27 +02:00
|
|
|
json_add_generic array "$1" "$JSON_CUR" "$cur"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_close_array() {
|
|
|
|
json_close_object
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_string() {
|
|
|
|
json_add_generic string "$1" "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_int() {
|
|
|
|
json_add_generic int "$1" "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_boolean() {
|
|
|
|
json_add_generic boolean "$1" "$2"
|
|
|
|
}
|
|
|
|
|
|
|
|
# functions read access to json variables
|
|
|
|
|
|
|
|
json_load() {
|
|
|
|
eval `jshn -r "$1"`
|
|
|
|
}
|
|
|
|
|
|
|
|
json_dump() {
|
2011-05-31 15:41:19 +02:00
|
|
|
jshn "$@" -w
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_get_type() {
|
|
|
|
local dest="$1"
|
2012-05-29 02:06:39 +02:00
|
|
|
local var="TYPE_${JSON_CUR}_$2"
|
|
|
|
eval "[ -n \"\${$var+x}\" ] && export -- \"$dest=\${$var}\""
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_get_var() {
|
|
|
|
local dest="$1"
|
2012-05-29 02:06:39 +02:00
|
|
|
local var="${JSON_CUR}_${2//[^a-zA-Z0-9_]/_}"
|
|
|
|
eval "[ -n \"\${$var+x}\" ] && export -- \"$dest=\${$var}\""
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 00:21:12 +02:00
|
|
|
json_get_vars() {
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
local _var="$1"; shift
|
|
|
|
json_get_var "$_var" "$_var"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-23 22:21:27 +02:00
|
|
|
json_select() {
|
|
|
|
local target="$1"
|
|
|
|
local type
|
|
|
|
|
|
|
|
[ -z "$1" ] && {
|
|
|
|
JSON_CUR="JSON_VAR"
|
2012-05-29 02:06:39 +02:00
|
|
|
return 0
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
[[ "$1" == ".." ]] && {
|
|
|
|
eval "JSON_CUR=\"\${UP_$JSON_CUR}\""
|
2012-05-29 02:06:39 +02:00
|
|
|
return 0
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
json_get_type type "$target"
|
|
|
|
case "$type" in
|
|
|
|
object|array)
|
|
|
|
json_get_var JSON_CUR "$target"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "WARNING: Variable '$target' does not exist or is not an array/object"
|
2012-05-29 02:06:39 +02:00
|
|
|
return 1
|
2011-05-23 22:21:27 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|