json.monkey2 23 KB

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