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
|
|
|
|
2014-11-23 19:25:39 +01:00
|
|
|
let "${JSON_PREFIX}$1 += 1" "$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
|
2014-11-23 20:02:33 +01:00
|
|
|
if [ "${4%%[0-9]*}" = "J_A" ]; then
|
|
|
|
_json_inc "S_$4" var
|
2011-09-11 11:40:22 +02:00
|
|
|
else
|
2014-11-23 20:02:33 +01:00
|
|
|
var="${2//[^a-zA-Z0-9_]/_}"
|
|
|
|
[[ "$var" == "$2" ]] || export -- "${JSON_PREFIX}N_${4}_${var}=$2"
|
2011-09-11 11:40:22 +02:00
|
|
|
fi
|
2011-08-11 23:29:23 +02:00
|
|
|
|
2013-11-28 17:51:26 +01:00
|
|
|
export -- \
|
|
|
|
"${JSON_PREFIX}${4}_$var=$3" \
|
2014-11-23 20:02:33 +01:00
|
|
|
"${JSON_PREFIX}T_${4}_$var=$1"
|
2013-11-28 17:51:26 +01:00
|
|
|
_jshn_append "JSON_UNSET" "${4}_$var"
|
2014-11-23 20:02:33 +01:00
|
|
|
_jshn_append "K_$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
|
|
|
|
|
2014-11-23 20:02:33 +01:00
|
|
|
local table="J_$3$seq"
|
|
|
|
_json_set_var "U_$table" "$cur"
|
|
|
|
export -- "${JSON_PREFIX}K_$table="
|
|
|
|
unset "${JSON_PREFIX}S_$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
|
2014-11-23 20:02:33 +01:00
|
|
|
_json_get_var "${JSON_PREFIX}JSON_CUR" "U_$_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() {
|
2014-04-20 22:55:48 +02:00
|
|
|
local unset tmp
|
2012-12-16 17:46:02 +01:00
|
|
|
|
|
|
|
_json_get_var unset JSON_UNSET
|
2014-11-23 20:21:14 +01:00
|
|
|
for tmp in $unset J_V; do
|
2013-10-30 10:19:52 +01:00
|
|
|
unset \
|
2014-11-23 20:02:33 +01:00
|
|
|
${JSON_PREFIX}U_$tmp \
|
|
|
|
${JSON_PREFIX}K_$tmp \
|
|
|
|
${JSON_PREFIX}S_$tmp \
|
|
|
|
${JSON_PREFIX}T_$tmp \
|
|
|
|
${JSON_PREFIX}N_$tmp \
|
2013-10-30 10:19:52 +01:00
|
|
|
${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
|
2014-11-23 19:44:10 +01:00
|
|
|
export -n ${JSON_PREFIX}JSON_SEQ=0
|
2012-12-16 17:46:02 +01:00
|
|
|
export -- \
|
2014-11-23 20:21:14 +01:00
|
|
|
${JSON_PREFIX}JSON_CUR="J_V" \
|
|
|
|
${JSON_PREFIX}K_J_V=
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_object() {
|
2014-11-23 20:02:33 +01:00
|
|
|
_json_add_table "$1" object T
|
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() {
|
2014-11-23 20:02:33 +01:00
|
|
|
_json_add_table "$1" array A
|
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"
|
2018-01-07 15:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json_add_null() {
|
|
|
|
local cur
|
|
|
|
_json_get_var cur JSON_CUR
|
|
|
|
_json_add_generic null "$1" "" "$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() {
|
2014-07-15 15:57:47 +02:00
|
|
|
eval "`jshn -r "$1"`"
|
2011-05-23 22:21:27 +02:00
|
|
|
}
|
|
|
|
|
2018-01-18 21:21:09 +01:00
|
|
|
json_load_file() {
|
|
|
|
eval "`jshn -R "$1"`"
|
|
|
|
}
|
|
|
|
|
2011-05-23 22:21:27 +02:00
|
|
|
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
|
2014-11-23 20:02:33 +01:00
|
|
|
local __var="${JSON_PREFIX}T_${__cur}_${2//[^a-zA-Z0-9_]/_}"
|
2013-01-08 10:48:01 +01:00
|
|
|
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
|
2014-11-23 20:02:33 +01:00
|
|
|
local __var="${JSON_PREFIX}K_${_tbl_cur}"
|
2013-10-20 00:00:42 +02:00
|
|
|
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" ] && {
|
2014-11-23 20:21:14 +01:00
|
|
|
_json_set_var JSON_CUR "J_V"
|
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
|
2014-11-23 20:02:33 +01:00
|
|
|
_json_get_var cur "U_$cur"
|
2012-12-16 18:04:14 +01:00
|
|
|
_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" ]
|
|
|
|
}
|
2018-02-01 16:41:27 +01:00
|
|
|
|
|
|
|
json_for_each_item() {
|
|
|
|
[ "$#" -ge 2 ] || return 0
|
|
|
|
local function="$1"; shift
|
|
|
|
local target="$1"; shift
|
|
|
|
local type val
|
|
|
|
|
|
|
|
json_get_type type "$target"
|
|
|
|
case "$type" in
|
|
|
|
object|array)
|
|
|
|
local keys key
|
|
|
|
json_select "$target"
|
|
|
|
json_get_keys keys
|
|
|
|
for key in $keys; do
|
|
|
|
json_get_var val "$key"
|
|
|
|
eval "$function \"\$val\" \"\$key\" \"\$@\""
|
|
|
|
done
|
|
|
|
json_select ..
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
json_get_var val "$target"
|
|
|
|
eval "$function \"\$val\" \"\" \"\$@\""
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|