Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | # functions for parsing and generating json _json_get_var() { # dest=$1 # var=$2 eval "$1=\"\$${JSON_PREFIX}$2\"" } _json_set_var() { # var=$1 local ___val="$2" eval "${JSON_PREFIX}$1=\"\$___val\"" } __jshn_raw_append() { # var=$1 local value="$2" local sep="${3:- }" eval "export -- \"$1=\${$1:+\${$1}\${value:+\$sep}}\$value\"" } _jshn_append() { # var=$1 local _a_value="$2" eval "${JSON_PREFIX}$1=\"\${${JSON_PREFIX}$1} \$_a_value\"" } _get_var() { # var=$1 # value=$2 eval "$1=\"\$$2\"" } _set_var() { # var=$1 local __val="$2" eval "$1=\"\$__val\"" } _json_inc() { # var=$1 # dest=$2 let "${JSON_PREFIX}$1 += 1" "$2 = ${JSON_PREFIX}$1" } _json_add_generic() { # type=$1 # name=$2 # value=$3 # cur=$4 local var if [ "${4%%[0-9]*}" = "J_A" ]; then _json_inc "S_$4" var else var="${2//[^a-zA-Z0-9_]/_}" [[ "$var" == "$2" ]] || export -- "${JSON_PREFIX}N_${4}_${var}=$2" fi export -- \ "${JSON_PREFIX}${4}_$var=$3" \ "${JSON_PREFIX}T_${4}_$var=$1" _jshn_append "JSON_UNSET" "${4}_$var" _jshn_append "K_$4" "$var" } _json_add_table() { # name=$1 # type=$2 # itype=$3 local cur seq _json_get_var cur JSON_CUR _json_inc JSON_SEQ seq local table="J_$3$seq" _json_set_var "U_$table" "$cur" export -- "${JSON_PREFIX}K_$table=" unset "${JSON_PREFIX}S_$table" _json_set_var JSON_CUR "$table" _jshn_append "JSON_UNSET" "$table" _json_add_generic "$2" "$1" "$table" "$cur" } _json_close_table() { local _s_cur _json_get_var _s_cur JSON_CUR _json_get_var "${JSON_PREFIX}JSON_CUR" "U_$_s_cur" } json_set_namespace() { local _new="$1" local _old="$2" [ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX" JSON_PREFIX="$_new" } json_cleanup() { local unset tmp _json_get_var unset JSON_UNSET for tmp in $unset J_V; do unset \ ${JSON_PREFIX}U_$tmp \ ${JSON_PREFIX}K_$tmp \ ${JSON_PREFIX}S_$tmp \ ${JSON_PREFIX}T_$tmp \ ${JSON_PREFIX}N_$tmp \ ${JSON_PREFIX}$tmp done unset \ ${JSON_PREFIX}JSON_SEQ \ ${JSON_PREFIX}JSON_CUR \ ${JSON_PREFIX}JSON_UNSET } json_init() { json_cleanup export -n ${JSON_PREFIX}JSON_SEQ=0 export -- \ ${JSON_PREFIX}JSON_CUR="J_V" \ ${JSON_PREFIX}K_J_V= } json_add_object() { _json_add_table "$1" object T } json_close_object() { _json_close_table } json_add_array() { _json_add_table "$1" array A } json_close_array() { _json_close_table } json_add_string() { local cur _json_get_var cur JSON_CUR _json_add_generic string "$1" "$2" "$cur" } json_add_int() { local cur _json_get_var cur JSON_CUR _json_add_generic int "$1" "$2" "$cur" } json_add_boolean() { local cur _json_get_var cur JSON_CUR _json_add_generic boolean "$1" "$2" "$cur" } json_add_double() { local cur _json_get_var cur JSON_CUR _json_add_generic double "$1" "$2" "$cur" } json_add_null() { local cur _json_get_var cur JSON_CUR _json_add_generic null "$1" "" "$cur" } json_add_fields() { while [ "$#" -gt 0 ]; do local field="$1" shift local name="${field%%=*}" local val="${field#*=}" [ "$name" != "$val" ] || val="" local type="${name#*:}" [ "$type" != "$name" ] || type=string name="${name%%:*}" case "$type" in string|int|boolean|double) local cur _json_get_var cur JSON_CUR _json_add_generic "$type" "$name" "$val" "$cur" ;; esac done } # functions read access to json variables json_compact() { JSON_NONEWLINE=1 JSON_INDENT= } json_pretty() { JSON_NONEWLINE= JSON_INDENT=1 } json_load() { eval "`jshn -r "$1"`" } json_load_file() { eval "`jshn -R "$1"`" } json_dump() { jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} ${JSON_NONEWLINE:+-n} ${JSON_INDENT:+-i} -w } json_get_type() { local __dest="$1" local __cur _json_get_var __cur JSON_CUR local __var="${JSON_PREFIX}T_${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" } json_get_keys() { local __dest="$1" local _tbl_cur if [ -n "$2" ]; then json_get_var _tbl_cur "$2" else _json_get_var _tbl_cur JSON_CUR fi local __var="${JSON_PREFIX}K_${_tbl_cur}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" } json_get_values() { local _v_dest="$1" local _v_keys _v_val _select= local _json_no_warning=1 unset "$_v_dest" [ -n "$2" ] && { json_select "$2" || return 1 _select=1 } json_get_keys _v_keys set -- $_v_keys while [ "$#" -gt 0 ]; do json_get_var _v_val "$1" __jshn_raw_append "$_v_dest" "$_v_val" shift done [ -n "$_select" ] && json_select .. return 0 } json_get_var() { local __dest="$1" local __cur _json_get_var __cur JSON_CUR local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var:-$3}\"; [ -n \"\${$__var+x}\${3+x}\" ]" } json_get_vars() { while [ "$#" -gt 0 ]; do local _var="$1"; shift if [ "$_var" != "${_var#*:}" ]; then json_get_var "${_var%%:*}" "${_var%%:*}" "${_var#*:}" else json_get_var "$_var" "$_var" fi done } json_select() { local target="$1" local type local cur [ -z "$1" ] && { _json_set_var JSON_CUR "J_V" return 0 } [[ "$1" == ".." ]] && { _json_get_var cur JSON_CUR _json_get_var cur "U_$cur" _json_set_var JSON_CUR "$cur" return 0 } json_get_type type "$target" case "$type" in object|array) json_get_var cur "$target" _json_set_var JSON_CUR "$cur" ;; *) [ -n "$_json_no_warning" ] || \ echo "WARNING: Variable '$target' does not exist or is not an array/object" return 1 ;; esac } json_is_a() { local type json_get_type type "$1" [ "$type" = "$2" ] } 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 } |