diff options
| author | Philip Prindeville | 2023-09-15 18:35:31 +0000 |
|---|---|---|
| committer | Robert Marko | 2026-02-12 15:57:28 +0000 |
| commit | 8c7b489daa029b3841584f6ac76e2a0b68d74ef3 (patch) | |
| tree | dc9e061fc844387c0b5d215fe2c90d039f1cf547 | |
| parent | 5a65cb5a79b757d24beeab4144333b012a4935ec (diff) | |
| download | libubox-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.sh | 55 | ||||
| -rwxr-xr-x | tests/shunit2/tests.sh | 25 |
2 files changed, 80 insertions, 0 deletions
@@ -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 |