2011-05-23 22:21:27 +02:00
|
|
|
# functions for parsing and generating json
|
|
|
|
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_get_var() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# dest=$1
|
|
|
|
# var=$2
|
|
|
|
eval "$1=\"\$${JSON_PREFIX}$2\""
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
2011-05-23 22:21:27 +02:00
|
|
|
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_set_var() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
2012-12-16 17:46:02 +01:00
|
|
|
local ___val="$2"
|
2013-10-30 11:36:46 +01:00
|
|
|
eval "${JSON_PREFIX}$1=\"\$___val\""
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 14:59:47 +01:00
|
|
|
__jshn_raw_append() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
2013-10-29 14:59:47 +01:00
|
|
|
local value="$2"
|
|
|
|
local sep="${3:- }"
|
|
|
|
|
2013-10-30 11:36:46 +01:00
|
|
|
eval "export -- \"$1=\${$1:+\${$1}\${value:+\$sep}}\$value\""
|
2013-10-29 14:59:47 +01:00
|
|
|
}
|
|
|
|
|
2012-12-16 17:46:02 +01:00
|
|
|
_jshn_append() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
2013-10-30 11:50:39 +01:00
|
|
|
local _a_value="$2"
|
2013-11-28 17:51:26 +01:00
|
|
|
eval "${JSON_PREFIX}$1=\"\${${JSON_PREFIX}$1} \$_a_value\""
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_get_var() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
|
|
|
# value=$2
|
|
|
|
eval "$1=\"\$$2\""
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_var() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
2012-12-16 17:46:02 +01:00
|
|
|
local __val="$2"
|
2013-10-30 11:36:46 +01:00
|
|
|
eval "$1=\"\$__val\""
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_json_inc() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# var=$1
|
|
|
|
# dest=$2
|
2012-12-16 17:46:02 +01:00
|
|
|
|
2013-11-28 17:51:26 +01:00
|
|
|
eval "${JSON_PREFIX}$1=\$(( \${${JSON_PREFIX}$1:-0} + 1))${2:+; $2=\"\$${JSON_PREFIX}$1\"}"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_add_generic() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# type=$1
|
2013-11-28 17:51:26 +01:00
|
|
|
# name=$2
|
2013-10-30 11:36:46 +01:00
|
|
|
# value=$3
|
2013-11-28 17:51:26 +01:00
|
|
|
# cur=$4
|
2011-05-23 22:21:27 +02:00
|
|
|
|
2013-11-28 17:51:26 +01:00
|
|
|
local var
|
|
|
|
if [ "${4%%[0-9]*}" = "JSON_ARRAY" ]; then
|
|
|
|
_json_inc "SEQ_$4" var
|
2011-09-11 11:40:22 +02:00
|
|
|
else
|
2013-11-28 17:51:26 +01:00
|
|
|
local name="${2//[^a-zA-Z0-9_]/_}"
|
|
|
|
[[ "$name" == "$2" ]] || export -- "${JSON_PREFIX}NAME_${4}_${name}=$2"
|
2011-09-11 11:40:22 +02:00
|
|
|
var="$name"
|
|
|
|
fi
|
2011-08-11 23:29:23 +02:00
|
|
|
|
2013-10-30 11:36:46 +01:00
|
|
|
local cur_var=
|
2013-11-28 17:51:26 +01:00
|
|
|
export -- \
|
|
|
|
"${JSON_PREFIX}${4}_$var=$3" \
|
|
|
|
"${JSON_PREFIX}TYPE_${4}_$var=$1"
|
|
|
|
_jshn_append "JSON_UNSET" "${4}_$var"
|
|
|
|
_jshn_append "KEYS_$4" "$var"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_add_table() {
|
2013-10-30 11:36:46 +01:00
|
|
|
# name=$1
|
|
|
|
# type=$2
|
|
|
|
# itype=$3
|
2013-11-28 17:51:26 +01:00
|
|
|
local cur seq
|
2012-12-16 17:46:02 +01:00
|
|
|
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_inc JSON_SEQ seq
|
|
|
|
|
2013-10-30 11:36:46 +01:00
|
|
|
local table="JSON_$3$seq"
|
2013-10-30 10:34:40 +01:00
|
|
|
_json_set_var "UP_$table" "$cur"
|
2013-11-28 17:51:26 +01:00
|
|
|
export -- "${JSON_PREFIX}KEYS_$table="
|
|
|
|
unset "${JSON_PREFIX}SEQ_$table"
|
2013-10-30 10:34:40 +01:00
|
|
|
_json_set_var JSON_CUR "$table"
|
2013-10-30 10:19:52 +01:00
|
|
|
_jshn_append "JSON_UNSET" "$table"
|
2012-12-16 17:46:02 +01:00
|
|
|
|
2013-11-28 17:51:26 +01:00
|
|
|
_json_add_generic "$2" "$1" "$table" "$cur"
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_json_close_table() {
|
2013-11-28 17:51:26 +01:00
|
|
|
local _s_cur
|
2012-12-16 17:46:02 +01:00
|
|
|
|
2013-10-30 10:34:40 +01:00
|
|
|
_json_get_var _s_cur JSON_CUR
|
2013-11-28 17:51:26 +01:00
|
|
|
_json_get_var "${JSON_PREFIX}JSON_CUR" "UP_$_s_cur"
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json_set_namespace() {
|
|
|
|
local _new="$1"
|
|
|
|
local _old="$2"
|
|
|
|
|
|
|
|
[ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX"
|
|
|
|
JSON_PREFIX="$_new"
|
|
|
|
}
|
|
|
|
|
|
|
|
json_cleanup() {
|
|
|
|
local unset
|
|
|
|
|
|
|
|
_json_get_var unset JSON_UNSET
|
2013-10-30 10:19:52 +01:00
|
|
|
for tmp in $unset JSON_VAR; do
|
|
|
|
unset \
|
|
|
|
${JSON_PREFIX}UP_$tmp \
|
|
|
|
${JSON_PREFIX}KEYS_$tmp \
|
|
|
|
${JSON_PREFIX}SEQ_$tmp \
|
|
|
|
${JSON_PREFIX}TYPE_$tmp \
|
|
|
|
${JSON_PREFIX}NAME_$tmp \
|
|
|
|
${JSON_PREFIX}$tmp
|
|
|
|
done
|
2012-12-16 17:46:02 +01:00
|
|
|
|
|
|
|
unset \
|
|
|
|
${JSON_PREFIX}JSON_SEQ \
|
|
|
|
${JSON_PREFIX}JSON_CUR \
|
2013-10-30 10:19:52 +01:00
|
|
|
${JSON_PREFIX}JSON_UNSET
|
2012-12-16 17:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json_init() {
|
|
|
|
json_cleanup
|
|
|
|
export -- \
|
|
|
|
${JSON_PREFIX}JSON_SEQ=0 \
|
|
|
|
${JSON_PREFIX}JSON_CUR="JSON_VAR" \
|
|
|
|
${JSON_PREFIX}KEYS_JSON_VAR= \
|
|
|
|
${JSON_PREFIX}TYPE_JSON_VAR=
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_object() {
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_add_table "$1" object TABLE
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_close_object() {
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_close_table
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_array() {
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_add_table "$1" array ARRAY
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_close_array() {
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_close_table
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_string() {
|
2013-11-28 17:51:26 +01:00
|
|
|
local cur
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_add_generic string "$1" "$2" "$cur"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_int() {
|
2013-11-28 17:51:26 +01:00
|
|
|
local cur
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_add_generic int "$1" "$2" "$cur"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_boolean() {
|
2013-11-28 17:51:26 +01:00
|
|
|
local cur
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_add_generic boolean "$1" "$2" "$cur"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2013-03-17 00:50:03 +01:00
|
|
|
json_add_double() {
|
2013-11-28 17:51:26 +01:00
|
|
|
local cur
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_add_generic double "$1" "$2" "$cur"
|
2013-03-17 00:50:03 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 22:21:27 +02:00
|
|
|
# functions read access to json variables
|
|
|
|
|
|
|
|
json_load() {
|
|
|
|
eval `jshn -r "$1"`
|
|
|
|
}
|
|
|
|
|
|
|
|
json_dump() {
|
2012-12-16 17:46:02 +01:00
|
|
|
jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_get_type() {
|
2013-01-08 10:48:01 +01:00
|
|
|
local __dest="$1"
|
|
|
|
local __cur
|
2012-12-16 17:46:02 +01:00
|
|
|
|
2013-01-08 10:48:01 +01:00
|
|
|
_json_get_var __cur JSON_CUR
|
|
|
|
local __var="${JSON_PREFIX}TYPE_${__cur}_${2//[^a-zA-Z0-9_]/_}"
|
|
|
|
eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:00:42 +02:00
|
|
|
json_get_keys() {
|
|
|
|
local __dest="$1"
|
|
|
|
local _tbl_cur
|
|
|
|
|
2013-10-29 15:11:42 +01:00
|
|
|
if [ -n "$2" ]; then
|
|
|
|
json_get_var _tbl_cur "$2"
|
|
|
|
else
|
|
|
|
_json_get_var _tbl_cur JSON_CUR
|
|
|
|
fi
|
2013-10-20 00:00:42 +02:00
|
|
|
local __var="${JSON_PREFIX}KEYS_${_tbl_cur}"
|
|
|
|
eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]"
|
|
|
|
}
|
|
|
|
|
2013-10-29 14:59:47 +01:00
|
|
|
json_get_values() {
|
|
|
|
local _v_dest="$1"
|
2013-10-29 15:11:42 +01:00
|
|
|
local _v_keys _v_val _select=
|
2014-02-02 15:22:23 +01:00
|
|
|
local _json_no_warning=1
|
2013-10-29 14:59:47 +01:00
|
|
|
|
|
|
|
unset "$_v_dest"
|
2013-10-29 15:11:42 +01:00
|
|
|
[ -n "$2" ] && {
|
2013-12-02 10:56:54 +01:00
|
|
|
json_select "$2" || return 1
|
2013-10-29 15:11:42 +01:00
|
|
|
_select=1
|
|
|
|
}
|
|
|
|
|
|
|
|
json_get_keys _v_keys
|
2013-10-29 14:59:47 +01:00
|
|
|
set -- $_v_keys
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
|
|
json_get_var _v_val "$1"
|
|
|
|
__jshn_raw_append "$_v_dest" "$_v_val"
|
|
|
|
shift
|
|
|
|
done
|
2013-10-29 15:11:42 +01:00
|
|
|
[ -n "$_select" ] && json_select ..
|
|
|
|
|
|
|
|
return 0
|
2013-10-29 14:59:47 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 22:21:27 +02:00
|
|
|
json_get_var() {
|
2012-12-16 17:46:02 +01:00
|
|
|
local __dest="$1"
|
|
|
|
local __cur
|
|
|
|
|
|
|
|
_json_get_var __cur JSON_CUR
|
|
|
|
local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}"
|
2013-12-10 18:30:15 +01:00
|
|
|
eval "export -- \"$__dest=\${$__var:-$3}\"; [ -n \"\${$__var+x}\${3+x}\" ]"
|
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
|
2013-12-10 18:30:15 +01:00
|
|
|
if [ "$_var" != "${_var#*:}" ]; then
|
|
|
|
json_get_var "${_var%%:*}" "${_var%%:*}" "${_var#*:}"
|
|
|
|
else
|
|
|
|
json_get_var "$_var" "$_var"
|
|
|
|
fi
|
2012-05-14 00:21:12 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-23 22:21:27 +02:00
|
|
|
json_select() {
|
|
|
|
local target="$1"
|
|
|
|
local type
|
2012-12-16 18:04:14 +01:00
|
|
|
local cur
|
2011-05-23 22:21:27 +02:00
|
|
|
|
|
|
|
[ -z "$1" ] && {
|
2012-12-16 17:46:02 +01:00
|
|
|
_json_set_var JSON_CUR "JSON_VAR"
|
2012-05-29 02:06:39 +02:00
|
|
|
return 0
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
[[ "$1" == ".." ]] && {
|
2012-12-16 18:04:14 +01:00
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_get_var cur "UP_$cur"
|
|
|
|
_json_set_var JSON_CUR "$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)
|
2012-12-16 18:04:14 +01:00
|
|
|
json_get_var cur "$target"
|
|
|
|
_json_set_var JSON_CUR "$cur"
|
2011-05-23 22:21:27 +02:00
|
|
|
;;
|
|
|
|
*)
|
2014-02-02 15:22:23 +01:00
|
|
|
[ -n "$_json_no_warning" ] || \
|
|
|
|
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
|
|
|
|
}
|
2012-12-17 13:54:45 +01:00
|
|
|
|
|
|
|
json_is_a() {
|
|
|
|
local type
|
|
|
|
|
|
|
|
json_get_type type "$1"
|
|
|
|
[ "$type" = "$2" ]
|
|
|
|
}
|