nutools/lib/ulib/json

46 lines
1.2 KiB
Bash

##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## gestion de contenu au format json
##@cooked nocomments
##@require base
uprovide json
urequire base
function json_filter() {
# traiter un flux json pour que chaque valeur soit sur une ligne séparée,
# facilitant le traitement par un script bash
awk '{
gsub(/}\][ ]*$/, "\n&")
gsub(/},[ ]*$/, "\n&")
gsub(/[][{]/, "&\n")
gsub(/,[ ]*"/, ",\n\"")
gsub(/"}/, "\",\n}")
print
}'
}
JSONFUNCS='
function json_get(line) {
if (!line) line = $0
match(line, /^[^"]*"[^"]*":[ ]*"?/); line = substr(line, RSTART + RLENGTH)
match(line, /"?[ ]*,?[ ]*$/); line = substr(line, 1, RSTART - 1)
return line
}
'
function awkjson() {
json_filter | awkrun -f "$JSONFUNCS" "$@"
}
function json_get() { php_json_get "$@"; }
function json_each() { php_json_each "$@"; }
function json_build() { php_json_build "$@"; }
function php_json_get() {
"$ULIBDIR/support/php/json_get.php" "$@"
}
function php_json_each() {
"$ULIBDIR/support/php/json_each.php" "$@"
}
function php_json_build() {
"$ULIBDIR/support/php/json_build.php" "$@"
}