File: //usr/syno/bin/jsoncmd
jget()
#1: json string
#2: filter
{
local jtext=$1
local filter=$2
local jval=`echo ${jtext}| /usr/bin/jq -e "${filter}"`
if [ $? -ne 0 ];then
echo ${jval} //dump error
return 1; #error
fi
#for string ==> trim prefix/suffix double quot
jval=`echo ${jval} | sed -e 's/^"//' -e 's/"$//'`
echo ${jval}
return 0
}
jfilter()
#1: json string
#2: filter
{
local jtext=$1
local filter=$2
local jval=`echo ${jtext}| /usr/bin/jq -e "${filter}"`
if [ $? -ne 0 ];then
echo ${jval} //dump error
return 1; #error
fi
echo ${jval}
return 0
}
check_type()
#1: val
#2: type
{
echo $1 | /usr/bin/jq -e "${2}" >/dev/null 2>&1
if [ $? -eq 0 ];then
echo 1
else
echo 0
fi
}
is_str()
#1: value
{
local chk_bool=$(check_type $1 "booleans")
local chk_num=$(check_type $1 "numbers")
local chk_obj=$(check_type $1 "objects")
local chk_array=$(check_type $1 "arrays")
local chk_null=$(check_type $1 "nulls")
if [ ${chk_bool} -eq 0 -a ${chk_num} -eq 0 -a ${chk_obj} -eq 0 -a ${chk_array} -eq 0 -a ${chk_null} -eq 0 ];then
echo 1
else
echo 0
fi
}
jout_begin()
{
echo "{" > ${SYNOPKG_BKP_OUTPUT_PATH}
}
jout_end()
{
echo "\"jout_end_key\": 0}" >> ${SYNOPKG_BKP_OUTPUT_PATH}
}
joutstr()
#1: key
#2: value
{
echo "\"$1\": \"$2\"," >> ${SYNOPKG_BKP_OUTPUT_PATH}
}
jout()
#1: key
#2: value
{
echo "\"$1\": $2," >> ${SYNOPKG_BKP_OUTPUT_PATH}
}
jdone()
{
jout_begin
jout "result" true
jout_end
}
jerr()
#1: error message
{
jout_begin
joutstr "err_msg" "$1"
jout "result" false
jout_end
}
jerrstr()
#1: string section
#2: string key
{
jout_begin
jout "err_msg" "{\"section\": \"$1\", \"key\": \"$2\"}"
joutstr "result" false
jout_end
}
jsize()
#1: json array
{
local jtext=$1
local size=0
echo ${jtext} | /usr/bin/jq -e "strings" >/dev/null 2>&1
local str_chk=$?
echo ${jtext} | /usr/bin/jq -e "arrays" >/dev/null 2>&1
local array_chk=$?
if [ ${str_chk} -ne 0 -a ${array_chk} -ne 0 ]; then
echo "input should be string or array" //dump error
return 1; #error
fi
size=`echo ${jtext} | /usr/bin/jq "length"`
echo ${size}
return 0
}
is_number()
{
ret=`echo $1 | awk '$0 ~/[^0-9]/ { print "no" }'`
if [ "x${ret}" = "xno" ];then
return 0
else
return 1
fi
}
jversion_compare()
#compare major version
#1: version1
#2: version2
#@return:
# - 1: major version1 > major version2 (ex: 2.3 > 1.9)
# - 0: major version1 == major version2 (ex: 1.7 == 1.0)
# - 2: major version1 < major version2 (ex: 1.7 < 2.1)
# - 3: error
{
local major1=`echo $1 | /usr/bin/cut -d"." -f1`
local minor1=`echo $1 | /usr/bin/cut -d"." -f2`
local major2=`echo $2 | /usr/bin/cut -d"." -f1`
local minor2=`echo $2 | /usr/bin/cut -d"." -f2`
$(is_number ${major1})
if [ $? -eq 0 ];then
return 3
fi
$(is_number ${major2})
if [ $? -eq 0 ];then
return 3
fi
$(is_number ${minor1})
if [ $? -eq 0 ];then
return 3
fi
$(is_number ${minor2})
if [ $? -eq 0 ];then
return 3
fi
if [ $major1 -gt $major2 ];then
return 1
elif [ $major1 -lt $major2 ];then
return 2
elif [ $major1 -eq $major2 ];then
return 0
fi
return 3 #error
}