-
Notifications
You must be signed in to change notification settings - Fork 4
/
json.sh
executable file
·323 lines (276 loc) · 8.19 KB
/
json.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env bash
# http://tldp.org/LDP/abs/html/string-manipulation.html#GETOPTSIMPLE
_command_not_found_handle ()
{
echo "The following command is not valid: \""$1\""";
echo "With the following argument(s): "
for args in "$@"; do
echo \""$args"\"
done
}
# http://stackoverflow.com/questions/918886/split-string-based-on-delimiter-in-bash
# You can read everything at once without using a while loop: read -r -d '' -a addr <<< "$in" # The -d '' is key here, it tells read not to stop at the first newline (which is the default -d) but to continue until EOF or a NULL byte (which only occur in binary data).
# read ADDR1 ADDR2 <<<$(IFS=";"; echo $IN)
# IP=1.2.3.4; IP=(${IP//./ });
# http://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-array-in-bash
# for i in "${!array[@]}"
# do
# echo "key : $i"
# echo "value: ${array[$i]}"
# done
# declare -A fullNames
# fullNames=( ["lhunath"]="Maarten Billemont" ["greycat"]="Greg Wooledge" )
# for user in "${!fullNames[@]}"
# do
# echo "User: $user, full name: ${fullNames[$user]}."
# done
# {s:9:"operation";s:3:"pop";s:9:"stackname";s:6:"AddApp";}
# {"operation":"pop","stackname":"ipsFree"}
# Task 1. Make a 1 dimensional associative array containing only strings.
# chr() - converts decimal value to its ASCII character representation
# ord() - converts ASCII character to its decimal value
# (taken from http://wooledge.org:8000/BashFAQ)
chr() {
printf \\$(printf '%03o' $1)
}
ord() {
printf '%d' "'$1"
}
pplslash() {
local cmd="$1"
shift
local -a a=()
for arg in "$@"; do
arg="${arg//\"/\\\"}" # arg="${arg//\\/\\\\}"
a+=( "$arg" )
done
"$cmd" "${a[@]}"
}
declare -a JSONTABLE # This still makes for a local declaration, a tad inefficient
JSONTABLE[ 8]="\\b"
JSONTABLE[ 9]="\\t"
JSONTABLE[ 10]="\\n"
JSONTABLE[ 12]="\\f"
JSONTABLE[ 13]="\\r"
JSONTABLE[ 34]="\\\""
JSONTABLE[ 47]="\\/"
JSONTABLE[ 92]="\\\\"
# local i
for i in {0..31} 47 92 127; do
test -z "${JSONTABLE[$i]}" &&
printf -v "JSONTABLE[$i]" '\\%04x' $i
done
json_escape() {
# JSONTABLE[ 0]="\\u0000"
# JSONTABLE[ 1]="\\u0001"
# JSONTABLE[ 2]="\\u0002"
# JSONTABLE[ 3]="\\u0003"
# JSONTABLE[ 4]="\\u0004"
# JSONTABLE[ 5]="\\u0005"
# JSONTABLE[ 6]="\\u0006"
# JSONTABLE[ 7]="\\u0007"
# JSONTABLE[ 11]="\\u000b"
# JSONTABLE[ 14]="\\u000e"
# JSONTABLE[ 15]="\\u000f"
# JSONTABLE[ 16]="\\u0010"
# JSONTABLE[ 17]="\\u0011"
# JSONTABLE[ 18]="\\u0012"
# JSONTABLE[ 19]="\\u0013"
# JSONTABLE[ 20]="\\u0014"
# JSONTABLE[ 21]="\\u0015"
# JSONTABLE[ 22]="\\u0016"
# JSONTABLE[ 23]="\\u0017"
# JSONTABLE[ 24]="\\u0018"
# JSONTABLE[ 25]="\\u0019"
# JSONTABLE[ 26]="\\u001a"
# JSONTABLE[ 27]="\\u001b"
# JSONTABLE[ 28]="\\u001c"
# JSONTABLE[ 29]="\\u001d"
# JSONTABLE[ 30]="\\u001e"
# JSONTABLE[ 31]="\\u001f"
# JSONTABLE[127]="\\u007f"
# JSONTABLE[ 8]="\\u0008"
# JSONTABLE[ 9]="\\u0009"
# JSONTABLE[ 10]="\\u000a"
# JSONTABLE[ 12]="\\u000c"
# JSONTABLE[ 13]="\\u000d"
local STRING="${1}"
local RESULT=""
local strlen=${#STRING}
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${STRING:$pos:1}
o=$( ord "$c" )
# printf "%04x %d '%s'\n" "$o" "'$c" "$( echo -n "$c" | xxd)"
if [ ! -z "${JSONTABLE[$o]}" ]; then
RESULT+="${JSONTABLE[$o]}"
# echo -n "${JSONTABLE[$o]}"
else
RESULT+="$c"
# echo -n "$c"
fi
done
echo "$RESULT"
}
aarray2json() {
# see http://stackoverflow.com/questions/4069188/how-to-pass-an-associative-array-as-argument-to-a-function-in-bash
# set -o xtrace
# eval "declare -A func_assoc_array="${1#*=}
# declare -p func_assoc_array >&2
# unset func_assoc_array
local defn="${1#*=\'}"
defn="${defn%\'}"
local -A func_assoc_array=$defn
# local -A func_assoc_array=${1#*=}
# eval declare -A func_assoc_array=${1#*=}
# declare -p func_assoc_array >&2
# set +o xtrace
# declare -p func_assoc_array >&2
OIFS=$IFS
IFS=
json=
for key in "${!func_assoc_array[@]}"
do
value="${func_assoc_array[$key]}"
e_key="$( json_escape "$key" )"
e_value="$( json_escape "$value" )"
json_pair="\"${e_key}\":\"${e_value}\""
json="${json}${json_pair},"
done
# remove trailing comma
json=${json%,}
# add surrounding braces
json="{${json}}"
echo "$json"
IFS=$OIFS
}
# json2array() {
# properties {"checkprops.testkey":"checkprops.testval""checkprops.testkey2":"""my.random.key.18156":"""":""}
# }
function aaray2json_ref {
# this version passes by reference and returns as $JSON
[ "$#" -ne "1" ] && echo no arguments > /dev/stderr && return 1
# couldn't find a way to pass it properly, so we're doing the same declare trick, but it's done inside the function.
def="$( declare -p "$1" )"
read _declare _type _definition < <( declare -p "$1" )
# declare -p _declare _type _definition
# _type=${_type:1:1} # A)ssociate a)rray -)var f)function ?
_type=${_type##-} # A)ssociate a)rray -)var f)function ?
[ -n "$_type" ] && _type="-$_type"
# see http://stackoverflow.com/questions/4069188/how-to-pass-an-associative-array-as-argument-to-a-function-in-bash
# echo eval "local -A func_assoc_array="${json_command#*=}
local de="${def#*=\'}"
de="${de%\'}"
# eval echo "declare -${_type} func_assoc_array=$de"
# eval "declare -${_type} func_assoc_array=$de"
local $_type func_assoc_array=$de
# declare -p func_assoc_array
# sleep 1
# declare -A func_assoc_array='([0]="'\''([operation]=\"push\" [stackname]=\"ba\\\"sh ^B: ^A: ^C: ^? ^H ^I^@:\" )'\''" )'
# exit
local key
local value
local e_key
local e_value
local json_pair
local json
local keys
local ignore_keys=0
keys=${!func_assoc_array[@]}
# If it is a non-associative array, or the first key is 0, assume we can ignore keys in JSON output
if [ "$_type" == "a" -o "${keys[0]}" == "0" ]; then
ignore_keys=1
fi
OIFS=$IFS
IFS=
for key in "${!func_assoc_array[@]}"
do
value=${func_assoc_array[$key]}
(( ! ignore_keys )) && e_key="$( json_escape "$key" )"
e_value="$( json_escape "$value" )"
if (( ignore_keys )); then
json="${json}\"${e_value}\","
else
json_pair="\"${e_key}\":\"${e_value}\"" json="${json}${json_pair},"
fi
done
# remove trailing comma
json=${json%,}
# add surrounding braces
if (( ignore_keys )); then
json="[${json}]"
else
json="{${json}}"
fi
JSON=$json # JSON is global
# echo "$json"
IFS=$OIFS
}
# gearman_stack stackname operation [value]
# gearman_stack ( insert | push | unshift | pop | slice | splice | shift | get | clear | count ) stackname [ value ]
# eg: gearman_stack pop MyStack
# gearman_stack push MyStack "This is a value"
# operations: insert push unshift pop slice splice shift get clear count
gearman_stack() {
SERVER_ADDR=localhost
SERVER_PORT=4731
argc=$#
declare -A stackargs=( ["operation"]="$1" ["stackname"]="$2" )
(( argc == 3 )) && stackargs["value"]="$3"
json_command="$( aarray2json "$( declare -p stackargs )" )"
echo Stack "${json_command}"
exec 5<>/dev/tcp/$SERVER_ADDR/$SERVER_PORT
el=$?
(( el )) &&
{
echo "Could not connect to GearProxy on $SERVER_ADDR:$SERVER_PORT ( errorlevel $el )"
exit 1
}
echo Stack2 "${json_command}" >&5
while IFS="" read -r REPLY <&5
do
LASTREPLY="${REPLY}"
echo "$REPLY"
done
echo "Last line: $REPLY"
REPLY="${LASTREPLY}"
}
test_crap() {
# declare an assocociative array
# declare -A assoc_array=(["key1"]="value1" ["key2"]="value2")
# show assocociative array definition
# declare -p assoc_array
# pass assocociative array in string form to function
# print_array "$(declare -p assoc_array)"
IFS=
# declare -A AA='(["operation"]="push" ["crlf"]="cr$( chr 13 )lf$( chr 10 )." ["stackname"]="ba\"sh ^B: ^A:$( chr 1 ) ^C: ^?:$( chr 127 ) ^H: ^I:$( chr 127 )^@:$( chr 0 )")'
declare -A AA='([this]="that" ["operation"]="push" ["crlf"]="crlf")'
declare -p AA
# AA["stackname"]="bash"
IFS=
# AA["operation"]="push"
for i in "${!AA[@]}"
do
echo "key : $i"
echo "value : ${AA[$i]}" | xxd
done
unset IFS
time {
declare -A AA='([this]="that" ["operation"]="push" ["crlf"]="crlf")'
json=$( aarray2json "$(declare -p AA)" )
echo encoded json is: $json
echo; echo
}
time (
declare -A AA='([this]="that" ["operation"]="push" ["crlf"]="crlf")'
aaray2json_ref AA
echo; echo
echo encoded jsonref is $JSON
)
}
# test_crap
# exit 0
#
# gearman_stack push bash "Test Value"
# gearman_stack push bash "Another Test Value"
# gearman_stack pop bash