[PATCH 1/1] jshn.sh: Add getters for the cursor and its parent
Philip Prindeville
philipp at redfish-solutions.com
Sun May 4 15:04:50 PDT 2025
From: Philip Prindeville <philipp at redfish-solutions.com>
When building JSON, the order in which it is synthesized might not
match the order in which the source information is parsed. In those
cases, it is useful to be able to save your place in an object or
an array and come back to it later and fill it out.
Signed-off-by: Philip Prindeville <philipp at redfish-solutions.com>
---
sh/jshn.sh | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/sh/jshn.sh b/sh/jshn.sh
index 1d3055711820e5a21ea59cae49e7bf3f56b626d8..5599c85835d0c81c8d47a9dfd73f32e4db2a7c9b 100644
--- a/sh/jshn.sh
+++ b/sh/jshn.sh
@@ -197,6 +197,42 @@ json_add_fields() {
done
}
+json_get_position() {
+ local __dest="$1"
+ eval "export -- \"$__dest=\${JSON_CUR}\"; [ -n \"\${JSON_CUR+x}\" ]"
+}
+
+json_move_to() {
+ local cur="$1"
+ _json_set_var JSON_CUR "$cur"
+}
+
+json_get_parent_position() {
+ local __dest="$1" cur parent
+ _json_get_var cur JSON_CUR
+ parent="U_$cur"
+ eval "export -- \"$__dest=\${$parent}\"; [ -n \"\${$parent+x}\" ]"
+}
+
+json_get_root_position() {
+ local __dest="$1" cur="J_V"
+ eval "export -- \"$__dest=\${cur}\"; [ -n \"\${cur+x}\" ]"
+}
+
+json_get_index() {
+ local __dest="$1"
+ local cur parent seq
+ _json_get_var cur JSON_CUR
+ _json_get_var parent "U_$cur"
+ if [ "${parent%%[0-9]*}" != "J_A" ]; then
+ [ -n "$_json_no_warning" ] || \
+ echo "WARNING: Not inside an array" >&2
+ return 1
+ fi
+ seq="S_$parent"
+ eval "export -- \"$__dest=\${$seq}\"; [ -n \"\${$seq+x}\" ]"
+}
+
# functions read access to json variables
json_compact() {
--
2.43.0
More information about the openwrt-devel
mailing list