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
|
#!/usr/bin/env bash
throw () {
echo
"$*"
>&2
exit
1
}
BRIEF=0
LEAFONLY=0
PRUNE=0
usage() {
echo
echo
"Usage: JSON.sh [-b] [-l] [-p] [-h]"
echo
echo
"-p - Prune empty. Exclude fields with empty values."
echo
"-l - Leaf only. Only show leaf nodes, which stops data duplication."
echo
"-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
echo
"-h - This help text."
echo
}
parse_options() {
set
--
"$@"
local
ARGN=$
#
while
[ $ARGN -
ne
0 ]
do
case
$1
in
-h) usage
exit
0
;;
-b) BRIEF=1
LEAFONLY=1
PRUNE=1
;;
-l) LEAFONLY=1
;;
-p) PRUNE=1
;;
?*)
echo
"ERROR: Unknown option."
usage
exit
0
;;
esac
shift
1
ARGN=$((ARGN-1))
done
}
awk_egrep () {
local
pattern_string=$1
gawk
'{
while
($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern=$pattern_string
}
tokenize () {
local
GREP
local
ESCAPE
local
CHAR
if
echo
"test string"
|
egrep
-ao --color=never
"test"
&>
/dev/null
then
GREP=
'egrep -ao --color=never'
else
GREP=
'egrep -ao'
fi
if
echo
"test string"
|
egrep
-o
"test"
&>
/dev/null
then
ESCAPE=
'(\[^u[:cntrl:]]|\u[0-9a-fA-F]{4})'
CHAR=
'[^[:cntrl:]"\]'
else
GREP=awk_egrep
ESCAPE=
'(\\[^u[:cntrl:]]|\u[0-9a-fA-F]{4})'
CHAR=
'[^[:cntrl:]"\\]'
fi
local
STRING=
""
$CHAR*($ESCAPE$CHAR*)*
""
local
NUMBER=
'-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local
KEYWORD=
'null|false|true'
local
SPACE=
'[[:space:]]+'
$GREP
"$STRING|$NUMBER|$KEYWORD|$SPACE|."
|
egrep
-
v
"^$SPACE$"
}
parse_array () {
local
index=0
local
ary=
''
read
-r token
case
"$token"
in
']'
) ;;
*)
while
:
do
parse_value
"$1"
"$index"
index=$((index+1))
ary=
"$ary"
"$value"
read
-r token
case
"$token"
in
']'
)
break
;;
','
) ary=
"$ary,"
;;
*) throw
"EXPECTED , or ] GOT ${token:-EOF}"
;;
esac
read
-r token
done
;;
esac
[
"$BRIEF"
-
eq
0 ] && value=`
printf
'[%s]'
"$ary"
` || value=
:
}
parse_object () {
local
key
local
obj=
''
read
-r token
case
"$token"
in
'}'
) ;;
*)
while
:
do
case
"$token"
in
'"'
*
'"'
) key=$token ;;
*) throw
"EXPECTED string GOT ${token:-EOF}"
;;
esac
read
-r token
case
"$token"
in
':'
) ;;
*) throw
"EXPECTED : GOT ${token:-EOF}"
;;
esac
read
-r token
parse_value
"$1"
"$key"
obj=
"$obj$key:$value"
read
-r token
case
"$token"
in
'}'
)
break
;;
','
) obj=
"$obj,"
;;
*) throw
"EXPECTED , or } GOT ${token:-EOF}"
;;
esac
read
-r token
done
;;
esac
[
"$BRIEF"
-
eq
0 ] && value=`
printf
'{%s}'
"$obj"
` || value=
:
}
parse_value () {
local
jpath=
"${1:+$1,}$2"
isleaf=0 isempty=0 print=0
case
"$token"
in
'{'
) parse_object
"$jpath"
;;
'['
) parse_array
"$jpath"
;;
# At this point, the only valid single-character tokens are digits.
''
|[!0-9]) throw
"EXPECTED value GOT ${token:-EOF}"
;;
*) value=$token
isleaf=1
[
"$value"
=
'""'
] && isempty=1
;;
esac
[
"$value"
=
''
] &&
return
[
"$LEAFONLY"
-
eq
0 ] && [
"$PRUNE"
-
eq
0 ] && print=1
[
"$LEAFONLY"
-
eq
1 ] && [
"$isleaf"
-
eq
1 ] && [ $PRUNE -
eq
0 ] && print=1
[
"$LEAFONLY"
-
eq
0 ] && [
"$PRUNE"
-
eq
1 ] && [
"$isempty"
-
eq
0 ] && print=1
[
"$LEAFONLY"
-
eq
1 ] && [
"$isleaf"
-
eq
1 ] &&
[ $PRUNE -
eq
1 ] && [ $isempty -
eq
0 ] && print=1
[
"$print"
-
eq
1 ] &&
printf
"[%s]t%sn"
"$jpath"
"$value"
:
}
parse () {
read
-r token
parse_value
read
-r token
case
"$token"
in
''
) ;;
*) throw
"EXPECTED EOF GOT $token"
;;
esac
}
parse_options
"$@"
if
([
"$0"
=
"$BASH_SOURCE"
] || ! [ -n
"$BASH_SOURCE"
]);
then
tokenize | parse
fi
|
本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/1353428,如需转载请自行联系原作者