summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@nbd.name>2022-09-27 14:17:51 +0200
committerFelix Fietkau <nbd@nbd.name>2022-09-27 14:17:52 +0200
commitea56013409d5823001b47a9bba6f74055a6d76a5 (patch)
tree7e47fe657d6ab889667a77f6c03ace62c4d2d077
parentd2223ef9da7172a84d1508733dc58840e1381e3c (diff)
downloadlibubox-ea56013409d5823001b47a9bba6f74055a6d76a5.tar.gz
jshn.sh: add json_add_fields function for adding multiple fields at once
This simplifies passing extra object data as a function parameter Signed-off-by: Felix Fietkau <nbd@nbd.name>
-rw-r--r--sh/jshn.sh23
1 files changed, 23 insertions, 0 deletions
diff --git a/sh/jshn.sh b/sh/jshn.sh
index 7b0155d..c973692 100644
--- a/sh/jshn.sh
+++ b/sh/jshn.sh
@@ -174,6 +174,29 @@ json_add_null() {
_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_load() {