-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjsonDecode.il
174 lines (154 loc) · 7.41 KB
/
jsonDecode.il
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
procedure( jsonDecode( file )
let( ( state )
indent = pcreCompile( "^[\\s\\t]+" )
colon = pcreCompile( "\":[\\s\\t]+\n*" )
comma = pcreCompile( "([\\s\\t]*,[\\s\\t]+$)" )
keywordIdentifier = pcreCompile( "^\".+(\":)" )
newline = pcreCompile( "\\n" )
; Type identifier
boolTrue = pcreCompile( "(^true$)" )
boolFalse = pcreCompile( "(^false$)" )
number = pcreCompile( "(^[0-9]*$)|(^[0-9]*[.][0-9]+$)" )
tmpTable = makeTable( "json" nil )
json = tmpTable
prt = infile( file )
state = "awaitObject"
openCurlyBraces = 0
openBrackets = 0
currentKey = nil
tmpArray = nil
; Read file
while( gets(line prt)
; Replace
line = pcreReplace( indent line "" 0 )
line = pcreReplace( colon line "\":\n" 0 )
line = pcreReplace( comma line "\n,\n" 0 )
subStrings = parseString( line "\n" )
foreach( subString subStrings
; Detect start of json
if( openCurlyBraces > 0 then
case( state
(
"awaitObject"
name = pcreMatchList( keywordIdentifier list( subString ) )
if( name then
state = "awaitTypeOrValue"
else
when( state != "start" && member( subString list( "{" "," "[" "]" ) )
error()
)
)
name = car( name )
name = substring( name 2 strlen( name ) - 3)
currentKey = tconc( currentKey name )
cmd = strcat( "json->" buildString( car( currentKey ) "->" ) )
if( evalstring( cmd ) then
print( "existing" )
else
cmd = strcat( "json->" buildString( car( currentKey ) "->" ) "=makeTable( \"json\" nil )" )
evalstring( cmd )
)
)
(
"awaitTypeOrValue"
case( subString
(
"{"
state = "awaitObject"
openCurlyBraces = openCurlyBraces + 1
)
(
"}"
; remove last element
currentKey = lconc( nil reverse( cdr( reverse( car( currentKey ) ) ) ) )
openCurlyBraces = openCurlyBraces - 1
)
(
"["
state = "awaitArray"
)
(
","
state = "awaitObject"
)
(
t
; Set value
if( pcreMatchp( number subString ) then
subString = atof( subString )
else
if( pcreMatchp( boolTrue subString ) then
subString = t
else
if( pcreMatchp( boolFalse subString) then
subString = nil
else
subString = substring( subString 2 strlen( subString ) - 2 )
)
)
)
cmd = strcat( "json->" buildString( car( currentKey ) "->" ) "=subString" )
evalstring( cmd )
state = "awaitTypeOrValue"
; remove last element
currentKey = lconc( nil reverse( cdr( reverse( car( currentKey ) ) ) ) )
)
)
)
(
"awaitArray"
case( subString
(
","
)
(
"]"
; Set list
state = "awaitTypeOrValue"
tmpArray = car( tmpArray )
; append array
cmd = strcat( "json->" buildString( car( currentKey ) "->" ) "=tmpArray" )
evalstring( cmd )
tmpArray = nil
; remove last element
currentKey = lconc( nil reverse( cdr( reverse( car( currentKey ) ) ) ) )
)
(
t
; make List
if( pcreMatchp( number subString ) then
subString = atof( subString )
else
if( pcreMatchp( boolTrue subString ) then
subString = t
else
if( pcreMatchp( boolFalse subString) then
subString = nil
else
subString = substring( subString 2 strlen( subString ) - 2 )
)
)
)
tmpArray = tconc( tmpArray subString )
)
)
)
(
t
error()
)
)
else
when( subString == "{"
openCurlyBraces = openCurlyBraces + 1
)
)
)
)
when( openCurlyBraces != 0 || openBrackets != 0
error( "unclosed objects" )
)
close( prt )
json
)
)