json.monkey2 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #rem monkeydoc
  2. JSON (JavaScript Object Notation) is a lightweight data-interchange format.
  3. To load a json object from a file, use the [[JsonObject.Load]] function.
  4. To convert a string to a json object, use the [[JsonObject.Parse]] function.
  5. To convert a json object to a string, use the [[JsonObject.ToString]] method.
  6. You can inspect the members of a json object using methods such as [[JsonObject.GetString]], [[JsonObject.GetNumber]] etc.
  7. You can modify the members of a json object using methods such as [[JsonObject.SetString]], [[JsonObject.SetNumber]] etc.
  8. #end
  9. Namespace std.json
  10. #rem monkeydoc JsonError class.
  11. #end
  12. Class JsonError Extends Throwable
  13. End
  14. #rem monkeydoc JsonValue class.
  15. This is base class of all JsonValue types.
  16. #end
  17. Class JsonValue
  18. Const NullValue:=New JsonValue
  19. #rem monkeydoc True if value is null.
  20. #end
  21. Property IsNull:Bool()
  22. Return Self=NullValue
  23. End
  24. #rem monkeydoc True if value is a bool.
  25. #end
  26. Property IsBool:Bool() Virtual
  27. Return False
  28. End
  29. #rem monkeydoc True if value is a number.
  30. #end
  31. Property IsNumber:Bool() Virtual
  32. Return False
  33. End
  34. #rem monkeydoc True if value is a string.
  35. #end
  36. Property IsString:Bool() Virtual
  37. Return False
  38. End
  39. #rem monkeydoc True if value is an array.
  40. #end
  41. Property IsArray:Bool() Virtual
  42. Return False
  43. End
  44. #rem monkeydoc True if value is an object.
  45. #end
  46. Property IsObject:Bool() Virtual
  47. Return False
  48. End
  49. #rem monkeydoc Gets bool value.
  50. If the value is a bool, returns its actual value.
  51. If the value is not a bool, false is returned by default.
  52. #end
  53. Method ToBool:Bool() Virtual
  54. Return False
  55. End
  56. #rem monkeydoc Gets number value.
  57. If the value is a number, returns its actual value.
  58. If the value is not a number, 0 is returned by default.
  59. #end
  60. Method ToNumber:Double() Virtual
  61. Return 0
  62. End
  63. #rem monkeydoc Gets string value.
  64. If the value is a string, returns its actual value.
  65. If the value is not a string, "" is returned by default.
  66. #end
  67. Method ToString:String() Virtual
  68. Return ""
  69. End
  70. #rem monkeydoc Gets array value.
  71. If the value is an array, returns its actual value in the form of a Stack<JsonValue> object.
  72. If the value is not an array, null is returned by default.
  73. #end
  74. Method ToArray:Stack<JsonValue>() Virtual
  75. Return Null
  76. End
  77. #rem monkeydoc Gets object value.
  78. If the value is an object, returns its actual value in the form of a StringMap<JsonValue> object.
  79. If the value is not an object, null is returned by default.
  80. #end
  81. Method ToObject:StringMap<JsonValue>() Virtual
  82. Return Null
  83. End
  84. #rem monkeydoc Converts the value to a JSON string.
  85. #end
  86. Method ToJson:String() Virtual
  87. Local buf:=New StringStack
  88. PushJson( buf )
  89. Return buf.Join( "" )
  90. End
  91. #rem monkeydoc Saves the value as a JSON string to a file.
  92. #end
  93. Method Save:Bool( path:String )
  94. Local buf:=New StringStack
  95. PushJson( buf )
  96. Local src:=buf.Join( "" )
  97. Return stringio.SaveString( src,path )
  98. End
  99. #rem monkeydoc Load the value from a JSON string stored in a file.
  100. #end
  101. Function Load:JsonValue( path:String,throwex:Bool=False )
  102. Local src:=stringio.LoadString( path )
  103. If Not src Return Null
  104. Return Parse( src,throwex )
  105. End
  106. #rem monkeydoc Creates a JSONValue from a JSON string.
  107. Returns null if the json could not be parsed.
  108. #end
  109. Function Parse:JsonValue( src:String,throwex:Bool=False )
  110. Try
  111. Local parser:=New JsonParser( src )
  112. Return parser.ParseValue()
  113. Catch ex:JsonError
  114. If throwex Throw ex
  115. End
  116. Return Null
  117. End
  118. Protected
  119. Global _indent:String
  120. #rem monkeydoc @hidden
  121. #end
  122. Method New()
  123. End
  124. #rem monkeydoc @hidden
  125. #end
  126. Method PushJson:Void( buf:StringStack ) Virtual
  127. buf.Push( ToJson() )
  128. End
  129. End
  130. #rem monkeydoc JsonBool class.
  131. #end
  132. Class JsonBool Extends JsonValue
  133. Const TrueValue:JsonBool=New JsonBool( True )
  134. Const FalseValue:JsonBool=New JsonBool( False )
  135. Method New( data:Bool=False )
  136. _data=data
  137. End
  138. #rem monkeydoc @hidden Deprecated!
  139. #end
  140. Property Data:Bool()
  141. Return _data
  142. Setter( data:Bool )
  143. _data=data
  144. End
  145. Property IsBool:Bool() Override
  146. Return True
  147. End
  148. Method ToBool:Bool() Override
  149. Return _data
  150. End
  151. #rem monkeydoc @hidden Deprecated!
  152. #end
  153. Method ToNumber:Double() Override
  154. Return _data
  155. End
  156. #rem monkeydoc @hidden Deprecated!
  157. #end
  158. Method ToString:String() Override
  159. Return _data ? "true" Else "false"
  160. End
  161. Method ToJson:String() Override
  162. If _data Return "true"
  163. Return "false"
  164. End
  165. Private
  166. Field _data:Bool
  167. End
  168. #rem monkeydoc JsonNumber class.
  169. #end
  170. Class JsonNumber Extends JsonValue
  171. Method New( data:Double=0 )
  172. _data=data
  173. End
  174. #rem monkeydoc @hidden Deprecated!
  175. #end
  176. Property Data:Double()
  177. Return _data
  178. Setter( data:Double )
  179. _data=data
  180. End
  181. Property IsNumber:Bool() Override
  182. Return True
  183. End
  184. Method ToNumber:Double() Override
  185. Return _data
  186. End
  187. #rem monkeydoc @hidden Deprecated!
  188. #end
  189. Method ToBool:Bool() Override
  190. Return _data
  191. End
  192. #rem monkeydoc @hidden Deprecated!
  193. #end
  194. Method ToString:String() Override
  195. Return _data
  196. End
  197. Method ToJson:String() Override
  198. Return String( _data )
  199. End
  200. Private
  201. Field _data:Double
  202. End
  203. #rem monkeydoc JsonString class.
  204. #end
  205. Class JsonString Extends JsonValue
  206. Method New( data:String="" )
  207. _data=data
  208. End
  209. #rem monkeydoc @hidden Deprecated!
  210. #end
  211. Property Data:String()
  212. Return _data
  213. Setter( data:String )
  214. _data=data
  215. End
  216. Property IsString:Bool() Override
  217. Return True
  218. End
  219. Method ToString:String() Override
  220. Return _data
  221. End
  222. #rem monkeydoc @hidden Deprecated!
  223. #end
  224. Method ToBool:Bool() Override
  225. Return _data
  226. End
  227. #rem monkeydoc @hidden Deprecated!
  228. #end
  229. Method ToNumber:Double() Override
  230. Return Double( _data )
  231. End
  232. Method ToJson:String() Override
  233. Return "~q"+_data.Replace( "~q","\~q" )+"~q"
  234. End
  235. Private
  236. Field _data:String
  237. End
  238. #rem monkeydoc JsonArray class.
  239. #end
  240. Class JsonArray Extends JsonValue
  241. Method New( length:Int=0 )
  242. _data=New Stack<JsonValue>( length )
  243. End
  244. Method New( data:JsonValue[] )
  245. _data=New Stack<JsonValue>( data )
  246. End
  247. Method New( data:Stack<JsonValue> )
  248. _data=data
  249. End
  250. #rem monkeydoc @hidden Deprecated!
  251. #end
  252. Property Data:Stack<JsonValue>()
  253. Return _data
  254. Setter( data:Stack<JsonValue> )
  255. _data=data
  256. End
  257. #rem monkeydoc Returns true.
  258. #end
  259. Property IsArray:Bool() Override
  260. Return True
  261. End
  262. #rem monkeydoc Gets the internal stack used to store the array.
  263. #end
  264. Method ToArray:Stack<JsonValue>() Override
  265. Return _data
  266. End
  267. #rem monkeydoc Gets an array element.
  268. Returns the json value at `index`.
  269. @param index Array index.
  270. #end
  271. Method GetValue:JsonValue( index:int )
  272. Assert( index>=0 And index<_data.Length )
  273. Return _data[index]
  274. End
  275. #rem monkeydoc Gets a bool array element.
  276. Returns the bool value at `index`, or false if `index` is out of range or the value at `index` is not a bool.
  277. @param index Array index.
  278. #end
  279. Method GetBool:Bool( index:int )
  280. Local value:=GetValue( index )
  281. If value Return value.ToBool()
  282. Return False
  283. End
  284. #rem monkeydoc Gets a numeric array element.
  285. Returns the numeric value at `index`, or 0 if `index` is out of range or the value at `index` is not numeric.
  286. @param index Array index.
  287. #end
  288. Method GetNumber:Double( index:int )
  289. Local value:=GetValue( index )
  290. If value Return value.ToNumber()
  291. Return 0
  292. End
  293. #rem monkeydoc Gets a string array element.
  294. Returns the string value at `index`, or an empty string if `index` is out of range or the value at `index` is not a string.
  295. @param index Array index.
  296. #end
  297. Method GetString:String( index:Int )
  298. Local value:=GetValue( index )
  299. If value Return value.ToString()
  300. Return ""
  301. End
  302. #rem monkeydoc Gets an array array element.
  303. Returns the array at `index`, or null if `index` is out of range or the value at `index` is not an array.
  304. @param index Array index.
  305. #end
  306. Method GetArray:JsonArray( index:int )
  307. Return Cast<JsonArray>( GetValue( index ) )
  308. End
  309. #rem monkeydoc Gets an object array element.
  310. Returns the object at `index`, or null if `index` is out of range or the value at `index` is not an object.
  311. @param index Array index.
  312. #end
  313. Method GetObject:JsonObject( index:Int )
  314. Return Cast<JsonObject>( GetValue( index ) )
  315. End
  316. #rem monkeydoc Sets an array element.
  317. Sets the json value at `index` to `value`.
  318. @param index Array index.
  319. @param value Value to set.
  320. #end
  321. Method SetValue( index:Int,value:JsonValue )
  322. Assert( index>=0 And index<_data.Length )
  323. _data[index]=value
  324. End
  325. #rem monkeydoc Sets an array element.
  326. Sets the json value at `index` to `value`.
  327. @param index Array index.
  328. @param value Value to set.
  329. #end
  330. Method SetBool( index:Int,value:Bool )
  331. SetValue( index,value ? JsonBool.TrueValue Else JsonBool.FalseValue )
  332. End
  333. #rem monkeydoc Sets an array element.
  334. Sets the json value at `index` to `value`.
  335. @param index Array index.
  336. @param value Value to set.
  337. #end
  338. Method SetNumber( index:int,value:Double )
  339. SetValue( index,New JsonNumber( value ) )
  340. End
  341. #rem monkeydoc Sets an array element.
  342. Sets the json value at `index` to `value`.
  343. @param index Array index.
  344. @param value Value to set.
  345. #end
  346. Method SetString( index:Int,value:String )
  347. SetValue( index,New JsonString( value ) )
  348. End
  349. #rem monkeydoc Sets an array element.
  350. Sets the json value at `index` to `value`.
  351. @param index Array index.
  352. @param value Value to set.
  353. #end
  354. Method SetArray( index:int,value:JsonValue[] )
  355. SetValue( index,New JsonArray( value ) )
  356. End
  357. Method SetArray( index:int,value:Stack<JsonValue> )
  358. SetValue( index,New JsonArray( value ) )
  359. End
  360. #rem monkeydoc Sets an array element.
  361. Sets the json value at `index` to `value`.
  362. @param index Array index.
  363. @param value Value to set.
  364. #end
  365. Method SetObject( index:Int,value:StringMap<JsonValue> )
  366. SetValue( index,New JsonObject( value ) )
  367. End
  368. #rem monkeydoc True if array is empty.
  369. #end
  370. Property Empty:Bool()
  371. Return _data.Empty
  372. End
  373. #rem monkeydoc Length of the array.
  374. #end
  375. Property Length:Int()
  376. Return _data.Length
  377. End
  378. #rem monkeydoc Gets the json value at an index.
  379. Returns the json value at `index`, or null if `index` is out of range.
  380. @param index The index.
  381. @return The json value at `index`.
  382. #end
  383. Operator[]:JsonValue( index:Int )
  384. If index<0 Or index>=_data.Length Return null
  385. Return _data[index]
  386. End
  387. #rem monkeydoc Sets the json value at an index.
  388. Sets the json value at `index` to `value`, or does nothing if `index` is out of range.
  389. @param index The index.
  390. @param value The json value to store at `index`.
  391. #end
  392. Operator[]=( index:Int,value:JsonValue )
  393. If index<0 Or index>=_data.Length Return
  394. DebugAssert( index>=0 )
  395. If index>=_data.Length _data.Resize( index+1 )
  396. _data[index]=value
  397. End
  398. #rem monkeydoc Gets an iterator to all json values in the array.
  399. #end
  400. Method All:Stack<JsonValue>.Iterator()
  401. Return _data.All()
  402. End
  403. #rem monkeydoc Adds a json value to the array.
  404. @param value The json value to add.
  405. #end
  406. Method Push( value:JsonValue )
  407. _data.Push( value )
  408. End
  409. #rem monkeydoc @hidden
  410. #end
  411. Method Add( value:JsonValue )
  412. _data.Add( value )
  413. End
  414. Private
  415. Field _data:Stack<JsonValue>
  416. Method PushJson:Void( buf:StringStack ) Override
  417. buf.Push( "[" )
  418. Local indent:=_indent
  419. Local n:=0
  420. For Local value:=Eachin _data
  421. If n
  422. buf.Push( "," )
  423. Endif
  424. n+=1
  425. If value
  426. buf.Push( value.ToJson() )
  427. Else
  428. buf.Push( "null" )
  429. Endif
  430. Next
  431. _indent=indent
  432. buf.Push( "]" )
  433. End
  434. End
  435. #rem monkeydoc JsonObject class.
  436. #end
  437. Class JsonObject Extends JsonValue
  438. Method New( data:StringMap<JsonValue> =Null )
  439. If Not data data=New StringMap<JsonValue>
  440. _data=data
  441. End
  442. #rem monkeydoc @hidden Deprecated!
  443. #end
  444. Property Data:StringMap<JsonValue>()
  445. Return _data
  446. Setter( data:StringMap<JsonValue> )
  447. _data=data
  448. End
  449. #rem monkeydoc Returns True.
  450. #end
  451. Property IsObject:Bool() Override
  452. Return True
  453. End
  454. #rem monkeydoc Gets the internal string map used to store the object.
  455. #end
  456. Method ToObject:StringMap<JsonValue>() Override
  457. Return _data
  458. End
  459. #rem monkeydoc Gets the value associated with a key.
  460. Returns the json value associated with `key`, or null if `key` does not exist.
  461. @param index Object key.
  462. #end
  463. Method GetValue:JsonValue( key:String )
  464. Return _data[key]
  465. End
  466. #rem monkeydoc Gets a bool from an object.
  467. Returns the bool value associated with `key`, or false if `key` does not exist or the value associated with `key` is not a bool.
  468. @param key Object key.
  469. #end
  470. Method GetBool:Bool( key:String )
  471. Local value:=GetValue( key )
  472. If value Return value.ToBool()
  473. Return False
  474. End
  475. #rem monkeydoc Gets a number from an object.
  476. Returns the numeric value associated with `key`, or 0 if `key` does not exist or the value associated with `key` is not numeric.
  477. @param key Object key.
  478. #end
  479. Method GetNumber:Double( key:String )
  480. Local value:=GetValue( key )
  481. If value Return value.ToNumber()
  482. Return 0
  483. End
  484. #rem monkeydoc Gets a string from an object.
  485. Returns the string value associated with `key`, or an empty string if `key` does not exist or the value associated with `key` is not a string.
  486. @param key Object key.
  487. #end
  488. Method GetString:String( key:String )
  489. Local value:=GetValue( key )
  490. If value Return value.ToString()
  491. Return ""
  492. End
  493. #rem monkeydoc Gets an array from an object.
  494. Returns the json array associated with `key`, or null if `key` does not exist or the value associated with `key` is not an array.
  495. @param key Object key.
  496. #end
  497. Method GetArray:JsonArray( key:String )
  498. Return Cast<JsonArray>( GetValue( key ) )
  499. End
  500. #rem monkeydoc Gets an object from the object.
  501. Returns the json object associated with `key`, or null if `key` does not exist or the value associated with `key` is not an object.
  502. @param key Object key.
  503. @return A json object.
  504. #end
  505. Method GetObject:JsonObject( key:String )
  506. Return Cast<JsonObject>( GetValue( key ) )
  507. End
  508. #rem monkeydoc Sets a key to a value.
  509. Sets the value associated with `key` in the object.
  510. Any previous value associated with `key` is overwritten.
  511. @param key Object key.
  512. @param value Value to set.
  513. #end
  514. Method SetValue( key:String,value:JsonValue )
  515. _data[key]=value
  516. End
  517. #rem monkeydoc Sets a key to a value.
  518. Sets the value associated with `key` in the object.
  519. Any previous value associated with `key` is overwritten.
  520. @param key Object key.
  521. @param value Value to set.
  522. #end
  523. Method SetBool( key:String,value:Bool )
  524. _data[key]=value ? JsonBool.TrueValue Else JsonBool.FalseValue
  525. End
  526. #rem monkeydoc Sets a key to a value.
  527. Sets the value associated with `key` in the object.
  528. Any previous value associated with `key` is overwritten.
  529. @param key Object key.
  530. @param value Value to set.
  531. #end
  532. Method SetNumber( key:String,value:Double )
  533. _data[key]=New JsonNumber( value )
  534. End
  535. #rem monkeydoc Sets a key to a value.
  536. Sets the value associated with `key` in the object.
  537. Any previous value associated with `key` is overwritten.
  538. @param key Object key.
  539. @param value Value to set.
  540. #end
  541. Method SetString( key:String,value:String )
  542. _data[key]=New JsonString( value )
  543. End
  544. #rem monkeydoc Sets a key to a value.
  545. Sets the value associated with `key` in the object.
  546. Any previous value associated with `key` is overwritten.
  547. @param key Object key.
  548. @param value Value to set.
  549. #end
  550. Method SetArray( key:String,value:JsonValue[] )
  551. _data[key]=New JsonArray( value )
  552. End
  553. Method SetArray( key:String,value:Stack<JsonValue> )
  554. _data[key]=New JsonArray( value )
  555. End
  556. #rem monkeydoc Sets a key to a value.
  557. Sets the value associated with `key` in the object.
  558. Any previous value associated with `key` is overwritten.
  559. @param key Object key.
  560. @param value Value to set.
  561. #end
  562. Method SetObject( key:String,value:StringMap<JsonValue> )
  563. _data[key]=New JsonObject( value )
  564. End
  565. #rem monkeydoc True if object is empty.
  566. #end
  567. Property Empty:Bool()
  568. Return _data.Empty
  569. End
  570. #rem monkeydoc Counts the number of keys in the object.
  571. Returns the number of keys contained in the object.
  572. @return The number of keys.
  573. #end
  574. Method Count:Int()
  575. Return _data.Count()
  576. End
  577. #rem monkeydoc Checks if the object contains a key.
  578. Returns true if object contains `key`.
  579. @param key Key to check for.
  580. @return True if object contains `key`.
  581. #end
  582. Method Contains:Bool( key:String )
  583. Return _data.Contains( key )
  584. End
  585. #rem monkeydoc Gets an iterator to all json values in the object.
  586. #end
  587. Method All:StringMap<JsonValue>.Iterator()
  588. Return _data.All()
  589. End
  590. #rem monkeydoc Gets the json value associated with a key.
  591. Returns the json value associated with `key`, or null if `key` does not exist.
  592. @param key The key.
  593. @return The json value associated with `key`.
  594. #end
  595. Operator[]:JsonValue( key:String )
  596. Return _data[key]
  597. End
  598. #rem monkeydoc Sets the json value in the object.
  599. Sets the json value associated with `key` to `value`.
  600. @param key The key.
  601. @param value The json value to associate with `key`.
  602. #end
  603. Operator[]=( key:String,value:JsonValue )
  604. _data[key]=value
  605. End
  606. Function Load:JsonObject( path:String,throwex:Bool=False )
  607. Local src:=std.stringio.LoadString( path )
  608. If Not src Return Null
  609. Return Parse( src,throwex )
  610. End
  611. Function Parse:JsonObject( json:String,throwex:Bool=False )
  612. Try
  613. Local parser:=New JsonParser( json )
  614. Return New JsonObject( parser.ParseObject() )
  615. Catch ex:JsonError
  616. If throwex Throw ex
  617. End
  618. Return Null
  619. End
  620. Private
  621. Field _data:StringMap<JsonValue>
  622. Method PushJson:Void( buf:StringStack ) Override
  623. buf.Push( "{~n" )
  624. _indent+="~t"
  625. Local t:=False
  626. For Local it:=Eachin _data
  627. If t buf.Push( ",~n" )
  628. buf.Push( _indent+"~q"+it.Key.Replace( "~q","\~q" )+"~q:" )
  629. If it.Value
  630. buf.Push( it.Value.ToJson() )
  631. Else
  632. buf.Push( "null" )
  633. Endif
  634. t=True
  635. Next
  636. _indent=_indent.Slice( 0,-1 )
  637. buf.Push( "~n"+_indent+"}" )
  638. End
  639. End
  640. #rem monkeydoc JsonParser class.
  641. #end
  642. Class JsonParser
  643. Method New( json:String )
  644. _text=json
  645. Bump()
  646. End
  647. Method ParseValue:JsonValue()
  648. If TokeType=T_STRING Return New JsonString( ParseString() )
  649. If TokeType=T_NUMBER Return New JsonNumber( ParseNumber() )
  650. If Toke="{" Return New JsonObject( ParseObject() )
  651. If Toke="[" Return New JsonArray( ParseArray() )
  652. If CParse( "true" ) Return JsonBool.TrueValue
  653. If CParse( "false" ) Return JsonBool.FalseValue
  654. If CParse( "null" ) Return JsonValue.NullValue
  655. Return Null
  656. End
  657. Private
  658. Const T_EOF:=0
  659. Const T_STRING:=1
  660. Const T_NUMBER:=2
  661. Const T_SYMBOL:=3
  662. Const T_IDENT:=4
  663. Field _text:String
  664. Field _toke:String
  665. Field _type:Int
  666. Field _pos:Int
  667. Method GetChar:Int()
  668. If _pos=_text.Length Throw New JsonError()
  669. _pos+=1
  670. Return _text[_pos-1]
  671. End
  672. Method PeekChar:Int()
  673. If _pos=_text.Length Return 0
  674. Return _text[_pos]
  675. End
  676. Method ParseChar:Void( chr:Int )
  677. If _pos>=_text.Length Or _text[_pos]<>chr Throw New JsonError()
  678. _pos+=1
  679. End
  680. Method CParseChar:Bool( chr:Int )
  681. If _pos>=_text.Length Or _text[_pos]<>chr Return False
  682. _pos+=1
  683. Return True
  684. End
  685. Method CParseDigits:Bool()
  686. Local p:=_pos
  687. While _pos<_text.Length And _text[_pos]>=48 And _text[_pos]<=57
  688. _pos+=1
  689. Wend
  690. Return _pos>p
  691. End
  692. Method Bump:String()
  693. While _pos<_text.Length
  694. If _text[_pos]=47
  695. '// comment? - can't live without 'em!
  696. If _pos+1=_text.Length Or _text[_pos+1]<>47 Exit
  697. _pos+=2
  698. While _pos<_text.Length And _text[_pos]<>10
  699. _pos+=1
  700. Wend
  701. If _pos<_text.Length _pos+=1
  702. Else
  703. If _text[_pos]>32 Exit
  704. _pos+=1
  705. Endif
  706. Wend
  707. If _pos=_text.Length
  708. _toke=""
  709. _type=T_EOF
  710. Return _toke
  711. Endif
  712. Local pos:=_pos
  713. Local chr:=GetChar()
  714. If chr=34
  715. Repeat
  716. Local chr:=GetChar()
  717. If chr=34 Exit
  718. If chr=92 GetChar()
  719. Forever
  720. _type=T_STRING
  721. Else If chr=39
  722. Repeat
  723. Local chr:=GetChar()
  724. If chr=39 Exit
  725. If chr=92 GetChar()
  726. Forever
  727. _type=T_STRING
  728. Else If (chr>=48 And chr<=57) Or chr=45
  729. If chr=45 '-
  730. chr=GetChar()
  731. If chr<48 Or chr>57 Throw New JsonError()
  732. Endif
  733. If chr<>48 '0
  734. CParseDigits()
  735. End
  736. If CParseChar( 46 ) '.
  737. CParseDigits()
  738. Endif
  739. If CParseChar( 69 ) Or CParseChar( 101 ) 'e E
  740. If PeekChar()=43 Or PeekChar()=45 GetChar() '+ -
  741. If Not CParseDigits() Throw New JsonError()
  742. Endif
  743. _type=T_NUMBER
  744. Else If (chr>=65 And chr<91) Or (chr>=97 And chr<123) Or chr=95
  745. chr=PeekChar()
  746. While (chr>=65 And chr<91) Or (chr>=97 And chr<123) Or (chr>=48 And chr<58) Or chr=95
  747. GetChar()
  748. chr=PeekChar()
  749. Wend
  750. _type=T_IDENT
  751. Else
  752. _type=T_SYMBOL
  753. Endif
  754. _toke=_text.Slice( pos,_pos )
  755. Return _toke
  756. End
  757. Property Toke:String()
  758. Return _toke
  759. End
  760. Property TokeType:Int()
  761. Return _type
  762. End
  763. Method CParse:Bool( toke:String )
  764. If toke<>_toke Return False
  765. Bump()
  766. Return True
  767. End
  768. Method Parse:Void( toke:String )
  769. If Not CParse( toke ) Throw New JsonError()
  770. End
  771. Method ParseObject:StringMap<JsonValue>()
  772. Parse( "{" )
  773. Local map:=New StringMap<JsonValue>
  774. If CParse( "}" ) Return map
  775. Repeat
  776. Local name:=Toke
  777. If TokeType=T_IDENT
  778. Bump()
  779. Else
  780. name=ParseString()
  781. Endif
  782. Parse( ":" )
  783. Local value:=ParseValue()
  784. map.Set( name,value )
  785. Until Not CParse( "," )
  786. Parse( "}" )
  787. Return map
  788. End
  789. Method ParseArray:Stack<JsonValue>()
  790. Parse( "[" )
  791. Local stack:=New Stack<JsonValue>
  792. If CParse( "]" ) Return stack
  793. Repeat
  794. Local value:=ParseValue()
  795. stack.Add( value )
  796. Until Not CParse( "," )
  797. Parse( "]" )
  798. Return stack
  799. End
  800. Method ParseString:String()
  801. If TokeType<>T_STRING Throw New JsonError()
  802. Local toke:=Toke.Slice( 1,-1 )
  803. Local i:=toke.Find( "\" )
  804. If i<>-1
  805. Local frags:=New StringStack,p:=0,esc:=""
  806. Repeat
  807. If i+1>=toke.Length Throw New JsonError()
  808. frags.Push( toke.Slice( p,i ) )
  809. Select toke[i+1]
  810. Case 34 esc="~q" '\"
  811. Case 92 esc="\" '\\
  812. Case 47 esc="/" '\/
  813. Case 98 esc=String.FromChar( 8 ) '\b
  814. Case 102 esc=String.FromChar( 12 ) '\f
  815. Case 114 esc=String.FromChar( 13 ) '\r
  816. Case 110 esc=String.FromChar( 10 ) '\n
  817. Case 117 '\uxxxx
  818. If i+6>toke.Length Throw New JsonError()
  819. Local val:=0
  820. For Local j:=2 Until 6
  821. Local chr:=toke[i+j]
  822. If chr>=48 And chr<58
  823. val=val Shl 4 | (chr-48)
  824. Else If chr>=65 And chr<123
  825. chr&=31
  826. If chr<1 Or chr>6 Throw New JsonError()
  827. val=val Shl 4 | (chr+9)
  828. Else
  829. Throw New JsonError()
  830. Endif
  831. Next
  832. esc=String.FromChar( val )
  833. i+=4
  834. Default
  835. Throw New JsonError()
  836. End
  837. frags.Push( esc )
  838. p=i+2
  839. i=toke.Find( "\",p )
  840. If i<>-1 Continue
  841. frags.Push( toke.Slice( p ) )
  842. Exit
  843. Forever
  844. toke=frags.Join( "" )
  845. Endif
  846. Bump()
  847. Return toke
  848. End
  849. Method ParseNumber:Double()
  850. If TokeType<>T_NUMBER Throw New JsonError()
  851. Local toke:=Toke
  852. Bump()
  853. Return Double( toke )
  854. End
  855. End