|
| 1 | +# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*- |
| 2 | +# @str-parse-json |
| 3 | +# |
| 4 | +# Parses the buffer ($1) with JSON and returns: |
| 5 | +# |
| 6 | +# 1. Fields for the given key ($2) in the given hash ($3) |
| 7 | +# 2. The hash looks like follows: |
| 8 | +# 1/1 → strings at the level 1 of the 1st object |
| 9 | +# 1/2 → strings at the level 1 of the 2nd object |
| 10 | +# … |
| 11 | +# 2/1 → strings at 2nd level of the 1st object |
| 12 | +# … |
| 13 | +# |
| 14 | +# The strings are parseable with "${(@Q)${(@z)value}", i.e.: |
| 15 | +# they're concatenated and quoted strings found in the JSON. |
| 16 | +# |
| 17 | +# Example: |
| 18 | +# |
| 19 | +# {"zplugin-ices":{ |
| 20 | +# "default":{ |
| 21 | +# "wait":"1", |
| 22 | +# "lucid":"", |
| 23 | +# "as":"program", |
| 24 | +# "pick":"fzy", |
| 25 | +# "make":"", |
| 26 | +# }, |
| 27 | +# "bgn":{ |
| 28 | +# "wait":"1", |
| 29 | +# "lucid":"", |
| 30 | +# "as":"null", |
| 31 | +# "make":"", |
| 32 | +# "sbin":"fzy;contrib/fzy-*" |
| 33 | +# } |
| 34 | +# }} |
| 35 | +# |
| 36 | +# Will result in: |
| 37 | +# |
| 38 | +# local -A Strings |
| 39 | +# Strings[1/1]='zplugin-ices' |
| 40 | +# Strings[2/1]='default bgn' |
| 41 | +# Strings[3/1]='wait 1 lucid \ as program pick fzy make \ ' |
| 42 | +# Strings[3/2]='wait 1 lucid \ as null make \ sbin fzy\;contrib/fzy-\*' |
| 43 | +# |
| 44 | +# So that when you e.g.: expect a key `bgn' but don't know at which |
| 45 | +# position, you can do: |
| 46 | +# |
| 47 | +# local -A Strings |
| 48 | +# @str-parse-json "$json" "zplugin-ices" Strings |
| 49 | +# |
| 50 | +# integer pos |
| 51 | +# pos=${${(@Q)${(@z)Strings[2/1]}}[(I)bgn]} |
| 52 | +# if (( pos )) { |
| 53 | +# local -A ices |
| 54 | +# ices=( "${(@Q)${(@z)Strings[3/$pos]}}" ) |
| 55 | +# # Use the `ices' hash holding the values of the `bgn' object |
| 56 | +# … |
| 57 | +# } |
| 58 | +# |
| 59 | +# $1 - the buffer with JSON |
| 60 | +# $2 - the key in the JSON that should be mapped to the |
| 61 | +# result (i.e.: you can map subset of the input) |
| 62 | +# $3 - the name of the output hash parameter |
| 63 | + |
| 64 | +@str-parse-json() { |
| 65 | + emulate -LR zsh -o extendedglob -o warncreateglobal -o typesetsilent |
| 66 | + |
| 67 | + local -A __pos_to_level __level_to_pos __pair_map \ |
| 68 | + __final_pairs __Strings __Counts |
| 69 | + local __input=$1 __workbuf=$1 __key=$2 __varname=$3 \ |
| 70 | + __style __quoting |
| 71 | + integer __nest=${4:-1} __idx=0 __pair_idx __level=0 \ |
| 72 | + __start __end __sidx=1 __had_quoted_value=0 |
| 73 | + local -a match mbegin mend __pair_order |
| 74 | + |
| 75 | + (( ${(P)+__varname} )) || typeset -gA "$__varname" |
| 76 | + |
| 77 | + __pair_map=( "(" ")" "{" "}" "[" "]" ) |
| 78 | + while [[ $__workbuf = (#b)[^"{}()[]\\\"'":,]#((["({[]})\"'":,])|[\\](*))(*) ]]; do |
| 79 | + [[ -n ${match[3]} ]] && { |
| 80 | + __idx+=${mbegin[1]} |
| 81 | + |
| 82 | + [[ $__quoting = \' ]] && \ |
| 83 | + { __workbuf=${match[3]}; } || \ |
| 84 | + { __workbuf=${match[3]:1}; (( ++ __idx )); } |
| 85 | + |
| 86 | + } || { |
| 87 | + __idx+=${mbegin[1]} |
| 88 | + [[ -z $__quoting ]] && { |
| 89 | + if [[ ${match[1]} = ["({["] ]]; then |
| 90 | + __Strings[$__level/${__Counts[$__level]}]+=" $'\0'--object--$'\0'" |
| 91 | + __pos_to_level[$__idx]=$(( ++ __level )) |
| 92 | + __level_to_pos[$__level]=$__idx |
| 93 | + (( __Counts[$__level] += 1 )) |
| 94 | + __sidx=__idx+1 |
| 95 | + __had_quoted_value=0 |
| 96 | + elif [[ ${match[1]} = ["]})"] ]]; then |
| 97 | + (( !__had_quoted_value )) && \ |
| 98 | + __Strings[$__level/${__Counts[$__level]}]+=" ${(q)__input[__sidx,__idx-1]//((#s)[[:blank:]]##|([[:blank:]]##(#e)))}" |
| 99 | + __had_quoted_value=1 |
| 100 | + if (( __level > 0 )); then |
| 101 | + __pair_idx=${__level_to_pos[$__level]} |
| 102 | + __pos_to_level[$__idx]=$(( __level -- )) |
| 103 | + [[ ${__pair_map[${__input[__pair_idx]}]} = ${__input[__idx]} ]] && { |
| 104 | + __final_pairs[$__idx]=$__pair_idx |
| 105 | + __final_pairs[$__pair_idx]=$__idx |
| 106 | + __pair_order+=( $__idx ) |
| 107 | + } |
| 108 | + else |
| 109 | + __pos_to_level[$__idx]=-1 |
| 110 | + fi |
| 111 | + fi |
| 112 | + } |
| 113 | + |
| 114 | + [[ ${match[1]} = \" && $__quoting != \' ]] && \ |
| 115 | + if [[ $__quoting = '"' ]]; then |
| 116 | + __Strings[$__level/${__Counts[$__level]}]+=" ${(q)__input[__sidx,__idx-1]}" |
| 117 | + __quoting="" |
| 118 | + else |
| 119 | + __had_quoted_value=1 |
| 120 | + __sidx=__idx+1 |
| 121 | + __quoting='"' |
| 122 | + fi |
| 123 | + |
| 124 | + [[ ${match[1]} = , && -z $__quoting ]] && \ |
| 125 | + { |
| 126 | + (( !__had_quoted_value )) && \ |
| 127 | + __Strings[$__level/${__Counts[$__level]}]+=" ${(q)__input[__sidx,__idx-1]//((#s)[[:blank:]]##|([[:blank:]]##(#e)))}" |
| 128 | + __sidx=__idx+1 |
| 129 | + __had_quoted_value=0 |
| 130 | + } |
| 131 | + |
| 132 | + [[ ${match[1]} = : && -z $__quoting ]] && \ |
| 133 | + { |
| 134 | + __had_quoted_value=0 |
| 135 | + __sidx=__idx+1 |
| 136 | + } |
| 137 | + |
| 138 | + [[ ${match[1]} = \' && $__quoting != \" ]] && \ |
| 139 | + if [[ $__quoting = "'" ]]; then |
| 140 | + __Strings[$__level/${__Counts[$__level]}]+=" ${(q)__input[__sidx,__idx-1]}" |
| 141 | + __quoting="" |
| 142 | + else |
| 143 | + __had_quoted_value=1 |
| 144 | + __sidx=__idx+1 |
| 145 | + __quoting="'" |
| 146 | + fi |
| 147 | + |
| 148 | + __workbuf=${match[4]} |
| 149 | + } |
| 150 | + done |
| 151 | + |
| 152 | + local __text value __found |
| 153 | + integer __pair_a __pair_b |
| 154 | + for __pair_a ( "${__pair_order[@]}" ) { |
| 155 | + __pair_b="${__final_pairs[$__pair_a]}" |
| 156 | + __text="${__input[__pair_b,__pair_a]}" |
| 157 | + if [[ $__text = [[:space:]]#\{[[:space:]]#[\"\']${__key}[\"\']* ]]; then |
| 158 | + if (( __nest != 2 )) { |
| 159 | + __found="$__text" |
| 160 | + break |
| 161 | + } |
| 162 | + fi |
| 163 | + } |
| 164 | + |
| 165 | + if [[ -n $__found && $__nest -lt 2 ]] { |
| 166 | + @str-parse-json "$__found" "$__key" "$__varname" 2 |
| 167 | + } |
| 168 | + |
| 169 | + if (( __nest == 2 )) { |
| 170 | + : ${(PAA)__varname::="${(kv)__Strings[@]}"} |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +# vim:ft=zsh:sts=4:sw=4:et |
0 commit comments