mirror of
https://github.com/myvesta/vesta
synced 2025-08-20 21:34:12 -07:00
Created few test cases
This commit is contained in:
parent
87fcc5d83b
commit
4e926e746f
3 changed files with 264 additions and 0 deletions
105
test/json.sh
Executable file
105
test/json.sh
Executable file
|
@ -0,0 +1,105 @@
|
|||
# https://github.com/dominictarr/JSON.sh
|
||||
# Code is licensed under the MIT license
|
||||
# Copyright (c) 2011 Dominic Tarr
|
||||
|
||||
throw () {
|
||||
echo "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
tokenize () {
|
||||
local ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
|
||||
local CHAR='[^[:cntrl:]"\\]'
|
||||
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
|
||||
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
|
||||
local KEYWORD='null|false|true'
|
||||
local SPACE='[[:space:]]+'
|
||||
egrep -ao "$STRING|$NUMBER|$KEYWORD|$SPACE|." --color=never |
|
||||
egrep -v "^$SPACE$" # eat whitespace
|
||||
}
|
||||
|
||||
parse_array () {
|
||||
local index=0
|
||||
local ary=''
|
||||
read -r token
|
||||
case "$token" in
|
||||
']') ;;
|
||||
*)
|
||||
while :
|
||||
do
|
||||
parse_value "$1" "$index"
|
||||
let index=$index+1
|
||||
ary="$ary""$value"
|
||||
read -r token
|
||||
case "$token" in
|
||||
']') break ;;
|
||||
',') ary="$ary," ;;
|
||||
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
|
||||
esac
|
||||
read -r token
|
||||
done
|
||||
;;
|
||||
esac
|
||||
value=`printf '[%s]' $ary`
|
||||
}
|
||||
|
||||
parse_object () {
|
||||
local key
|
||||
local obj=''
|
||||
read -r token
|
||||
case "$token" in
|
||||
'}') ;;
|
||||
*)
|
||||
while :
|
||||
do
|
||||
case "$token" in
|
||||
'"'*'"') key=$token ;;
|
||||
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
|
||||
esac
|
||||
read -r token
|
||||
case "$token" in
|
||||
':') ;;
|
||||
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
|
||||
esac
|
||||
read -r token
|
||||
parse_value "$1" "$key"
|
||||
obj="$obj$key:$value"
|
||||
read -r token
|
||||
case "$token" in
|
||||
'}') break ;;
|
||||
',') obj="$obj," ;;
|
||||
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
|
||||
esac
|
||||
read -r token
|
||||
done
|
||||
;;
|
||||
esac
|
||||
value=`printf '{%s}' "$obj"`
|
||||
}
|
||||
|
||||
parse_value () {
|
||||
local jpath="${1:+$1,}$2"
|
||||
case "$token" in
|
||||
'{') parse_object "$jpath" ;;
|
||||
'[') parse_array "$jpath" ;;
|
||||
# At this point, the only valid single-character tokens are digits.
|
||||
''|[^0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
|
||||
*) value=$token ;;
|
||||
esac
|
||||
printf "[%s]\t%s\n" "$jpath" "$value"
|
||||
}
|
||||
|
||||
parse () {
|
||||
read -r token
|
||||
parse_value
|
||||
read -r token
|
||||
case "$token" in
|
||||
'') ;;
|
||||
*) throw "EXPECTED EOF GOT $token" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
if [ $0 = $BASH_SOURCE ];
|
||||
then
|
||||
tokenize | parse
|
||||
fi
|
Loading…
Add table
Add a link
Reference in a new issue