App-git-hub
view release on metacpan or search on metacpan
share/lib/git-hub.d/json.bash view on Meta::CPAN
2)
JSON.del "$1"
printf "$1\t$2\n"
;;
3)
if [ "$1" == '-' ]; then
echo "$JSON__cache" | JSON.del "$1"
printf "$1\t$2\n"
else
echo ${!3} | JSON.del "$1"
printf "$1\t$2\n"
fi
;;
*) JSON.die 'Usage: JSON.put [-s|-n|-b|-z] <key-path> <new-value> [<tree-var>]' ;;
esac
}
JSON.del() {
set -o pipefail
case $# in
1)
grep -Ev "$1 "
;;
2)
if [ "$1" == '-' ]; then
echo "$JSON__cache" | grep -Ev "$1 "
else
echo ${!1} | grep -Ev "$1 "
fi
;;
*) JSON.die 'Usage: JSON.get [-s|-n|-b|-z] <key-path> [<tree-var>]' ;;
esac
}
JSON.cache() {
case $# in
0)
echo -n "$JSON__cache"
;;
1)
printf -v "$1" "%s" "$JSON__cache"
;;
*) JSON.die 'Usage: JSON.cache [<tree-var>]' ;;
esac
}
#-----------------------------------------------------------------------------
JSON_CHR='[^"\\[:cntrl:]]'
JSON_ESC='(\\["\\/bfnrt]|\\u[0-9a-fA-F]{4})'
JSON_STR="\"($JSON_CHR|$JSON_ESC)*\""
JSON_NUM='-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?'
JSON_BOOL='null|false|true'
JSON_SPACE='[[:space:]]+'
JSON_PUNCT='[][{}:,]'
JSON_OTHER='.'
JSON_SCALAR="^($JSON_STR|$JSON_NUM|$JSON_BOOL)$"
JSON_TOKEN="$JSON_STR|$JSON_NUM|$JSON_BOOL|$JSON_PUNCT|$JSON_SPACE|$JSON_OTHER"
JSON.lex() {
local GREP_COLORS GREP_COLOR
\grep -Eo "$JSON_TOKEN" | \grep -Ev "^$JSON_SPACE$"
}
JSON.parse() {
read -r JSON_token
case "$JSON_token" in
'{') JSON.parse-object '' ;;
'[') JSON.parse-array '' ;;
*) JSON.parse-error "'{' or '['";;
esac
}
JSON.parse-object() {
read -r JSON_token
while [ $JSON_token != '}' ]; do
[[ $JSON_token =~ ^\" ]] || JSON.parse-error STRING #"
local key="${JSON_token:1:$((${#JSON_token}-2))}"
read -r JSON_token
[ $JSON_token == ':' ] || JSON.parse-error "':'"
read -r JSON_token
JSON.parse-value "$1/$key"
read -r JSON_token
if [ $JSON_token == ',' ]; then
read -r JSON_token
else
[ $JSON_token == '}' ] || JSON.parse-error "'}'"
fi
done
}
JSON.parse-array() {
local index=0
read -r JSON_token
while [ $JSON_token != ']' ]; do
JSON.parse-value "$1/$((index++))"
read -r JSON_token
if [ $JSON_token == ',' ]; then
read -r JSON_token
else
[ $JSON_token == ']' ] || JSON.parse-error "']'"
fi
done
}
JSON.parse-value() {
case "$JSON_token" in
'[') JSON.parse-array "$1";;
'{') JSON.parse-object "$1";;
*)
[[ "$JSON_token" =~ $JSON_SCALAR ]] ||
JSON.parse-error
printf "%s\t%s\n" "$1" "$JSON_token"
esac
}
JSON.parse-error() {
msg="JSON.parse error. Unexpected token: '$JSON_token'."
[ -n "$1" ] && msg+=" Expected: $1."
JSON.die "$msg"
}
( run in 0.781 second using v1.01-cache-2.11-cpan-437f7b0c052 )