json.monkey2 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. Local data:=_data
  234. data=data.Replace( "\","\\" )
  235. data=data.Replace( "~q","\~q" )
  236. Return "~q"+data+"~q"
  237. End
  238. Private
  239. Field _data:String
  240. End
  241. #rem monkeydoc JsonArray class.
  242. #end
  243. Class JsonArray Extends JsonValue
  244. Method New( length:Int=0 )
  245. _data=New Stack<JsonValue>( length )
  246. End
  247. Method New( data:JsonValue[] )
  248. _data=New Stack<JsonValue>( data )
  249. End
  250. Method New( data:Stack<JsonValue> )
  251. _data=data
  252. End
  253. #rem monkeydoc @hidden Deprecated!
  254. #end
  255. Property Data:Stack<JsonValue>()
  256. Return _data
  257. Setter( data:Stack<JsonValue> )
  258. _data=data
  259. End
  260. #rem monkeydoc Returns true.
  261. #end
  262. Property IsArray:Bool() Override
  263. Return True
  264. End
  265. #rem monkeydoc Gets the internal stack used to store the array.
  266. #end
  267. Method ToArray:Stack<JsonValue>() Override
  268. Return _data
  269. End
  270. #rem monkeydoc Gets an array element.
  271. Returns the json value at `index`.
  272. @param index Array index.
  273. #end
  274. Method GetValue:JsonValue( index:int )
  275. Assert( index>=0 And index<_data.Length )
  276. Return _data[index]
  277. End
  278. #rem monkeydoc Gets a bool array element.
  279. Returns the bool value at `index`, or false if `index` is out of range or the value at `index` is not a bool.
  280. @param index Array index.
  281. #end
  282. Method GetBool:Bool( index:int )
  283. Local value:=GetValue( index )
  284. If value Return value.ToBool()
  285. Return False
  286. End
  287. #rem monkeydoc Gets a numeric array element.
  288. Returns the numeric value at `index`, or 0 if `index` is out of range or the value at `index` is not numeric.
  289. @param index Array index.
  290. #end
  291. Method GetNumber:Double( index:int )
  292. Local value:=GetValue( index )
  293. If value Return value.ToNumber()
  294. Return 0
  295. End
  296. #rem monkeydoc Gets a string array element.
  297. 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.
  298. @param index Array index.
  299. #end
  300. Method GetString:String( index:Int )
  301. Local value:=GetValue( index )
  302. If value Return value.ToString()
  303. Return ""
  304. End
  305. #rem monkeydoc Gets an array array element.
  306. Returns the array at `index`, or null if `index` is out of range or the value at `index` is not an array.
  307. @param index Array index.
  308. #end
  309. Method GetArray:JsonArray( index:int )
  310. Return Cast<JsonArray>( GetValue( index ) )
  311. End
  312. #rem monkeydoc Gets an object array element.
  313. Returns the object at `index`, or null if `index` is out of range or the value at `index` is not an object.
  314. @param index Array index.
  315. #end
  316. Method GetObject:JsonObject( index:Int )
  317. Return Cast<JsonObject>( GetValue( index ) )
  318. End
  319. #rem monkeydoc Sets an array element.
  320. Sets the json value at `index` to `value`.
  321. @param index Array index.
  322. @param value Value to set.
  323. #end
  324. Method SetValue( index:Int,value:JsonValue )
  325. Assert( index>=0 And index<_data.Length )
  326. _data[index]=value
  327. End
  328. #rem monkeydoc Sets an array element.
  329. Sets the json value at `index` to `value`.
  330. @param index Array index.
  331. @param value Value to set.
  332. #end
  333. Method SetBool( index:Int,value:Bool )
  334. SetValue( index,value ? JsonBool.TrueValue Else JsonBool.FalseValue )
  335. End
  336. #rem monkeydoc Sets an array element.
  337. Sets the json value at `index` to `value`.
  338. @param index Array index.
  339. @param value Value to set.
  340. #end
  341. Method SetNumber( index:int,value:Double )
  342. SetValue( index,New JsonNumber( value ) )
  343. End
  344. #rem monkeydoc Sets an array element.
  345. Sets the json value at `index` to `value`.
  346. @param index Array index.
  347. @param value Value to set.
  348. #end
  349. Method SetString( index:Int,value:String )
  350. SetValue( index,New JsonString( value ) )
  351. End
  352. #rem monkeydoc Sets an array element.
  353. Sets the json value at `index` to `value`.
  354. @param index Array index.
  355. @param value Value to set.
  356. #end
  357. Method SetArray( index:int,value:JsonValue[] )
  358. SetValue( index,New JsonArray( value ) )
  359. End
  360. Method SetArray( index:int,value:Stack<JsonValue> )
  361. SetValue( index,New JsonArray( value ) )
  362. End
  363. #rem monkeydoc Sets an array element.
  364. Sets the json value at `index` to `value`.
  365. @param index Array index.
  366. @param value Value to set.
  367. #end
  368. Method SetObject( index:Int,value:StringMap<JsonValue> )
  369. SetValue( index,New JsonObject( value ) )
  370. End
  371. #rem monkeydoc True if array is empty.
  372. #end
  373. Property Empty:Bool()
  374. Return _data.Empty
  375. End
  376. #rem monkeydoc Length of the array.
  377. #end
  378. Property Length:Int()
  379. Return _data.Length
  380. End
  381. #rem monkeydoc Gets the json value at an index.
  382. Returns the json value at `index`, or null if `index` is out of range.
  383. @param index The index.
  384. @return The json value at `index`.
  385. #end
  386. Operator[]:JsonValue( index:Int )
  387. If index<0 Or index>=_data.Length Return null
  388. Return _data[index]
  389. End
  390. #rem monkeydoc Sets the json value at an index.
  391. Sets the json value at `index` to `value`, or does nothing if `index` is out of range.
  392. @param index The index.
  393. @param value The json value to store at `index`.
  394. #end
  395. Operator[]=( index:Int,value:JsonValue )
  396. If index<0 Or index>=_data.Length Return
  397. DebugAssert( index>=0 )
  398. If index>=_data.Length _data.Resize( index+1 )
  399. _data[index]=value
  400. End
  401. #rem monkeydoc Gets an iterator to all json values in the array.
  402. #end
  403. Method All:Stack<JsonValue>.Iterator()
  404. Return _data.All()
  405. End
  406. #rem monkeydoc Adds a json value to the array.
  407. @param value The json value to add.
  408. #end
  409. Method Push( value:JsonValue )
  410. _data.Push( value )
  411. End
  412. #rem monkeydoc @hidden
  413. #end
  414. Method Add( value:JsonValue )
  415. _data.Add( value )
  416. End
  417. Private
  418. Field _data:Stack<JsonValue>
  419. Method PushJson:Void( buf:StringStack ) Override
  420. buf.Push( "[" )
  421. Local indent:=_indent
  422. Local n:=0
  423. For Local value:=Eachin _data
  424. If n
  425. buf.Push( "," )
  426. Endif
  427. n+=1
  428. If value
  429. buf.Push( value.ToJson() )
  430. Else
  431. buf.Push( "null" )
  432. Endif
  433. Next
  434. _indent=indent
  435. buf.Push( "]" )
  436. End
  437. End
  438. #rem monkeydoc JsonObject class.
  439. #end
  440. Class JsonObject Extends JsonValue
  441. Method New( data:StringMap<JsonValue> =Null )
  442. If Not data data=New StringMap<JsonValue>
  443. _data=data
  444. End
  445. #rem monkeydoc @hidden Deprecated!
  446. #end
  447. Property Data:StringMap<JsonValue>()
  448. Return _data
  449. Setter( data:StringMap<JsonValue> )
  450. _data=data
  451. End
  452. #rem monkeydoc Returns True.
  453. #end
  454. Property IsObject:Bool() Override
  455. Return True
  456. End
  457. #rem monkeydoc Gets the internal string map used to store the object.
  458. #end
  459. Method ToObject:StringMap<JsonValue>() Override
  460. Return _data
  461. End
  462. #rem monkeydoc Gets the value associated with a key.
  463. Returns the json value associated with `key`, or null if `key` does not exist.
  464. @param index Object key.
  465. #end
  466. Method GetValue:JsonValue( key:String )
  467. Return _data[key]
  468. End
  469. #rem monkeydoc Gets a bool from an object.
  470. Returns the bool value associated with `key`, or false if `key` does not exist or the value associated with `key` is not a bool.
  471. @param key Object key.
  472. #end
  473. Method GetBool:Bool( key:String )
  474. Local value:=GetValue( key )
  475. If value Return value.ToBool()
  476. Return False
  477. End
  478. #rem monkeydoc Gets a number from an object.
  479. Returns the numeric value associated with `key`, or 0 if `key` does not exist or the value associated with `key` is not numeric.
  480. @param key Object key.
  481. #end
  482. Method GetNumber:Double( key:String )
  483. Local value:=GetValue( key )
  484. If value Return value.ToNumber()
  485. Return 0
  486. End
  487. #rem monkeydoc Gets a string from an object.
  488. 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.
  489. @param key Object key.
  490. #end
  491. Method GetString:String( key:String )
  492. Local value:=GetValue( key )
  493. If value Return value.ToString()
  494. Return ""
  495. End
  496. #rem monkeydoc Gets an array from an object.
  497. Returns the json array associated with `key`, or null if `key` does not exist or the value associated with `key` is not an array.
  498. @param key Object key.
  499. #end
  500. Method GetArray:JsonArray( key:String )
  501. Return Cast<JsonArray>( GetValue( key ) )
  502. End
  503. #rem monkeydoc Gets an object from the object.
  504. Returns the json object associated with `key`, or null if `key` does not exist or the value associated with `key` is not an object.
  505. @param key Object key.
  506. @return A json object.
  507. #end
  508. Method GetObject:JsonObject( key:String )
  509. Return Cast<JsonObject>( GetValue( key ) )
  510. End
  511. #rem monkeydoc Sets a key to a value.
  512. Sets the value associated with `key` in the object.
  513. Any previous value associated with `key` is overwritten.
  514. @param key Object key.
  515. @param value Value to set.
  516. #end
  517. Method SetValue( key:String,value:JsonValue )
  518. _data[key]=value
  519. End
  520. #rem monkeydoc Sets a key to a value.
  521. Sets the value associated with `key` in the object.
  522. Any previous value associated with `key` is overwritten.
  523. @param key Object key.
  524. @param value Value to set.
  525. #end
  526. Method SetBool( key:String,value:Bool )
  527. _data[key]=value ? JsonBool.TrueValue Else JsonBool.FalseValue
  528. End
  529. #rem monkeydoc Sets a key to a value.
  530. Sets the value associated with `key` in the object.
  531. Any previous value associated with `key` is overwritten.
  532. @param key Object key.
  533. @param value Value to set.
  534. #end
  535. Method SetNumber( key:String,value:Double )
  536. _data[key]=New JsonNumber( value )
  537. End
  538. #rem monkeydoc Sets a key to a value.
  539. Sets the value associated with `key` in the object.
  540. Any previous value associated with `key` is overwritten.
  541. @param key Object key.
  542. @param value Value to set.
  543. #end
  544. Method SetString( key:String,value:String )
  545. _data[key]=New JsonString( value )
  546. End
  547. #rem monkeydoc Sets a key to a value.
  548. Sets the value associated with `key` in the object.
  549. Any previous value associated with `key` is overwritten.
  550. @param key Object key.
  551. @param value Value to set.
  552. #end
  553. Method SetArray( key:String,value:JsonValue[] )
  554. _data[key]=New JsonArray( value )
  555. End
  556. Method SetArray( key:String,value:Stack<JsonValue> )
  557. _data[key]=New JsonArray( value )
  558. End
  559. #rem monkeydoc Sets a key to a value.
  560. Sets the value associated with `key` in the object.
  561. Any previous value associated with `key` is overwritten.
  562. @param key Object key.
  563. @param value Value to set.
  564. #end
  565. Method SetObject( key:String,value:StringMap<JsonValue> )
  566. _data[key]=New JsonObject( value )
  567. End
  568. #rem monkeydoc True if object is empty.
  569. #end
  570. Property Empty:Bool()
  571. Return _data.Empty
  572. End
  573. #rem monkeydoc Counts the number of keys in the object.
  574. Returns the number of keys contained in the object.
  575. @return The number of keys.
  576. #end
  577. Method Count:Int()
  578. Return _data.Count()
  579. End
  580. #rem monkeydoc Checks if the object contains a key.
  581. Returns true if object contains `key`.
  582. @param key Key to check for.
  583. @return True if object contains `key`.
  584. #end
  585. Method Contains:Bool( key:String )
  586. Return _data.Contains( key )
  587. End
  588. #rem monkeydoc Gets an iterator to all json values in the object.
  589. #end
  590. Method All:StringMap<JsonValue>.Iterator()
  591. Return _data.All()
  592. End
  593. #rem monkeydoc Gets the json value associated with a key.
  594. Returns the json value associated with `key`, or null if `key` does not exist.
  595. @param key The key.
  596. @return The json value associated with `key`.
  597. #end
  598. Operator[]:JsonValue( key:String )
  599. Return _data[key]
  600. End
  601. #rem monkeydoc Sets the json value in the object.
  602. Sets the json value associated with `key` to `value`.
  603. @param key The key.
  604. @param value The json value to associate with `key`.
  605. #end
  606. Operator[]=( key:String,value:JsonValue )
  607. _data[key]=value
  608. End
  609. Function Load:JsonObject( path:String,throwex:Bool=False )
  610. Local src:=std.stringio.LoadString( path )
  611. If Not src Return Null
  612. Return Parse( src,throwex )
  613. End
  614. Function Parse:JsonObject( json:String,throwex:Bool=False )
  615. Try
  616. Local parser:=New JsonParser( json )
  617. Return New JsonObject( parser.ParseObject() )
  618. Catch ex:JsonError
  619. If throwex Throw ex
  620. End
  621. Return Null
  622. End
  623. Private
  624. Field _data:StringMap<JsonValue>
  625. Method PushJson:Void( buf:StringStack ) Override
  626. buf.Push( "{~n" )
  627. _indent+="~t"
  628. Local t:=False
  629. For Local it:=Eachin _data
  630. If t buf.Push( ",~n" )
  631. buf.Push( _indent+"~q"+it.Key.Replace( "~q","\~q" )+"~q:" )
  632. If it.Value
  633. buf.Push( it.Value.ToJson() )
  634. Else
  635. buf.Push( "null" )
  636. Endif
  637. t=True
  638. Next
  639. _indent=_indent.Slice( 0,-1 )
  640. buf.Push( "~n"+_indent+"}" )
  641. End
  642. End
  643. #rem monkeydoc JsonParser class.
  644. #end
  645. Class JsonParser
  646. Method New( json:String )
  647. _text=json
  648. Bump()
  649. End
  650. Method ParseValue:JsonValue()
  651. If TokeType=T_STRING Return New JsonString( ParseString() )
  652. If TokeType=T_NUMBER Return New JsonNumber( ParseNumber() )
  653. If Toke="{" Return New JsonObject( ParseObject() )
  654. If Toke="[" Return New JsonArray( ParseArray() )
  655. If CParse( "true" ) Return JsonBool.TrueValue
  656. If CParse( "false" ) Return JsonBool.FalseValue
  657. If CParse( "null" ) Return JsonValue.NullValue
  658. Return Null
  659. End
  660. Private
  661. Const T_EOF:=0
  662. Const T_STRING:=1
  663. Const T_NUMBER:=2
  664. Const T_SYMBOL:=3
  665. Const T_IDENT:=4
  666. Field _text:String
  667. Field _toke:String
  668. Field _type:Int
  669. Field _pos:Int
  670. Method GetChar:Int()
  671. If _pos=_text.Length Throw New JsonError()
  672. _pos+=1
  673. Return _text[_pos-1]
  674. End
  675. Method PeekChar:Int()
  676. If _pos=_text.Length Return 0
  677. Return _text[_pos]
  678. End
  679. Method ParseChar:Void( chr:Int )
  680. If _pos>=_text.Length Or _text[_pos]<>chr Throw New JsonError()
  681. _pos+=1
  682. End
  683. Method CParseChar:Bool( chr:Int )
  684. If _pos>=_text.Length Or _text[_pos]<>chr Return False
  685. _pos+=1
  686. Return True
  687. End
  688. Method CParseDigits:Bool()
  689. Local p:=_pos
  690. While _pos<_text.Length And _text[_pos]>=48 And _text[_pos]<=57
  691. _pos+=1
  692. Wend
  693. Return _pos>p
  694. End
  695. Method Bump:String()
  696. While _pos<_text.Length
  697. If _text[_pos]=47
  698. '// comment? - can't live without 'em!
  699. If _pos+1=_text.Length Or _text[_pos+1]<>47 Exit
  700. _pos+=2
  701. While _pos<_text.Length And _text[_pos]<>10
  702. _pos+=1
  703. Wend
  704. If _pos<_text.Length _pos+=1
  705. Else
  706. If _text[_pos]>32 Exit
  707. _pos+=1
  708. Endif
  709. Wend
  710. If _pos=_text.Length
  711. _toke=""
  712. _type=T_EOF
  713. Return _toke
  714. Endif
  715. Local pos:=_pos
  716. Local chr:=GetChar()
  717. If chr=34
  718. Repeat
  719. Local chr:=GetChar()
  720. If chr=34 Exit
  721. If chr=92 GetChar()
  722. Forever
  723. _type=T_STRING
  724. Else If chr=39
  725. Repeat
  726. Local chr:=GetChar()
  727. If chr=39 Exit
  728. If chr=92 GetChar()
  729. Forever
  730. _type=T_STRING
  731. Else If (chr>=48 And chr<=57) Or chr=45
  732. If chr=45 '-
  733. chr=GetChar()
  734. If chr<48 Or chr>57 Throw New JsonError()
  735. Endif
  736. If chr<>48 '0
  737. CParseDigits()
  738. End
  739. If CParseChar( 46 ) '.
  740. CParseDigits()
  741. Endif
  742. If CParseChar( 69 ) Or CParseChar( 101 ) 'e E
  743. If PeekChar()=43 Or PeekChar()=45 GetChar() '+ -
  744. If Not CParseDigits() Throw New JsonError()
  745. Endif
  746. _type=T_NUMBER
  747. Else If (chr>=65 And chr<91) Or (chr>=97 And chr<123) Or chr=95
  748. chr=PeekChar()
  749. While (chr>=65 And chr<91) Or (chr>=97 And chr<123) Or (chr>=48 And chr<58) Or chr=95
  750. GetChar()
  751. chr=PeekChar()
  752. Wend
  753. _type=T_IDENT
  754. Else
  755. _type=T_SYMBOL
  756. Endif
  757. _toke=_text.Slice( pos,_pos )
  758. Return _toke
  759. End
  760. Property Toke:String()
  761. Return _toke
  762. End
  763. Property TokeType:Int()
  764. Return _type
  765. End
  766. Method CParse:Bool( toke:String )
  767. If toke<>_toke Return False
  768. Bump()
  769. Return True
  770. End
  771. Method Parse:Void( toke:String )
  772. If Not CParse( toke ) Throw New JsonError()
  773. End
  774. Method ParseObject:StringMap<JsonValue>()
  775. Parse( "{" )
  776. Local map:=New StringMap<JsonValue>
  777. If CParse( "}" ) Return map
  778. Repeat
  779. Local name:=Toke
  780. If TokeType=T_IDENT
  781. Bump()
  782. Else
  783. name=ParseString()
  784. Endif
  785. Parse( ":" )
  786. Local value:=ParseValue()
  787. map.Set( name,value )
  788. Until Not CParse( "," )
  789. Parse( "}" )
  790. Return map
  791. End
  792. Method ParseArray:Stack<JsonValue>()
  793. Parse( "[" )
  794. Local stack:=New Stack<JsonValue>
  795. If CParse( "]" ) Return stack
  796. Repeat
  797. Local value:=ParseValue()
  798. stack.Add( value )
  799. Until Not CParse( "," )
  800. Parse( "]" )
  801. Return stack
  802. End
  803. Method ParseString:String()
  804. If TokeType<>T_STRING Throw New JsonError()
  805. Local toke:=Toke.Slice( 1,-1 )
  806. Local i:=toke.Find( "\" )
  807. If i<>-1
  808. Local frags:=New StringStack,p:=0,esc:=""
  809. Repeat
  810. If i+1>=toke.Length Throw New JsonError()
  811. frags.Push( toke.Slice( p,i ) )
  812. Select toke[i+1]
  813. Case 34 esc="~q" '\"
  814. Case 92 esc="\" '\\
  815. Case 47 esc="/" '\/
  816. Case 98 esc=String.FromChar( 8 ) '\b
  817. Case 102 esc=String.FromChar( 12 ) '\f
  818. Case 114 esc=String.FromChar( 13 ) '\r
  819. Case 110 esc=String.FromChar( 10 ) '\n
  820. Case 117 '\uxxxx
  821. If i+6>toke.Length Throw New JsonError()
  822. Local val:=0
  823. For Local j:=2 Until 6
  824. Local chr:=toke[i+j]
  825. If chr>=48 And chr<58
  826. val=val Shl 4 | (chr-48)
  827. Else If chr>=65 And chr<123
  828. chr&=31
  829. If chr<1 Or chr>6 Throw New JsonError()
  830. val=val Shl 4 | (chr+9)
  831. Else
  832. Throw New JsonError()
  833. Endif
  834. Next
  835. esc=String.FromChar( val )
  836. i+=4
  837. Default
  838. Throw New JsonError()
  839. End
  840. frags.Push( esc )
  841. p=i+2
  842. i=toke.Find( "\",p )
  843. If i<>-1 Continue
  844. frags.Push( toke.Slice( p ) )
  845. Exit
  846. Forever
  847. toke=frags.Join( "" )
  848. Endif
  849. Bump()
  850. Return toke
  851. End
  852. Method ParseNumber:Double()
  853. If TokeType<>T_NUMBER Throw New JsonError()
  854. Local toke:=Toke
  855. Bump()
  856. Return Double( toke )
  857. End
  858. End