-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.brs
331 lines (285 loc) · 10.1 KB
/
json.brs
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
324
325
326
327
328
329
330
331
'Copyright (c) 2010 Chris Hoffman
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' THE SOFTWARE.
Function BSJSON() As Object
this = CreateObject("roAssociativeArray")
'Constants
this.TOKEN_NONE = 0
this.TOKEN_CURLY_OPEN = 1
this.TOKEN_CURLY_CLOSE = 2
this.TOKEN_SQUARED_OPEN = 3
this.TOKEN_SQUARED_CLOSE = 4
this.TOKEN_COLON = 5
this.TOKEN_COMMA = 6
this.TOKEN_STRING = 7
this.TOKEN_NUMBER = 8
this.TOKEN_TRUE = 9
this.TOKEN_FALSE = 10
this.TOKEN_NULL = 11
'Functions
this.JsonDecode = json_decode
this.ParseObject = json_parse_object
this.ParseArray = json_parse_array
this.ParseValue = json_parse_value
this.ParseString = json_parse_string
this.ParseNumber = json_parse_number
this.EatWhitespace = json_eat_whitespace
this.GetLastIndexOfNumber = json_get_last_index_number
this.LookAhead = json_look_ahead
this.NextToken = json_next_token
return this
End Function
Function json_decode(json As String)
success = [True] 'In list to pass as reference
if json.Len() > 0 then
charArray = CreateObject("roArray", json.Len(), false)
for i=0 to json.Len() - 1
charArray.Push(json.Mid(i,1))
end for
index = [0] 'In list to pass as reference
value = m.ParseValue(charArray, index, success)
if success[0] then return value
end if
return invalid
End Function
Function json_parse_object(json As Object, index As Object, success As Object)
aa = CreateObject("roAssociativeArray")
' {
m.NextToken(json, index)
while (true)
token = m.LookAhead(json, index)
if token = m.TOKEN_NONE then
success[0] = false
return invalid
else if token = m.TOKEN_COMMA then
m.NextToken(json, index)
else if token = m.TOKEN_CURLY_CLOSE then
m.NextToken(json, index)
return aa
else
' name
name = m.ParseString(json, index, success)
if not success[0] then
return invalid
end if
' :
token = m.NextToken(json, index)
if token <> m.TOKEN_COLON then
return invalid
end if
' value
value = m.ParseValue(json, index, success)
if not success[0] then
return invalid
end if
aa[name] = value
end if
end while
End Function
Function json_parse_array(json As Object, index As Object, success As Object)
array = CreateObject("roArray",1,true)
' [
m.NextToken(json, index)
while (true)
token = m.LookAhead(json, index)
if token = m.TOKEN_NONE then
success[0] = false
return invalid
else if token = m.TOKEN_COMMA then
m.NextToken(json, index)
else if token = m.TOKEN_SQUARED_CLOSE then
m.NextToken(json, index)
return array
else
value = m.ParseValue(json, index, success)
if not success[0] then
return invalid
end if
array.Push(value)
end if
end while
End Function
Function json_parse_value(json As Object, index As Object, success As Object)
next_token = m.LookAhead(json, index)
if next_token = m.TOKEN_STRING then
return m.ParseString(json, index, success)
else if next_token = m.TOKEN_NUMBER then
return m.ParseNumber(json, index)
else if next_token = m.TOKEN_CURLY_OPEN then
return m.ParseObject(json, index, success)
else if next_token = m.TOKEN_SQUARED_OPEN then
return m.ParseArray(json, index, success)
else if next_token = m.TOKEN_TRUE then
m.NextToken(json, index)
return True
else if next_token = m.TOKEN_FALSE then
m.NextToken(json, index)
return False
else if next_token = m.TOKEN_NULL then
m.NextToken(json, index)
return invalid 'No null in BRS
else
success[0] = false
return invalid
end if
End Function
Function json_parse_string(json As Object, index As Object, success As Object)
s = ""
m.EatWhitespace(json, index)
' "
c = json[index[0]]
index[0] = index[0] + 1
complete = false
valid = true
while not complete and index[0] < json.Count()
c = json[index[0]]
index[0] = index[0] + 1
if c = CHR(34) then 'Double quote
complete = true
exit while
else if c = "\" then
if index[0] >= json.Count() then
exit while
end if
c = json[index[0]]
index[0] = index[0] + 1
if c = CHR(34) then
s = s + CHR(34)
else if c = "\" then
s = s + "\"
else if c = "/" then
s = s + "/"
else if c = "b" then
s = s + CHR(8) 'Backspace
else if c = "f" then
s = s + CHR(12) 'Formfeed
else if c = "n" then
s = s + CHR(11) 'Newline
else if c = "r" then
s = s + CHR(13) 'Carriage return
else if c = "t" then
s = s + CHR(9) 'Tab
else if c = "u" then
remainingLength = json.Count() - index[0] - 1
if remainingLength >= 4 then
'Brightscript does not support unicode, invalidate string
valid = false
index[0] = index[0] + 4
else
exit while
end if
end if
else
s = s + c
end if
end while
if not complete then
success[0] = false
return invalid
end if
if not valid then
return invalid
end if
return s
End Function
Function json_parse_number(json As Object, index As Object)
m.EatWhitespace(json, index)
lastIndex = m.GetLastIndexOfNumber(json, index)
number_str = ""
while index[0] <= lastIndex
number_str = number_str + json[index[0]]
index[0] = index[0] + 1
end while
index[0] = lastIndex + 1
return Val(number_str)
End Function
Function json_get_last_index_number(json As Object, index As Object)
regex = CreateObject("roRegex","\d|e|E|\.|-","")
lastIndex = index[0]
while lastIndex<json.Count()
if not regex.IsMatch(json[lastIndex]) then
exit while
end if
lastIndex = lastIndex + 1
end while
return lastIndex - 1
End Function
Sub json_eat_whitespace(json As Object, index As Object)
regex = CreateObject("roRegex"," |\t|\n|\r","")
while index[0]<json.Count()
if not regex.IsMatch(json[index[0]]) then
exit while
end if
index[0] = index[0] + 1
end while
End Sub
Function json_look_ahead(json As Object, index As Object)
saveIndex=[]
saveIndex[0] = index[0]
return m.NextToken(json, saveIndex)
End Function
Function json_next_token(json As Object, index As Object)
m.EatWhitespace(json, index)
if index[0] = json.Count() then
return m.TOKEN_NONE
end if
c = json[index[0]]
index[0] = index[0] + 1
if c="{" then
return m.TOKEN_CURLY_OPEN
else if c="}" then
return m.TOKEN_CURLY_CLOSE
else if c="[" then
return m.TOKEN_SQUARED_OPEN
else if c="]" then
return m.TOKEN_SQUARED_CLOSE
else if c="," then
return m.TOKEN_COMMA
else if c=CHR(34) then 'Double quote
return m.TOKEN_STRING
else if c="0" or c="1" or c="2" or c="3" or c="4" or c="5" or c="6" or c="7" or c="8" or c="9" or c="-" then
return m.TOKEN_NUMBER
else if c=":" then
return m.TOKEN_COLON
end if
index[0] = index[0] - 1
remainingLength = json.Count() - index[0] - 1
' false
if remainingLength >= 5 then
if json[index[0]] = "f" and json[index[0] + 1] = "a" and json[index[0] + 2] = "l" and json[index[0] + 3] = "s" and json[index[0] + 4] = "e" then
index[0] = index[0] + 5
return m.TOKEN_FALSE
end if
end if
' true
if remainingLength >= 4 then
if json[index[0]] = "t" and json[index[0] + 1] = "r" and json[index[0] + 2] = "u" and json[index[0] + 3] = "e" then
index[0] = index[0] + 4
return m.TOKEN_TRUE
end if
end if
' null
if remainingLength >= 4 then
if json[index[0]] = "n" and json[index[0] + 1] = "u" and json[index[0] + 2] = "l" and json[index[0] + 3] = "l" then
index[0] = index[0] + 4
return m.TOKEN_NULL
end if
end if
return m.TOKEN_NONE
End Function