3 [ -n "$include_json" ] && return 0
6 . "${_EXEC:-.}/cgilite/db23.sh"
8 # debug(){ [ $# -gt 0 ] && printf '%s\n' "$@" >&2 || tee -a /dev/stderr; }
11 printf '%s\n' "$@" >&2;
12 printf 'Exc: %s\n' "$json_document" >&2
16 while true; do case "$json_document" in
17 [" ${BR}${CR} "]*) json_document="${json_document#?}";;
23 local string json_document="$json_document" end=0
26 case $json_document in
27 \"*) json_document="${json_document#?}"
29 *) json_except "Expected string specifyer starting with (\")"
33 while [ "$json_document" ]; do case $json_document in
35 string="${string}${json_document%"${json_document#??}"}"
36 json_document="${json_document#??}"
39 json_document="${json_document#?}"
44 string="${string}${json_document%"${json_document#?}"}"
45 json_document="${json_document#?}"
49 if [ $end -eq 0 ]; then
50 json_except "Document ended mid-string"
54 printf "%s %s\n" "$(STRING "$string")" "$json_document"
58 local key json_document="$json_document"
61 case $json_document in
63 key="$(json_string)" || return 1
64 json_document="${key#* }"
67 *) json_except "Expected key specifyer starting with '\"'"
72 case $json_document in
73 :*) json_document="${json_document#?}"
75 *) json_except "Expected value separator \":\""
80 printf '%s %s\n' "$key" "$json_document"
84 local number json_document="$json_document"
87 number="${json_document%%[" ${BR}${CR} ,}]"]*}"
88 json_document="${json_document#"$number"}"
89 if ! number="$(printf %f "$number")"; then
90 json_except "Invalid number format"
94 printf '%s %s\n' "${number%.000000}" "$json_document"
98 local struct="$(DB2 new)" value json_document="$json_document"
101 case $json_document in
102 "["*) json_document="${json_document#?}"
104 *) json_except "Expected array starting with \"[\""
110 case $json_document in
112 printf "%s %s\n" "" "${json_document#?}"
120 value="$(json_value)" || return 1
121 json_document="${value#* }"
122 value="$(UNSTRING "${value%% *}")"
124 struct="$(DB2 "$struct" append "@" "$value")" \
125 || struct="$(DB2 "$struct" set "@" "$value")"
128 case $json_document in
129 ,*) json_document="${json_document#?}"
131 "]"*) json_document="${json_document#?}"
134 *) json_except "Unexpected character mid-array"
140 printf "%s %s\n" "$(STRING "$struct")" "$json_document"
144 local struct="$(DB2 new)" key value json_document="$json_document"
147 case $json_document in
148 "{"*) json_document="${json_document#?}"
150 *) json_except "Expected object starting with \"{\""
156 case $json_document in
158 printf "%s %s\n" "" "${json_document#?}"
166 key="$(json_key)" || return 1
167 json_document="${key#* }"
168 key="$(UNSTRING "${key%% *}")"
170 value="$(json_value)" || return 1
171 json_document="${value#* }"
172 value="$(UNSTRING "${value%% *}")"
174 struct="$(DB2 "$struct" set "$key" "$value")"
177 case $json_document in
178 ,*) json_document="${json_document#?}"
180 "}"*) json_document="${json_document#?}"
183 *) json_except "Unexpected character mid-object"
189 printf "%s %s\n" "$(STRING "$struct")" "$json_document"
193 local value json_document="$json_document"
197 case $json_document in
199 value="$(json_string)" || return 1
200 json_document="${value#* }"
201 value="str:${value%% *}"
205 value="$(json_number)" || return 1
206 json_document="${value#* }"
207 value="num:${value%% *}"
211 value="$(json_object)" || return 1
212 json_document="${value#* }"
213 value="obj:${value%% *}"
217 value="$(json_array)" || return 1
218 json_document="${value#* }"
219 value="arr:${value%% *}"
223 json_document="${json_document#null}"
228 json_document="${json_document#true}"
233 json_document="${json_document#false}"
239 printf "%s %s\n" "$value" "$json_document"
243 local json_document="$1" json
249 local json="$1" jpath="${2#.}" key idx
253 str:*) json_type="string";;
254 arr:*) json_type="array";;
255 obj:*) json_type="object";;
256 num:*) json_type="number";;
258 json_type="boolean";;
259 null) json_type="null";;
264 printf %s\\n "${json#???:}"
268 idx="${jpath%%"]"*}" idx="${idx#"["}"
269 jpath="${jpath#"["*"]"}"
272 key="${jpath%%"']"*}" key="${key#"['"}"
273 jpath="${jpath#"['"*"']"}"
278 *) key="${jpath%%[".["]*}"
279 jpath="${jpath#"$key"}"
283 if [ "$key" -a "$json_type" = object ]; then
284 if ! json="$(DB2 "${json#obj:}" get "$key")"; then
285 debug "Key not found: \"$key\""
288 elif [ "$idx" -a "$json_type" = array ]; then
289 if ! json="$(DB2 "${json#arr:}" get @ "$(( idx + 1 ))")"; then
290 debug "Array index not found: \"$idx\""
293 elif [ "$key" ]; then
294 debug "Cannot select key (\"$key\") from value of type \"$json_type\""
296 elif [ "$idx" ]; then
297 debug "Cannot select index ($idx) from value of type \"$json_type\""
300 json_get "$json" "$jpath"