Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 483 lines (428 sloc) 8.839 kb
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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
Type jsondecoder
Field txt$
Field i
Field curchr$
Field things:TList

Method New()
things=New TList
End Method

Method getnext(tokens$[],onlywhitespace=1)
oldi=i
While i<Len(txt)
c$=Chr(txt[i])
i:+1
For token$=EachIn tokens
If c=token
curchr=c
Return 1
EndIf
Next
If onlywhitespace And (Not (c=" " Or c="~t" Or c="~n" Or c="~r"))
i:-1
Return 0
EndIf
Wend
i=oldi
Return 0
End Method

Function Create:jsondecoder(txt$)
j:jsondecoder=New jsondecoder
j.txt=txt
j.i=i
Return j
End Function

Method parse()
While getnext(["{","["])
Select curchr
Case "{" 'new object
o:jsonobject=parseobject()
If Not o
Print "error - couldn't parse object"
EndIf
things.addlast o
Case "[" 'new array
a:jsonarray=parsearray()
If Not a
Print "error - couldn't parse array"
EndIf
things.addlast a
End Select
Wend
End Method

Method parseobject:jsonobject()
o:jsonobject=New jsonobject
While getnext(["~q","}"])
Select curchr
Case "~q"
p:jsonpair=parsepair()
If Not p
Print "error reading pair"
EndIf
o.pairs.addlast p
If Not getnext([",","}"])
Print "error after reading pair - expected either , or }"
EndIf
If curchr="}"
Return o
EndIf
Case "}"
Return o
End Select
Wend

Print "error reading Object - expected a } at least!"
End Method

Method parsepair:jsonpair()
p:jsonpair=New jsonpair
p.name=parsestring()
If Not getnext([":"])
Print "error reading pair - expected a :"
EndIf
v:jsonvalue=parsevalue()
If Not v
Print "error reading pair - couldn't read a value"
EndIf
p.value=v
Return p
End Method

Method parsearray:jsonarray()
a:jsonarray=New jsonarray
While getnext(["~q","-","0","1","2","3","4","5","6","7","8","9","{","[","t","f","n","]"])
Select curchr
Case "~q","-","0","1","2","3","4","5","6","7","8","9","{","[","t","f","n"
i:-1
v:jsonvalue=parsevalue()
a.values.addlast v
If Not getnext([",","]"])
Print "error - expecting , or ]"
EndIf
If curchr="]"
Return a
EndIf

Case "]"
Return a
End Select
Wend
Print "error - expecting a value or ]"
End Method

Method parsestring$()
oldi=i
s$=""

