_json_inc() {
local _var="$1"
local _dest="$2"
- local seq
+ local _seq
- _json_get_var seq "$_var"
- seq="$((${seq:-0} + 1))"
- _json_set_var "$_var" "$seq"
- [ -n "$dest" ] && _set_var "$_dest" "$seq"
+ _json_get_var _seq "$_var"
+ _seq="$((${_seq:-0} + 1))"
+ _json_set_var "$_var" "$_seq"
+ [ -n "$_dest" ] && _set_var "$_dest" "$_seq"
}
_json_stack_push() {
local type="$2"
local itype="$3"
local cur new_cur
+ local seq
_json_get_var cur JSON_CUR
_json_inc JSON_SEQ seq
local table="JSON_$itype$seq"
_json_export "UP_$table" "$cur"
_json_export "KEYS_$table" ""
- [ "$TYPE" = "ARRAY" ] && _json_export "KEYS_$table" ""
+ [ "$itype" = "ARRAY" ] && _json_export "SEQ_$table" ""
_json_stack_push "$table"
_json_get_var new_cur JSON_CUR
- _json_add_generic object "$1" "$new_cur" "$cur"
+ _json_add_generic "$type" "$1" "$new_cur" "$cur"
}
_json_close_table() {
local stack new_stack
_json_get_var stack JSON_STACK
- _json_set_var cur "${JSON_STACK##* }"
+ _json_set_var JSON_CUR "${stack##* }"
new_stack="${stack% *}"
[[ "$stack" == "$new_stack" ]] && new_stack=
_json_set_var JSON_STACK "$new_stack"
}
json_get_type() {
- local dest="$1"
- local cur
+ local __dest="$1"
+ local __cur
- _json_get_var cur JSON_CUR
- local var="${JSON_PREFIX}TYPE_${cur}_$2"
- eval "export -- \"$dest=\${$var}\"; [ -n \"\${$var+x}\" ]"
+ _json_get_var __cur JSON_CUR
+ local __var="${JSON_PREFIX}TYPE_${__cur}_${2//[^a-zA-Z0-9_]/_}"
+ eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]"
}
json_get_var() {
json_select() {
local target="$1"
local type
+ local cur
[ -z "$1" ] && {
_json_set_var JSON_CUR "JSON_VAR"
return 0
}
[[ "$1" == ".." ]] && {
- eval "JSON_CUR=\"\${UP_$JSON_CUR}\""
+ _json_get_var cur JSON_CUR
+ _json_get_var cur "UP_$cur"
+ _json_set_var JSON_CUR "$cur"
return 0
}
json_get_type type "$target"
case "$type" in
object|array)
- _json_get_var JSON_CUR "$target"
+ json_get_var cur "$target"
+ _json_set_var JSON_CUR "$cur"
;;
*)
echo "WARNING: Variable '$target' does not exist or is not an array/object"
;;
esac
}
+
+json_is_a() {
+ local type
+
+ json_get_type type "$1"
+ [ "$type" = "$2" ]
+}