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' "num:${number%.000000}" "$json_document"
98 local value json_document="$json_document"
102 case $json_document in
104 value="$(json_string)" || return 1
105 json_document="${value#* }"
106 value="str:${value%% *}"
110 value="$(json_number)" || return 1
111 json_document="${value#* }"
116 value="$(json_object)" || return 1
117 json_document="${value#* }"
118 value="obj:${value%% *}"
122 value="$(json_array)" || return 1
123 json_document="${value#* }"
124 value="arr:${value%% *}"
128 json_document="${json_document#null}"
133 json_document="${json_document#true}"
138 json_document="${json_document#false}"
144 printf "%s %s\n" "$value" "$json_document"
148 local struct="$(DB2 new)" value json_document="$json_document"
151 case $json_document in
152 "["*) json_document="${json_document#?}"
154 *) json_except "Expected array starting with \"[\""
160 case $json_document in
162 printf "%s %s\n" "" "${json_document#?}"
170 value="$(json_value)" || return 1
171 json_document="${value#* }"
172 value="$(UNSTRING "${value%% *}")"
174 struct="$(DB2 "$struct" append "@" "$value")" \
175 || struct="$(DB2 "$struct" set "@" "$value")"
178 case $json_document in
179 ,*) json_document="${json_document#?}"
181 "]"*) json_document="${json_document#?}"
184 *) json_except "Unexpected character mid-array"
190 printf "%s %s\n" "$(STRING "$struct")" "$json_document"
194 local struct="$(DB2 new)" key value json_document="$json_document"
197 case $json_document in
198 "{"*) json_document="${json_document#?}"
200 *) json_except "Expected object starting with \"{\""
206 case $json_document in
208 printf "%s %s\n" "" "${json_document#?}"
216 key="$(json_key)" || return 1
217 json_document="${key#* }"
218 key="$(UNSTRING "${key%% *}")"
220 value="$(json_value)" || return 1
221 json_document="${value#* }"
222 value="$(UNSTRING "${value%% *}")"
224 struct="$(DB2 "$struct" set "$key" "$value")"
227 case $json_document in
228 ,*) json_document="${json_document#?}"
230 "}"*) json_document="${json_document#?}"
233 *) json_except "Unexpected character mid-object"
239 printf "%s %s\n" "$(STRING "$struct")" "$json_document"
243 local json_document="$1"
249 local json="$1" jpath="${2#.}" id 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";;
269 idx="${jpath%%"]"*}" idx="${idx#"["}"
270 jpath="${jpath#"["*"]"}"
273 id="${jpath%%"']"*}" id="${id#"['"}"
274 jpath="${jpath#"['"*"']"}"
276 *) id="${jpath%%[".["]*}"
277 jpath="${jpath#"$id"}"
281 if [ "$id" -a "$json_type" = object ]; then
282 # if ! json="$(DB2 "$(UNSTRING "$json")" get "$id")"; then
283 if ! json="$(DB2 "$json" get "$id")"; then
284 debug "No key: \"$id\""
287 elif [ "$idx" -a "$json_type" = array ]; then
288 if ! json="$(DB2 "$(UNSTRING "$json")" get @ "$(( idx + 1 ))")"; then
289 # if ! json="$(DB2 "$json" get @ "$(( idx + 1 ))")"; then
290 debug "No array index: \"$idx\""
294 debug "Value type missmatch"
297 json_get "$json" "$jpath"