json_get "$json" "$jpath"
return $?
}
+
+json_dump_string() {
+ local in="$1" out=''
+ while [ "$in" ]; do case $in in
+ \\*) out="${out}\\\\"; in="${in#\\}" ;;
+ "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
+ "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
+ " "*) out="${out}\\t"; in="${in# }" ;;
+ \"*) out="${out}\\\""; in="${in#\"}" ;;
+ *) out="${out}${in%%[\\${CR}${BR} \"]*}"; in="${in#"${in%%[\\${BR}${CR} \"]*}"}" ;;
+ esac; done
+ printf '"%s"' "${out}"
+}
+
+json_dump_array() {
+ local json="$1" value out=''
+
+ for value in $(DB2 "$json" iterate @); do
+ out="${out},$(json_dump "$(UNSTRING "$value")")"
+ done
+ printf '[%s]' "${out#,}"
+}
+
+json_dump_object() {
+ local json="$1" key value out=''
+
+ while read -r key value; do
+ out="${out},$(json_dump_string "$(UNSTRING "$key")"):$(json_dump "$(UNSTRING "$value")")"
+ done <<-EOF
+ ${json}
+ EOF
+ printf '{%s}' "${out#,}"
+}
+
+json_dump() {
+ local json="$1"
+
+ case $json in
+ str:*)
+ json_dump_string "${json#str:}"
+ ;;
+ arr:*)
+ json_dump_array "${json#arr:}"
+ ;;
+ obj:*)
+ json_dump_object "${json#obj:}"
+ ;;
+ num:*)
+ printf "${json#num:}"
+ ;;
+ true|false|null)
+ printf %s\\n "$json"
+ ;;
+ *)
+ json_dump_string "${json}"
+ ;;
+ esac
+}