While getnext(["~q","\"],0)
s:+txt[oldi..i-1]
Select curchr
Case "~q"
Return s
Case "\"
Select Chr(txt[i])
Case "~q"
s:+"~q"
Case "\"
s:+"\"
Case "/"
s:+"/"
Case "b"
s:+Chr(8)
Case "f"
s:+Chr(12)
Case "n"
s:+"~n"
Case "r"
s:+"~r"
Case "t"
s:+"~t"
Case "u"
s:+parseunicode()
End Select
i:+1
End Select
oldi=i
Wend
End Method

Method parseunicode$()
n:Short=0
For t=1 To 4
n:*16
c=txt[i+t]
If c>48 And c<57
n:+c-48
ElseIf c>=65 And c<=70
n:+c-55
ElseIf c>=97 And c<=102
n:+c-87
EndIf
Next
i:+4
Return Chr(n)
End Method

Method parsevalue:jsonvalue()
If Not getnext(["~q","-","0","1","2","3","4","5","6","7","8","9","{","[","t","f","n"])
Print "error - expecting the beginning of a value"
EndIf
Select curchr
Case "~q"
s$=parsestring()
Return jsonstringvalue.Create(s,0)
Case "-","0","1","2","3","4","5","6","7","8","9"
n:Double=parsenumber()
Return jsonnumbervalue.Create(n)
Case "{"
o:jsonobject=parseobject()
Return o
Case "["
a:jsonarray=parsearray()
Return a
Case "t"
i:+3
Return jsonliteralvalue.Create(1)
Case "f"
i:+4
Return jsonliteralvalue.Create(0)
Case "n"
i:+2
Return jsonliteralvalue.Create(-1)
End Select
End Method

Method parsenumber:Double()
i:-1
sign=1
n:Double=0
Select Chr(txt[i])
Case "-"
i:+2
Return parsenumber()*(-1)
Case "0"
i:+1
If getnext(["."])
n=parsefraction()
EndIf
Case "1","2","3","4","5","6","7","8","9"
n=parseinteger()
If getnext(["."])
n:+parsefraction()
EndIf
End Select

If Chr(txt[i])="e" Or Chr(txt[i])="E"
i:+1
Select Chr(txt[i])
Case "+"
sign=1
Case "-"
sign=-1
Default
Print "error - not a + or - when reading exponent in number"
End Select
e=parseinteger()
n:*10^(sign*e)
EndIf
Print "parsed number "+String(n)
Return n
End Method

Method parsefraction:Double()
digits=0
n:Double=0
While txt[i]>=48 And txt[i]<=57 And i<Len(txt)
n:*10
n:+txt[i]-48
i:+1
digits:+1
Wend
n:/(10^digits)
If i=Len(txt)
Print "error - reached EOF while reading number"
EndIf
Print "parsed fraction "+String(n)
Return n
End Method

Method parseinteger:Double()
n:Double=0
While txt[i]>=48 And txt[i]<=57 And i<Len(txt)
n:*10
n:+txt[i]-48
i:+1
Wend
If i=Len(txt)
Print "error - reached EOF while reading number"
EndIf
Print "parsed integer "+String(n)
Return n
End Method

End Type

Type jsonvalue

Method repr$(tabs$="")
Return tabs
End Method
End Type

Type jsonobject Extends jsonvalue
Field pairs:TList

Method New()
pairs=New TList
End Method

Method addnewpair(txt$,value:jsonvalue)
pairs.addlast jsonpair.Create(txt,value)
End Method

Method repr$(tabs$="")
t$="{"
ntabs$=tabs+"~t"
op:jsonpair=Null
For p:jsonpair=EachIn pairs
If op Then t:+","
t:+"~n"+ntabs+p.repr(ntabs)
op=p
Next
t:+"~n"+tabs+"}"
Return t
End Method

Method getvalue:jsonvalue(name$)
For p:jsonpair=EachIn pairs
If p.name=name
Return p.value
EndIf
Next
End Method

Method getstringvalue$(name$)
v:jsonstringvalue=jsonstringvalue(getvalue(name))
If v
Return v.txt
EndIf
End Method

Method getnumbervalue:Double(name$)
v:jsonnumbervalue=jsonnumbervalue(getvalue(name))
If v
Return v.number
EndIf
End Method

Method getliteralvalue(name$)
v:jsonliteralvalue=jsonliteralvalue(getvalue(name))
If v
Return v.value
EndIf
End Method

Method getarrayvalue:jsonarray(name$)
v:jsonarray=jsonarray(getvalue(name))
Return v
End Method

Method getobjectvalue:jsonobject(name$)
v:jsonobject=jsonobject(getvalue(name))
Return v
End Method


End Type

Type jsonpair
Field name$,value:jsonvalue

Function Create:jsonpair(name$,value:jsonvalue)
p:jsonpair=New jsonpair
p.name=name
p.value=value
Return p
End Function

Method repr$(tabs$="")
t$="~q"+name+"~q : "
For i=1 To (Len(t)+7)/8
tabs:+"~t"
Next
middo$=""
For i=1 To (8-(Len(t) Mod 8))
middo:+" "
Next
Return t+middo+value.repr(tabs)
End Method
End Type

Type jsonarray Extends jsonvalue
Field values:TList

Method New()
values=New TList
End Method

Method repr$(tabs$="")
t$="["
ntabs$=tabs+"~t"
ov:jsonvalue=Null
For v:jsonvalue=EachIn values
If ov Then t:+","
t:+"~n"+ntabs+v.repr(ntabs)
ov=v
Next
t:+"~n"+tabs+"]"
Return t
End Method
End Type


Type jsonstringvalue Extends jsonvalue
Field txt$

Function Create:jsonstringvalue(txt$,pretty=1)
jsv:jsonstringvalue=New jsonstringvalue

If pretty
otxt$=""
i=0
For i=0 To Len(txt)-1
Select Chr(txt[i])
Case "~q"
otxt:+"\~q"
Case "\"
otxt:+"\\"
Case "/"
otxt:+"\/"
Case Chr(8)
otxt:+"\b"
Case Chr(12)
otxt:+"\f"
Case "~n"
otxt:+"\n"
Case "~r"
otxt:+"\r"
Case "~t"
otxt:+"\t"
Default
otxt:+Chr(txt[i])
End Select
Next
jsv.txt=otxt
Else
jsv.txt=txt
EndIf
Return jsv
End Function

Method repr$(tabs$="")
Return "~q"+txt+"~q"
End Method
End Type

Type jsonnumbervalue Extends jsonvalue
Field number:Double

Function Create:jsonnumbervalue(n:Double)
jnv:jsonnumbervalue=New jsonnumbervalue
jnv.number=n
Return jnv
End Function

Method repr$(tabs$="")
Return String(number)
End Method
End Type

Type jsonliteralvalue Extends jsonvalue
Field value
'1 - true
'0 - false
'-1 - nil

Function Create:jsonliteralvalue(value)
jlv:jsonliteralvalue=New jsonliteralvalue
jlv.value=value
Return jlv
End Function

Method repr$(tabs$="")
Select value
Case 1
Return "true"
Case 0
Return "false"
Case -1
Return "nil"
End Select
End Method
End Type



'EXAMPLE
f:TStream=ReadFile("json2.txt")
txt$=""
While Not Eof(f)
txt:+f.ReadLine()
Wend

j:jsondecoder=jsondecoder.Create(txt)
j.parse()


For v:jsonvalue=EachIn j.things
Print v.repr()
Next
Something went wrong with that request. Please try again.