summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Prindeville2023-09-15 18:35:31 +0000
committerRobert Marko2026-02-12 15:57:28 +0000
commit8c7b489daa029b3841584f6ac76e2a0b68d74ef3 (patch)
treedc9e061fc844387c0b5d215fe2c90d039f1cf547
parent5a65cb5a79b757d24beeab4144333b012a4935ec (diff)
downloadlibubox-8c7b489daa029b3841584f6ac76e2a0b68d74ef3.tar.gz
libubox: add anonymous strings, ints, et al in arrays
You can have a "name" in an array member without an associated value. Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com> Link: https://github.com/openwrt/libubox/pull/12 Signed-off-by: Robert Marko <robimarko@gmail.com>
-rw-r--r--sh/jshn.sh55
-rwxr-xr-xtests/shunit2/tests.sh25
2 files changed, 80 insertions, 0 deletions
diff --git a/sh/jshn.sh b/sh/jshn.sh
index 61a6a2e..68da325 100644
--- a/sh/jshn.sh
+++ b/sh/jshn.sh
@@ -150,30 +150,85 @@ json_add_string() {
_json_add_generic string "$1" "$2" "$cur"
}
+json_push_string() {
+ local cur
+ _json_get_var cur JSON_CUR
+ [ "${cur%%[0-9]*}" = "J_A" ] || {
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not in an array" >&2
+ return 1
+ }
+ _json_add_generic string "" "$1" "$cur"
+}
+
json_add_int() {
local cur
_json_get_var cur JSON_CUR
_json_add_generic int "$1" "$2" "$cur"
}
+json_push_int() {
+ local cur
+ _json_get_var cur JSON_CUR
+ [ "${cur%%[0-9]*}" = "J_A" ] || {
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not in an array" >&2
+ return 1
+ }
+ _json_add_generic int "" "$1" "$cur"
+}
+
json_add_boolean() {
local cur
_json_get_var cur JSON_CUR
_json_add_generic boolean "$1" "$2" "$cur"
}
+json_push_boolean() {
+ local cur
+ _json_get_var cur JSON_CUR
+ [ "${cur%%[0-9]*}" = "J_A" ] || {
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not in an array" >&2
+ return 1
+ }
+ _json_add_generic boolean "" "$1" "$cur"
+}
+
json_add_double() {
local cur
_json_get_var cur JSON_CUR
_json_add_generic double "$1" "$2" "$cur"
}
+json_push_double() {
+ local cur
+ _json_get_var cur JSON_CUR
+ [ "${cur%%[0-9]*}" = "J_A" ] || {
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not in an array" >&2
+ return 1
+ }
+ _json_add_generic double "" "$1" "$cur"
+}
+
json_add_null() {
local cur
_json_get_var cur JSON_CUR
_json_add_generic null "$1" "" "$cur"
}
+json_push_null() {
+ local cur
+ _json_get_var cur JSON_CUR
+ [ "${cur%%[0-9]*}" = "J_A" ] || {
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not in an array" >&2
+ return 1
+ }
+ _json_add_generic null "" "" "$cur"
+}
+
json_add_fields() {
while [ "$#" -gt 0 ]; do
local field="$1"
diff --git a/tests/shunit2/tests.sh b/tests/shunit2/tests.sh
index 21807ed..e2d8024 100755
--- a/tests/shunit2/tests.sh
+++ b/tests/shunit2/tests.sh
@@ -452,6 +452,31 @@ test_jshn_add_multi() {
set -u
}
+test_jshn_push_stuff() {
+ JSON_PREFIX="${JSON_PREFIX:-}"
+ . ../../sh/jshn.sh
+
+ set +u
+
+ # Test an array with anonymous strings, ints, booleans, doubles, and null in it
+ json_init
+ json_add_array "arr"
+
+ # first element uses the legacy method with no name
+ json_add_string "" "first"
+
+ # the rest use the new functions
+ json_push_string "second"
+ json_push_int 42
+ json_push_boolean true
+ json_push_double 3.141592
+ json_push_null
+
+ assertEquals '{ "arr": [ "first", "second", 42, false, 3.1415920000000002, null ] }' "$(json_dump)"
+
+ set -u
+}
+
test_jshn_append_via_json_script() {
JSON_PREFIX="${JSON_PREFIX:-}"
. ../../sh/jshn.sh