testjsondata.pp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. {
  2. This file is part of the Free Component Library
  3. JSON FPCUNit test for data structures
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. unit testjsondata;
  14. interface
  15. uses
  16. Classes, SysUtils, fpcunit, testutils, testregistry, fpjson;
  17. type
  18. { TTestJSONString }
  19. TTestJSONString = Class(TTestCase)
  20. Private
  21. Procedure TestTo(Const Src,Dest : String);
  22. Procedure TestFrom(Const Src,Dest : String);
  23. Published
  24. Procedure TestJSONStringToString;
  25. Procedure TestStringToJSONString;
  26. end;
  27. { TTestJSON }
  28. TTestJSON = Class(TTestCase)
  29. Protected
  30. Procedure TestItemCount(J : TJSONData;Expected : Integer);
  31. Procedure TestJSONType(J : TJSONData;Expected : TJSONType);
  32. Procedure TestJSON(J : TJSONData;Expected : String);
  33. Procedure TestIsNull(J : TJSONData;Expected : Boolean);
  34. Procedure TestAsBoolean(J : TJSONData;Expected : Boolean; ExpectError : boolean = False);
  35. Procedure TestAsInteger(J : TJSONData; Expected : Integer; ExpectError : boolean = False);
  36. Procedure TestAsInt64(J : TJSONData; Expected : Int64; ExpectError : boolean = False);
  37. Procedure TestAsString(J : TJSONData; Expected : String; ExpectError : boolean = False);
  38. Procedure TestAsFloat(J : TJSONData; Expected : TJSONFloat; ExpectError : boolean = False);
  39. end;
  40. { TTestNull }
  41. TTestNull = class(TTestJSON)
  42. published
  43. procedure TestNull;
  44. Procedure TestClone;
  45. end;
  46. { TTestBoolean }
  47. TTestBoolean = class(TTestJSON)
  48. published
  49. procedure TestTrue;
  50. procedure TestFalse;
  51. Procedure TestClone;
  52. end;
  53. { TTestInteger }
  54. TTestInteger = class(TTestJSON)
  55. Private
  56. Procedure DoTest(I : Integer);
  57. published
  58. procedure TestPositive;
  59. procedure TestNegative;
  60. procedure TestZero;
  61. Procedure TestClone;
  62. end;
  63. { TTestInt64 }
  64. TTestInt64 = class(TTestJSON)
  65. Private
  66. Procedure DoTest(I : Int64);
  67. published
  68. procedure TestPositive;
  69. procedure TestNegative;
  70. procedure TestZero;
  71. Procedure TestClone;
  72. end;
  73. { TTestFloat }
  74. TTestFloat = class(TTestJSON)
  75. Private
  76. Procedure DoTest(F : TJSONFloat);
  77. published
  78. procedure TestPositive;
  79. procedure TestNegative;
  80. procedure TestZero;
  81. Procedure TestClone;
  82. end;
  83. { TTestString }
  84. TTestString = class(TTestJSON)
  85. private
  86. procedure DoTestFloat(F: TJSOnFloat; S: String; OK: Boolean);
  87. published
  88. procedure TestString;
  89. procedure TestInteger;
  90. procedure TestNegativeInteger;
  91. procedure TestFloat;
  92. procedure TestNegativeFloat;
  93. Procedure TestBooleanTrue;
  94. Procedure TestBooleanFalse;
  95. Procedure TestClone;
  96. end;
  97. { TTestArray }
  98. TTestArray = class(TTestJSON)
  99. private
  100. procedure TestAddBoolean(B : Boolean);
  101. published
  102. Procedure TestCreate;
  103. Procedure TestCreateString;
  104. Procedure TestCreatePchar;
  105. procedure TestCreateStrings;
  106. procedure TestCreateInteger;
  107. procedure TestCreateInt64;
  108. procedure TestCreateFloat;
  109. procedure TestCreateBoolean;
  110. procedure TestCreateObject;
  111. procedure TestCreateJSONString;
  112. procedure TestCreateJSONObject;
  113. procedure TestCreateNilPointer;
  114. procedure TestCreatePointer;
  115. procedure TestAddInteger;
  116. procedure TestAddInt64;
  117. procedure TestAddFloat;
  118. procedure TestAddBooleanTrue;
  119. procedure TestAddBooleanFalse;
  120. procedure TestAddString;
  121. procedure TestAddNull;
  122. procedure TestAddObject;
  123. procedure TestAddArray;
  124. procedure TestDelete;
  125. procedure TestRemove;
  126. Procedure TestClone;
  127. end;
  128. { TTestObject }
  129. TTestObject = class(TTestJSON)
  130. private
  131. procedure TestAddBoolean(B : Boolean);
  132. published
  133. Procedure TestCreate;
  134. Procedure TestCreateString;
  135. Procedure TestCreatePchar;
  136. procedure TestCreateStrings;
  137. procedure TestCreateInteger;
  138. procedure TestCreateInt64;
  139. procedure TestCreateFloat;
  140. procedure TestCreateBoolean;
  141. procedure TestCreateObject;
  142. procedure TestCreateJSONString;
  143. procedure TestCreateJSONObject;
  144. procedure TestCreateNilPointer;
  145. procedure TestCreatePointer;
  146. procedure TestAddInteger;
  147. procedure TestAddInt64;
  148. procedure TestAddFloat;
  149. procedure TestAddBooleanTrue;
  150. procedure TestAddBooleanFalse;
  151. procedure TestAddString;
  152. procedure TestAddNull;
  153. procedure TestAddObject;
  154. procedure TestAddArray;
  155. procedure TestDelete;
  156. procedure TestRemove;
  157. procedure TestClone;
  158. procedure TestExtract;
  159. end;
  160. implementation
  161. { TTestJSON }
  162. procedure TTestJSON.TestItemCount(J: TJSONData; Expected: Integer);
  163. begin
  164. AssertEquals(J.ClassName+'.ItemCount',Expected,J.Count);
  165. end;
  166. procedure TTestJSON.TestJSONType(J: TJSONData; Expected: TJSONType);
  167. begin
  168. AssertEquals(J.ClassName+'.JSONType',Ord(Expected),Ord(J.JSONType));
  169. end;
  170. procedure TTestJSON.TestJSON(J: TJSONData; Expected: String);
  171. begin
  172. AssertEquals(J.ClassName+'.AsJSON',Expected,J.AsJSON);
  173. end;
  174. procedure TTestJSON.TestIsNull(J: TJSONData; Expected: Boolean);
  175. begin
  176. AssertEquals(J.ClassName+'.IsNull',Expected,J.IsNull);
  177. end;
  178. procedure TTestJSON.TestAsBoolean(J: TJSONData; Expected: Boolean; ExpectError: boolean = False);
  179. Var
  180. B : Boolean;
  181. AssignOK : Boolean;
  182. Msg : String;
  183. begin
  184. AssignOK:=False;
  185. Try
  186. B:=J.AsBoolean;
  187. AssignOK:=True;
  188. If Not ExpectError then
  189. AssertEquals(J.Classname+'.AsBoolean',Expected,B);
  190. except
  191. On E : Exception do
  192. begin
  193. AssignOK:=False;
  194. Msg:=E.Message;
  195. end;
  196. end;
  197. If ExpectError then
  198. begin
  199. If AssignOK then
  200. Fail(J.ClassName+'.AsBoolean must raise error');
  201. end
  202. else
  203. begin
  204. If not AssignOK then
  205. Fail(J.ClassName+'.AsBoolean raised unexpected exception: '+Msg)
  206. end;
  207. end;
  208. procedure TTestJSON.TestAsInteger(J: TJSONData; Expected: Integer;
  209. ExpectError: boolean);
  210. Var
  211. I : Integer;
  212. AssignOK : Boolean;
  213. Msg : String;
  214. begin
  215. AssignOK:=False;
  216. Try
  217. I:=J.AsInteger;
  218. AssignOK:=True;
  219. If Not ExpectError then
  220. AssertEquals(J.Classname+'.AsInteger',Expected,I);
  221. except
  222. On E : Exception do
  223. begin
  224. AssignOK:=False;
  225. Msg:=E.Message;
  226. end;
  227. end;
  228. If ExpectError then
  229. begin
  230. If AssignOK then
  231. Fail(J.ClassName+'.AsInteger must raise error');
  232. end
  233. else
  234. begin
  235. If not AssignOK then
  236. Fail(J.ClassName+'.AsInteger raised unexpected exception: '+Msg)
  237. end;
  238. end;
  239. procedure TTestJSON.TestAsInt64(J: TJSONData; Expected: Int64;
  240. ExpectError: boolean);
  241. Var
  242. I : Int64;
  243. AssignOK : Boolean;
  244. Msg : String;
  245. begin
  246. AssignOK:=False;
  247. Try
  248. I:=J.AsInt64;
  249. AssignOK:=True;
  250. If Not ExpectError then
  251. AssertEquals(J.Classname+'.AsInt64',Expected,I);
  252. except
  253. On E : Exception do
  254. begin
  255. AssignOK:=False;
  256. Msg:=E.Message;
  257. end;
  258. end;
  259. If ExpectError then
  260. begin
  261. If AssignOK then
  262. Fail(J.ClassName+'.AsInt64 must raise error');
  263. end
  264. else
  265. begin
  266. If not AssignOK then
  267. Fail(J.ClassName+'.AsInt64 raised unexpected exception: '+Msg)
  268. end;
  269. end;
  270. procedure TTestJSON.TestAsString(J: TJSONData; Expected: String;
  271. ExpectError: boolean);
  272. Var
  273. S : String;
  274. AssignOK : Boolean;
  275. Msg : String;
  276. begin
  277. AssignOK:=False;
  278. Try
  279. S:=J.AsString;
  280. AssignOK:=True;
  281. If Not ExpectError then
  282. AssertEquals(J.Classname+'.AsString',Expected,S);
  283. except
  284. On E : Exception do
  285. begin
  286. AssignOK:=False;
  287. Msg:=E.Message;
  288. end;
  289. end;
  290. If ExpectError then
  291. begin
  292. If AssignOK then
  293. Fail(J.ClassName+'.AsString must raise error');
  294. end
  295. else
  296. begin
  297. If not AssignOK then
  298. Fail(J.ClassName+'.AsString raised unexpected exception: '+Msg)
  299. end;
  300. end;
  301. procedure TTestJSON.TestAsFloat(J: TJSONData; Expected: TJSONFloat;
  302. ExpectError: boolean);
  303. Var
  304. F : TJSONFloat;
  305. AssignOK : Boolean;
  306. Msg : String;
  307. begin
  308. AssignOK:=False;
  309. Try
  310. F:=J.AsFloat;
  311. AssignOK:=True;
  312. If Not ExpectError then
  313. AssertEquals(J.Classname+'.AsFloat',Expected,F);
  314. except
  315. On E : Exception do
  316. begin
  317. AssignOK:=False;
  318. Msg:=E.Message;
  319. end;
  320. end;
  321. If ExpectError then
  322. begin
  323. If AssignOK then
  324. Fail(J.ClassName+'.AsFloat must raise error');
  325. end
  326. else
  327. begin
  328. If not AssignOK then
  329. Fail(J.ClassName+'.AsFloat raised unexpected exception: '+Msg)
  330. end;
  331. end;
  332. { TTestBoolean }
  333. procedure TTestBoolean.TestTrue;
  334. Var
  335. J : TJSONBoolean;
  336. begin
  337. J:=TJSONBoolean.Create(True);
  338. try
  339. TestJSONType(J,jtBoolean);
  340. TestItemCount(J,0);
  341. TestJSON(J,'true');
  342. TestIsNull(J,False);
  343. TestAsBoolean(J,True);
  344. TestAsInteger(J,1);
  345. TestAsInt64(J,1);
  346. TestAsString(J,BoolToStr(True));
  347. TestAsFloat(J,1.0);
  348. finally
  349. FreeAndNil(J);
  350. end;
  351. end;
  352. procedure TTestBoolean.TestFalse;
  353. Var
  354. J : TJSONBoolean;
  355. begin
  356. J:=TJSONBoolean.Create(False);
  357. try
  358. TestJSONType(J,jtBoolean);
  359. TestItemCount(J,0);
  360. TestJSON(J,'false');
  361. TestIsNull(J,False);
  362. TestAsBoolean(J,False);
  363. TestAsInteger(J,0);
  364. TestAsInt64(J,0);
  365. TestAsString(J,BoolToStr(False));
  366. TestAsFloat(J,0.0);
  367. finally
  368. FreeAndNil(J);
  369. end;
  370. end;
  371. procedure TTestBoolean.TestClone;
  372. Var
  373. B : TJSONBoolean;
  374. D : TJSONData;
  375. begin
  376. B:=TJSONBoolean.Create(true);
  377. try
  378. D:=B.Clone;
  379. try
  380. TestJSONType(D,jtBoolean);
  381. TestAsBoolean(D,true);
  382. finally
  383. D.Free;
  384. end;
  385. finally
  386. FreeAndNil(B);
  387. end;
  388. end;
  389. { TTestNull }
  390. procedure TTestNull.TestNull;
  391. Var
  392. J : TJSONNull;
  393. begin
  394. J:=TJSONNull.Create;
  395. try
  396. TestJSONType(J,jtNull);
  397. TestItemCount(J,0);
  398. TestJSON(J,'null');
  399. TestIsNull(J,True);
  400. TestAsBoolean(J,False,True);
  401. TestAsInteger(J,0,true);
  402. TestAsInt64(J,0,true);
  403. TestAsString(J,BoolToStr(False),true);
  404. TestAsFloat(J,0.0,true);
  405. finally
  406. FreeAndNil(J);
  407. end;
  408. end;
  409. procedure TTestNull.TestClone;
  410. Var
  411. J : TJSONNull;
  412. D : TJSONData;
  413. begin
  414. J:=TJSONNull.Create;
  415. try
  416. D:=J.Clone;
  417. try
  418. TestIsNull(D,True);
  419. finally
  420. D.Free;
  421. end;
  422. finally
  423. FreeAndNil(J);
  424. end;
  425. end;
  426. { TTestString }
  427. procedure TTestString.TestString;
  428. Const
  429. S = 'A string';
  430. Var
  431. J : TJSONString;
  432. begin
  433. J:=TJSONString.Create(S);
  434. try
  435. TestJSONType(J,jtString);
  436. TestItemCount(J,0);
  437. TestJSON(J,'"'+S+'"');
  438. TestIsNull(J,False);
  439. TestAsBoolean(J,False,True);
  440. TestAsInteger(J,0,true);
  441. TestAsInt64(J,0,true);
  442. TestAsString(J,S);
  443. TestAsFloat(J,0.0,true);
  444. finally
  445. FreeAndNil(J);
  446. end;
  447. end;
  448. procedure TTestString.TestInteger;
  449. Const
  450. S = '1';
  451. Var
  452. J : TJSONString;
  453. begin
  454. J:=TJSONString.Create(S);
  455. try
  456. TestJSONType(J,jtString);
  457. TestItemCount(J,0);
  458. TestJSON(J,'"'+S+'"');
  459. TestIsNull(J,False);
  460. TestAsBoolean(J,True,False);
  461. TestAsInteger(J,1,False);
  462. TestAsInt64(J,1,False);
  463. TestAsString(J,S);
  464. TestAsFloat(J,1.0,False);
  465. finally
  466. FreeAndNil(J);
  467. end;
  468. end;
  469. procedure TTestString.TestNegativeInteger;
  470. Const
  471. S = '-1';
  472. Var
  473. J : TJSONString;
  474. begin
  475. J:=TJSONString.Create(S);
  476. try
  477. TestJSONType(J,jtString);
  478. TestItemCount(J,0);
  479. TestJSON(J,'"'+S+'"');
  480. TestIsNull(J,False);
  481. TestAsBoolean(J,True,False);
  482. TestAsInteger(J,-1,False);
  483. TestAsInt64(J,-1,False);
  484. TestAsString(J,S);
  485. TestAsFloat(J,-1.0,False);
  486. finally
  487. FreeAndNil(J);
  488. end;
  489. end;
  490. procedure TTestString.TestFloat;
  491. begin
  492. DoTestFloat(1.0,'1.0',True);
  493. DoTestFloat(1.0,'1',True);
  494. DoTestFloat(1.0,'1e0',True);
  495. DoTestFloat(1.2,'1.2',True);
  496. DoTestFloat(12.0,'1.2e1',True);
  497. end;
  498. procedure TTestString.TestNegativeFloat;
  499. begin
  500. DoTestFloat(-1.0,'-1.0',True);
  501. DoTestFloat(-1.0,'-1',True);
  502. DoTestFloat(-1.0,'-1e0',True);
  503. DoTestFloat(-1.2,'-1.2',True);
  504. DoTestFloat(-12.0,'-1.2e1',True);
  505. end;
  506. procedure TTestString.TestBooleanTrue;
  507. Const
  508. S = 'true';
  509. Var
  510. J : TJSONString;
  511. begin
  512. J:=TJSONString.Create(S);
  513. try
  514. TestJSONType(J,jtString);
  515. TestItemCount(J,0);
  516. TestJSON(J,'"'+S+'"');
  517. TestIsNull(J,False);
  518. TestAsBoolean(J,True,False);
  519. TestAsInteger(J,-1,True);
  520. TestAsInt64(J,-1,True);
  521. TestAsString(J,S);
  522. TestAsFloat(J,-1.0,True);
  523. finally
  524. FreeAndNil(J);
  525. end;
  526. end;
  527. procedure TTestString.TestBooleanFalse;
  528. Const
  529. S = 'false';
  530. Var
  531. J : TJSONString;
  532. begin
  533. J:=TJSONString.Create(S);
  534. try
  535. TestJSONType(J,jtString);
  536. TestItemCount(J,0);
  537. TestJSON(J,'"'+S+'"');
  538. TestIsNull(J,False);
  539. TestAsBoolean(J,False,False);
  540. TestAsInteger(J,0,True);
  541. TestAsInt64(J,0,True);
  542. TestAsString(J,S);
  543. TestAsFloat(J,0,True);
  544. finally
  545. FreeAndNil(J);
  546. end;
  547. end;
  548. procedure TTestString.TestClone;
  549. Var
  550. S : TJSONString;
  551. D : TJSONData;
  552. begin
  553. S:=TJSONString.Create('aloha');
  554. try
  555. D:=S.Clone;
  556. try
  557. TestJSONType(D,jtString);
  558. TestAsString(D,'aloha');
  559. finally
  560. D.Free;
  561. end;
  562. finally
  563. FreeAndNil(S);
  564. end;
  565. end;
  566. procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean);
  567. Var
  568. J : TJSONString;
  569. begin
  570. J:=TJSONString.Create(S);
  571. try
  572. TestJSONType(J,jtString);
  573. TestItemCount(J,0);
  574. TestJSON(J,'"'+S+'"');
  575. TestIsNull(J,False);
  576. TestAsBoolean(J,(F<>0),Not OK);
  577. TestAsInteger(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
  578. TestAsInt64(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
  579. TestAsString(J,S);
  580. TestAsFloat(J,F,Not OK);
  581. finally
  582. FreeAndNil(J);
  583. end;
  584. end;
  585. { TTestInteger }
  586. procedure TTestInteger.DoTest(I: Integer);
  587. Var
  588. J : TJSONIntegerNumber;
  589. begin
  590. J:=TJSONIntegerNumber.Create(I);
  591. try
  592. TestJSONType(J,jtNumber);
  593. TestItemCount(J,0);
  594. AssertEquals('Numbertype is ntInteger',ord(ntInteger),Ord(J.NumberType));
  595. TestJSON(J,IntToStr(i));
  596. TestIsNull(J,False);
  597. TestAsBoolean(J,(I<>0));
  598. TestAsInteger(J,I);
  599. TestAsInt64(J,I);
  600. TestAsString(J,IntToStr(I));
  601. TestAsFloat(J,I);
  602. finally
  603. FreeAndNil(J);
  604. end;
  605. end;
  606. procedure TTestInteger.TestPositive;
  607. begin
  608. DoTest(1);
  609. end;
  610. procedure TTestInteger.TestNegative;
  611. begin
  612. DoTest(-1);
  613. end;
  614. procedure TTestInteger.TestZero;
  615. begin
  616. DoTest(0);
  617. end;
  618. procedure TTestInteger.TestClone;
  619. Var
  620. I : TJSONIntegerNumber;
  621. D : TJSONData;
  622. begin
  623. I:=TJSONIntegerNumber.Create(99);
  624. try
  625. D:=I.Clone;
  626. try
  627. TestJSONType(D,jtNumber);
  628. TestAsInteger(D,99);
  629. finally
  630. D.Free;
  631. end;
  632. finally
  633. FreeAndNil(I);
  634. end;
  635. end;
  636. { TTestInt64 }
  637. procedure TTestInt64.DoTest(I: Int64);
  638. Var
  639. J : TJSONInt64Number;
  640. begin
  641. J:=TJSONInt64Number.Create(I);
  642. try
  643. TestJSONType(J,jtNumber);
  644. TestItemCount(J,0);
  645. AssertEquals('Numbertype is ntInt64',ord(ntInt64),Ord(J.NumberType));
  646. TestJSON(J,IntToStr(i));
  647. TestIsNull(J,False);
  648. TestAsBoolean(J,(I<>0));
  649. TestAsInteger(J,I);
  650. TestAsInt64(J,I);
  651. TestAsString(J,IntToStr(I));
  652. TestAsFloat(J,I);
  653. finally
  654. FreeAndNil(J);
  655. end;
  656. end;
  657. procedure TTestInt64.TestPositive;
  658. begin
  659. DoTest(1);
  660. end;
  661. procedure TTestInt64.TestNegative;
  662. begin
  663. DoTest(-1);
  664. end;
  665. procedure TTestInt64.TestZero;
  666. begin
  667. DoTest(0);
  668. end;
  669. procedure TTestInt64.TestClone;
  670. Var
  671. I : TJSONInt64Number;
  672. D : TJSONData;
  673. begin
  674. I:=TJSONInt64Number.Create(99);
  675. try
  676. D:=I.Clone;
  677. try
  678. TestJSONType(D,jtNumber);
  679. AssertEquals('Numbertype is ntInt64',ord(ntInt64),Ord(TJSONInt64Number(D).NumberType));
  680. TestAsInteger(D,99);
  681. finally
  682. D.Free;
  683. end;
  684. finally
  685. FreeAndNil(I);
  686. end;
  687. end;
  688. { TTestFloat }
  689. procedure TTestFloat.DoTest(F: TJSONFloat);
  690. Var
  691. J : TJSONFloatNumber;
  692. S : String;
  693. begin
  694. Str(F,S);
  695. J:=TJSONFloatNumber.Create(F);
  696. try
  697. TestJSONType(J,jtNumber);
  698. TestItemCount(J,0);
  699. AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(J.NumberType));
  700. TestJSON(J,S);
  701. TestIsNull(J,False);
  702. TestAsBoolean(J,(F<>0));
  703. TestAsInteger(J,Round(F));
  704. TestAsInt64(J,Round(F));
  705. TestAsString(J,S);
  706. TestAsFloat(J,F);
  707. finally
  708. FreeAndNil(J);
  709. end;
  710. end;
  711. procedure TTestFloat.TestPositive;
  712. begin
  713. DoTest(1.0);
  714. DoTest(1.2);
  715. DoTest(1.2e1);
  716. DoTest(1.2e-1);
  717. DoTest(1.2e10);
  718. DoTest(1.2e-10);
  719. end;
  720. procedure TTestFloat.TestNegative;
  721. begin
  722. DoTest(-1.0);
  723. DoTest(-1.2);
  724. DoTest(-1.2e1);
  725. DoTest(-1.2e-1);
  726. DoTest(-1.2e10);
  727. DoTest(-1.2e-10);
  728. end;
  729. procedure TTestFloat.TestZero;
  730. begin
  731. DoTest(0.0);
  732. end;
  733. procedure TTestFloat.TestClone;
  734. Var
  735. F : TJSONFloatNumber;
  736. D : TJSONData;
  737. begin
  738. F:=TJSONFloatNumber.Create(1.23);
  739. try
  740. D:=F.Clone;
  741. try
  742. TestJSONType(D,jtNumber);
  743. AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(TJSONFloatNumber(D).NumberType));
  744. TestAsFloat(D,1.23);
  745. finally
  746. D.Free;
  747. end;
  748. finally
  749. FreeAndNil(F);
  750. end;
  751. end;
  752. { TTestArray }
  753. procedure TTestArray.TestCreate;
  754. Var
  755. J : TJSONArray;
  756. begin
  757. J:=TJSonArray.Create;
  758. try
  759. TestJSONType(J,jtArray);
  760. TestItemCount(J,0);
  761. TestJSON(J,'[]');
  762. TestIsNull(J,False);
  763. TestAsBoolean(J,False,True);
  764. TestAsInteger(J,1,True);
  765. TestAsInt64(J,1,True);
  766. TestAsString(J,'',True);
  767. TestAsFloat(J,0.0,True);
  768. finally
  769. FreeAndNil(J);
  770. end;
  771. end;
  772. procedure TTestArray.TestCreateString;
  773. Const
  774. S = 'A string';
  775. Var
  776. J : TJSONArray;
  777. begin
  778. J:=TJSonArray.Create([S]);
  779. try
  780. TestJSONType(J,jtArray);
  781. TestItemCount(J,1);
  782. TestJSONType(J[0],jtString);
  783. TestJSON(J,'["'+S+'"]');
  784. TestIsNull(J,False);
  785. finally
  786. FreeAndNil(J);
  787. end;
  788. end;
  789. procedure TTestArray.TestCreatePChar;
  790. Const
  791. S = 'A string';
  792. Var
  793. J : TJSONArray;
  794. begin
  795. J:=TJSonArray.Create([Pchar(S)]);
  796. try
  797. TestJSONType(J,jtArray);
  798. TestItemCount(J,1);
  799. TestJSONType(J[0],jtString);
  800. TestJSON(J,'["'+S+'"]');
  801. TestIsNull(J,False);
  802. finally
  803. FreeAndNil(J);
  804. end;
  805. end;
  806. procedure TTestArray.TestCreateStrings;
  807. Const
  808. S = 'A string';
  809. T = 'B string';
  810. Var
  811. J : TJSONArray;
  812. begin
  813. J:=TJSONArray.Create([S,T]);
  814. try
  815. TestJSONType(J,jtArray);
  816. TestItemCount(J,2);
  817. TestJSONType(J[0],jtString);
  818. TestJSONType(J[1],jtString);
  819. TestJSON(J,'["'+S+'", "'+T+'"]');
  820. TestIsNull(J,False);
  821. finally
  822. FreeAndNil(J);
  823. end;
  824. end;
  825. procedure TTestArray.TestCreateInteger;
  826. Const
  827. S = 3;
  828. Var
  829. J : TJSONArray;
  830. begin
  831. J:=TJSonArray.Create([S]);
  832. try
  833. TestJSONType(J,jtArray);
  834. TestItemCount(J,1);
  835. TestJSONType(J[0],jtNumber);
  836. TestJSON(J,'[3]');
  837. finally
  838. FreeAndNil(J);
  839. end;
  840. end;
  841. procedure TTestArray.TestCreateFloat;
  842. Const
  843. S = 1.2;
  844. Var
  845. J : TJSONArray;
  846. r : String;
  847. begin
  848. J:=TJSonArray.Create([S]);
  849. try
  850. TestJSONType(J,jtArray);
  851. TestItemCount(J,1);
  852. TestJSONType(J[0],jtNumber);
  853. Str(S,R);
  854. TestJSON(J,'['+R+']');
  855. finally
  856. FreeAndNil(J);
  857. end;
  858. end;
  859. procedure TTestArray.TestCreateInt64;
  860. Const
  861. S : Int64 = $FFFFFFFFFFFFF;
  862. Var
  863. J : TJSONArray;
  864. begin
  865. J:=TJSonArray.Create([S]);
  866. try
  867. TestJSONType(J,jtArray);
  868. TestItemCount(J,1);
  869. TestJSONType(J[0],jtNumber);
  870. TestJSON(J,'['+IntToStr(S)+']');
  871. finally
  872. FreeAndNil(J);
  873. end;
  874. end;
  875. procedure TTestArray.TestCreateBoolean;
  876. Const
  877. S = True;
  878. Var
  879. J : TJSONArray;
  880. begin
  881. J:=TJSonArray.Create([S]);
  882. try
  883. TestJSONType(J,jtArray);
  884. TestItemCount(J,1);
  885. TestJSONType(J[0],jtBoolean);
  886. TestJSON(J,'[true]');
  887. finally
  888. FreeAndNil(J);
  889. end;
  890. end;
  891. procedure TTestArray.TestCreateJSONObject;
  892. Var
  893. J : TJSONArray;
  894. begin
  895. J:=TJSonArray.Create([TJSONObject.Create]);
  896. try
  897. TestItemCount(J,1);
  898. TestJSONType(J[0],jtObject);
  899. TestJSON(J,'[{}]');
  900. finally
  901. FreeAndNil(J);
  902. end;
  903. end;
  904. procedure TTestArray.TestCreateJSONString;
  905. Const
  906. S = 'A string';
  907. Var
  908. J : TJSONArray;
  909. begin
  910. J:=TJSonArray.Create([TJSONString.Create(S)]);
  911. try
  912. TestItemCount(J,1);
  913. TestJSONType(J[0],jtString);
  914. TestJSON(J,'["'+S+'"]');
  915. finally
  916. FreeAndNil(J);
  917. end;
  918. end;
  919. procedure TTestArray.TestCreateObject;
  920. Var
  921. J : TJSONArray;
  922. begin
  923. J:=Nil;
  924. try
  925. Try
  926. J:=TJSONArray.Create([TObject.Create]);
  927. Fail('Array constructor accepts only TJSONData');
  928. finally
  929. FreeAndNil(J);
  930. end;
  931. except
  932. // Should be OK.
  933. end;
  934. end;
  935. procedure TTestArray.TestCreateNilPointer;
  936. Var
  937. J : TJSONArray;
  938. P : Pointer;
  939. begin
  940. J:=Nil;
  941. P:=Nil;
  942. Try
  943. J:=TJSONArray.Create([P]);
  944. TestJSONType(J[0],jtNull);
  945. finally
  946. FreeAndNil(J);
  947. end;
  948. end;
  949. procedure TTestArray.TestCreatePointer;
  950. Var
  951. J : TJSONArray;
  952. P : Pointer;
  953. begin
  954. J:=Nil;
  955. P:=@Self;
  956. try
  957. Try
  958. J:=TJSONArray.Create([P]);
  959. Fail('Array constructor accepts only NIL pointers');
  960. finally
  961. FreeAndNil(J);
  962. end;
  963. except
  964. // Should be OK.
  965. end;
  966. end;
  967. procedure TTestArray.TestAddInteger;
  968. Var
  969. J : TJSONArray;
  970. begin
  971. J:=TJSonArray.Create;
  972. try
  973. J.Add(Integer(0));
  974. TestItemCount(J,1);
  975. TestJSONType(J[0],jtNumber);
  976. AssertEquals('J[0] is TJSONIntegerNumber',J[0].ClassType,TJSONIntegerNumber);
  977. AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
  978. AssertEquals('J.Integers[0]=0',0,J.integers[0]);
  979. TestAsInteger(J[0],0);
  980. TestAsInt64(J[0],0);
  981. TestJSON(J,'[0]');
  982. finally
  983. FreeAndNil(J);
  984. end;
  985. end;
  986. procedure TTestArray.TestAddInt64;
  987. Var
  988. J : TJSONArray;
  989. begin
  990. J:=TJSonArray.Create;
  991. try
  992. J.Add(Int64(0));
  993. TestItemCount(J,1);
  994. TestJSONType(J[0],jtNumber);
  995. AssertEquals('J[0] is TJSONInt64Number',J[0].ClassType,TJSONInt64Number);
  996. AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
  997. AssertEquals('J.Int64s[0]=0',0,J.Int64s[0]);
  998. TestAsInteger(J[0],0);
  999. TestAsInt64(J[0],0);
  1000. TestJSON(J,'[0]');
  1001. finally
  1002. FreeAndNil(J);
  1003. end;
  1004. end;
  1005. procedure TTestArray.TestAddFloat;
  1006. Var
  1007. J : TJSONArray;
  1008. S : String;
  1009. F : TJSONFloat;
  1010. begin
  1011. F:=1.2;
  1012. J:=TJSonArray.Create;
  1013. try
  1014. J.Add(F);
  1015. TestItemCount(J,1);
  1016. TestJSONType(J[0],jtNumber);
  1017. AssertEquals('J[0] is TJSONFloatNumber',TJSONfloatNumber,J[0].ClassType);
  1018. AssertEquals('j.Types[0]=jtNumber',Ord(jtNumber),ord(J.Types[0]));
  1019. AssertEquals('J.Floats[0]='+FloatToStr(F),F,J.Floats[0]);
  1020. TestAsFloat(J[0],F);
  1021. Str(F,S);
  1022. TestJSON(J,'['+S+']');
  1023. finally
  1024. FreeAndNil(J);
  1025. end;
  1026. end;
  1027. procedure TTestArray.TestAddBoolean(B : Boolean);
  1028. Var
  1029. J : TJSONArray;
  1030. begin
  1031. B:=True;
  1032. J:=TJSonArray.Create;
  1033. try
  1034. J.Add(B);
  1035. TestItemCount(J,1);
  1036. TestJSONType(J[0],jtBoolean);
  1037. AssertEquals('J[0] is TJSONBoolean',TJSONBoolean,J[0].ClassType);
  1038. TestAsBoolean(J[0],B);
  1039. AssertEquals('J.Booleans[0]='+BoolToStr(B)+'"',B,J.Booleans[0]);
  1040. If B then
  1041. TestJSON(J,'[true]')
  1042. else
  1043. TestJSON(J,'[false]');
  1044. finally
  1045. FreeAndNil(J);
  1046. end;
  1047. end;
  1048. procedure TTestArray.TestAddBooleanTrue;
  1049. begin
  1050. TestAddBoolean(True);
  1051. end;
  1052. procedure TTestArray.TestAddBooleanFalse;
  1053. begin
  1054. TestAddBoolean(False);
  1055. end;
  1056. procedure TTestArray.TestAddString;
  1057. Var
  1058. J : TJSONArray;
  1059. S : String;
  1060. F : TJSONFloat;
  1061. begin
  1062. S:='A string';
  1063. J:=TJSonArray.Create;
  1064. try
  1065. J.Add(S);
  1066. TestItemCount(J,1);
  1067. TestJSONType(J[0],jtString);
  1068. AssertEquals('J[0] is TJSONString',TJSONString,J[0].ClassType);
  1069. TestAsString(J[0],S);
  1070. AssertEquals('J.Strings[0]="'+S+'"',S,J.Strings[0]);
  1071. TestJSON(J,'["'+StringToJSONString(S)+'"]');
  1072. finally
  1073. FreeAndNil(J);
  1074. end;
  1075. end;
  1076. procedure TTestArray.TestAddNull;
  1077. Var
  1078. J : TJSONArray;
  1079. S : String;
  1080. F : TJSONFloat;
  1081. begin
  1082. J:=TJSonArray.Create;
  1083. try
  1084. J.Add;
  1085. TestItemCount(J,1);
  1086. TestJSONType(J[0],jtNull);
  1087. AssertEquals('J[0] is TJSONNull',TJSONNull,J[0].ClassType);
  1088. AssertEquals('J.Nulls[0]=True',True,J.Nulls[0]);
  1089. TestIsNull(J[0],true);
  1090. TestJSON(J,'[null]');
  1091. finally
  1092. FreeAndNil(J);
  1093. end;
  1094. end;
  1095. procedure TTestArray.TestAddArray;
  1096. Var
  1097. J,J2 : TJSONArray;
  1098. begin
  1099. J:=TJSonArray.Create;
  1100. try
  1101. J2:=TJSonArray.Create;
  1102. J2.Add(0);
  1103. J2.Add(1);
  1104. J.Add(J2);
  1105. TestItemCount(J,1);
  1106. TestJSONType(J[0],jtArray);
  1107. AssertEquals('J[0] is TJSONArray',TJSONArray,J[0].ClassType);
  1108. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[0].ClassType);
  1109. TestAsInteger(J.Arrays[0][0],0);
  1110. TestAsInteger(J.Arrays[0][1],1);
  1111. TestAsInt64(J.Arrays[0][0],0);
  1112. TestAsInt64(J.Arrays[0][1],1);
  1113. TestJSON(J,'[[0, 1]]');
  1114. finally
  1115. FreeAndNil(J);
  1116. end;
  1117. end;
  1118. procedure TTestArray.TestAddObject;
  1119. Const
  1120. A = 'a';
  1121. B = 'b';
  1122. Var
  1123. J : TJSONArray;
  1124. J2 : TJSONObject;
  1125. begin
  1126. J:=TJSonArray.Create;
  1127. try
  1128. J2:=TJSonObject.Create;
  1129. J2.Add(A,0);
  1130. J2.Add(B,1);
  1131. J.Add(J2);
  1132. TestItemCount(J,1);
  1133. TestJSONType(J[0],jtObject);
  1134. AssertEquals('J[0] is TJSONObject',TJSONObject,J[0].ClassType);
  1135. AssertEquals('J.Objects[0] is TJSONObject',TJSONObject,J.Objects[0].ClassType);
  1136. TestAsInteger(J.Objects[0][A],0);
  1137. TestAsInteger(J.Objects[0][B],1);
  1138. TestAsInt64(J.Objects[0][A],0);
  1139. TestAsInt64(J.Objects[0][B],1);
  1140. TestJSON(J,'[{ "a" : 0, "b" : 1 }]');
  1141. finally
  1142. FreeAndNil(J);
  1143. end;
  1144. end;
  1145. procedure TTestArray.TestDelete;
  1146. Var
  1147. J : TJSONArray;
  1148. begin
  1149. J:=TJSonArray.Create;
  1150. try
  1151. J.Add(0);
  1152. J.Add(1);
  1153. TestItemCount(J,2);
  1154. TestJSONType(J[0],jtNumber);
  1155. TestJSONType(J[1],jtNumber);
  1156. TestJSON(J,'[0, 1]');
  1157. J.Delete(1);
  1158. TestItemCount(J,1);
  1159. J.Delete(0);
  1160. TestItemCount(J,0);
  1161. finally
  1162. FreeAndNil(J);
  1163. end;
  1164. end;
  1165. procedure TTestArray.TestRemove;
  1166. Var
  1167. J : TJSONArray;
  1168. I : TJSONData;
  1169. begin
  1170. J:=TJSonArray.Create;
  1171. try
  1172. J.Add(0);
  1173. J.Add(1);
  1174. J.Add(2);
  1175. TestItemCount(J,3);
  1176. TestJSONType(J[0],jtNumber);
  1177. TestJSONType(J[1],jtNumber);
  1178. TestJSONType(J[2],jtNumber);
  1179. TestJSON(J,'[0, 1, 2]');
  1180. I:=J[1];
  1181. J.Remove(I);
  1182. TestItemCount(J,2);
  1183. TestAsInteger(J[0],0);
  1184. TestAsInteger(J[1],2);
  1185. TestAsInt64(J[0],0);
  1186. TestAsInt64(J[1],2);
  1187. finally
  1188. FreeAndNil(J);
  1189. end;
  1190. end;
  1191. procedure TTestArray.TestClone;
  1192. Var
  1193. J,J2 : TJSONArray;
  1194. D : TJSONData;
  1195. begin
  1196. J:=TJSonArray.Create;
  1197. try
  1198. J.Add(1);
  1199. J.Add('aloha');
  1200. D:=J.Clone;
  1201. try
  1202. TestJSONType(D,jtArray);
  1203. J2:=TJSonArray(D);
  1204. TestItemCount(J2,2);
  1205. TestJSONType(J2[0],jtNumber);
  1206. TestJSONType(J2[1],jtString);
  1207. TestAsInteger(J2[0],1);
  1208. TestAsString(J2[1],'aloha');
  1209. finally
  1210. D.Free;
  1211. end;
  1212. finally
  1213. FreeAndNil(J);
  1214. end;
  1215. end;
  1216. { TTestObject }
  1217. procedure TTestObject.TestCreate;
  1218. Var
  1219. J : TJSONObject;
  1220. begin
  1221. J:=TJSONObject.Create;
  1222. try
  1223. TestJSONType(J,jtObject);
  1224. TestItemCount(J,0);
  1225. TestJSON(J,'{}');
  1226. TestIsNull(J,False);
  1227. TestAsBoolean(J,False,True);
  1228. TestAsInteger(J,1,True);
  1229. TestAsInt64(J,1,True);
  1230. TestAsString(J,'',True);
  1231. TestAsFloat(J,0.0,True);
  1232. finally
  1233. FreeAndNil(J);
  1234. end;
  1235. end;
  1236. procedure TTestObject.TestAddInteger;
  1237. Const
  1238. A = 'a';
  1239. Var
  1240. J : TJSONObject;
  1241. begin
  1242. J:=TJSonObject.Create;
  1243. try
  1244. J.Add(A,Integer(0));
  1245. TestItemCount(J,1);
  1246. TestJSONType(J[A],jtNumber);
  1247. AssertEquals('J[''a''] is TJSONIntegerNumber',J[A].ClassType,TJSONIntegerNumber);
  1248. AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
  1249. AssertEquals('J.Integers[''a'']=0',0,J.integers[A]);
  1250. TestAsInteger(J[A],0);
  1251. TestAsInt64(J[A],0);
  1252. TestJSON(J,'{ "'+A+'" : 0 }');
  1253. finally
  1254. FreeAndNil(J);
  1255. end;
  1256. end;
  1257. procedure TTestObject.TestAddInt64;
  1258. Const
  1259. A = 'a';
  1260. Var
  1261. J : TJSONObject;
  1262. begin
  1263. J:=TJSonObject.Create;
  1264. try
  1265. J.Add(A,Int64(0));
  1266. TestItemCount(J,1);
  1267. TestJSONType(J[A],jtNumber);
  1268. AssertEquals('J[''a''] is TJSONInt64Number',J[A].ClassType,TJSONInt64Number);
  1269. AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
  1270. AssertEquals('J.Int64s[''a'']=0',0,J.Int64s[A]);
  1271. TestAsInteger(J[A],0);
  1272. TestAsInt64(J[A],0);
  1273. TestJSON(J,'{ "'+A+'" : 0 }');
  1274. finally
  1275. FreeAndNil(J);
  1276. end;
  1277. end;
  1278. procedure TTestObject.TestAddFloat;
  1279. Const
  1280. A = 'a';
  1281. Var
  1282. J : TJSONObject;
  1283. S : String;
  1284. F : TJSONFloat;
  1285. begin
  1286. F:=1.2;
  1287. J:=TJSonObject.Create;
  1288. try
  1289. J.Add(A,F);
  1290. TestItemCount(J,1);
  1291. TestJSONType(J[A],jtNumber);
  1292. AssertEquals('J[''a''] is TJSONFloatNumber',TJSONfloatNumber,J[a].ClassType);
  1293. AssertEquals('j.Types[''a'']=jtNumber',Ord(jtNumber),ord(J.Types[a]));
  1294. AssertEquals('J.Floats[''a'']='+FloatToStr(F),F,J.Floats[a]);
  1295. TestAsFloat(J[A],F);
  1296. Str(F,S);
  1297. TestJSON(J,'{ "'+a+'" : '+S+' }');
  1298. finally
  1299. FreeAndNil(J);
  1300. end;
  1301. end;
  1302. procedure TTestObject.TestAddBoolean(B : Boolean);
  1303. Const
  1304. A = 'a';
  1305. Var
  1306. J : TJSONObject;
  1307. begin
  1308. B:=True;
  1309. J:=TJSonObject.Create;
  1310. try
  1311. J.Add(A,B);
  1312. TestItemCount(J,1);
  1313. TestJSONType(J[A],jtBoolean);
  1314. AssertEquals('J[''a''] is TJSONBoolean',TJSONBoolean,J[a].ClassType);
  1315. TestAsBoolean(J[a],B);
  1316. AssertEquals('J.Booleans[''a'']='+BoolToStr(B)+'"',B,J.Booleans[a]);
  1317. If B then
  1318. TestJSON(J,'{ "'+a+'" : true }')
  1319. else
  1320. TestJSON(J,'{ "'+a+'" : false }');
  1321. finally
  1322. FreeAndNil(J);
  1323. end;
  1324. end;
  1325. procedure TTestObject.TestAddBooleanTrue;
  1326. begin
  1327. TestAddBoolean(True);
  1328. end;
  1329. procedure TTestObject.TestAddBooleanFalse;
  1330. begin
  1331. TestAddBoolean(False);
  1332. end;
  1333. procedure TTestObject.TestAddString;
  1334. Const
  1335. A = 'a';
  1336. Var
  1337. J : TJSONObject;
  1338. S : String;
  1339. F : TJSONFloat;
  1340. begin
  1341. S:='A string';
  1342. J:=TJSonObject.Create;
  1343. try
  1344. J.Add(A,S);
  1345. TestItemCount(J,1);
  1346. TestJSONType(J[a],jtString);
  1347. AssertEquals('J[''a''] is TJSONString',TJSONString,J[A].ClassType);
  1348. TestAsString(J[a],S);
  1349. AssertEquals('J.Strings[''a'']="'+S+'"',S,J.Strings[A]);
  1350. TestJSON(J,'{ "'+a+'" : "'+StringToJSONString(S)+'" }');
  1351. finally
  1352. FreeAndNil(J);
  1353. end;
  1354. end;
  1355. procedure TTestObject.TestAddNull;
  1356. Const
  1357. A = 'a';
  1358. Var
  1359. J : TJSONObject;
  1360. S : String;
  1361. F : TJSONFloat;
  1362. begin
  1363. J:=TJSonObject.Create;
  1364. try
  1365. J.Add(a);
  1366. TestItemCount(J,1);
  1367. TestJSONType(J[a],jtNull);
  1368. AssertEquals('J[''a''] is TJSONNull',TJSONNull,J[A].ClassType);
  1369. AssertEquals('J.Nulls[''a'']=True',True,J.Nulls[A]);
  1370. TestIsNull(J[a],true);
  1371. TestJSON(J,'{ "'+a+'" : null }');
  1372. finally
  1373. FreeAndNil(J);
  1374. end;
  1375. end;
  1376. procedure TTestObject.TestAddObject;
  1377. Const
  1378. A = 'a';
  1379. B = 'b';
  1380. C = 'c';
  1381. Var
  1382. J,J2 : TJSONObject;
  1383. begin
  1384. J:=TJSonObject.Create;
  1385. try
  1386. J2:=TJSonObject.Create;
  1387. J2.Add(B,0);
  1388. J2.Add(C,1);
  1389. J.Add(A,J2);
  1390. TestItemCount(J,1);
  1391. TestJSONType(J[A],jtObject);
  1392. AssertEquals('J[''a''] is TJSONObject',TJSONObject,J[A].ClassType);
  1393. AssertEquals('J.Objects[''a''] is TJSONObject',TJSONObject,J.Objects[A].ClassType);
  1394. TestAsInteger(J.Objects[A][B],0);
  1395. TestAsInteger(J.Objects[A][C],1);
  1396. TestAsInt64(J.Objects[A][B],0);
  1397. TestAsInt64(J.Objects[A][C],1);
  1398. TestJSON(J,'{ "a" : { "b" : 0, "c" : 1 } }');
  1399. finally
  1400. FreeAndNil(J);
  1401. end;
  1402. end;
  1403. procedure TTestObject.TestAddArray;
  1404. Const
  1405. A = 'a';
  1406. Var
  1407. J : TJSONObject;
  1408. J2 : TJSONArray;
  1409. begin
  1410. J:=TJSONObject.Create;
  1411. try
  1412. J2:=TJSonArray.Create;
  1413. J2.Add(0);
  1414. J2.Add(1);
  1415. J.Add(A,J2);
  1416. TestItemCount(J,1);
  1417. TestJSONType(J[A],jtArray);
  1418. AssertEquals('J[''a''] is TJSONArray',TJSONArray,J[A].ClassType);
  1419. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[A].ClassType);
  1420. TestAsInteger(J.Arrays[A][0],0);
  1421. TestAsInteger(J.Arrays[A][1],1);
  1422. TestAsInt64(J.Arrays[A][0],0);
  1423. TestAsInt64(J.Arrays[A][1],1);
  1424. TestJSON(J,'{ "a" : [0, 1] }');
  1425. finally
  1426. FreeAndNil(J);
  1427. end;
  1428. end;
  1429. procedure TTestObject.TestDelete;
  1430. Const
  1431. A = 'a';
  1432. B = 'b';
  1433. Var
  1434. J : TJSONObject;
  1435. begin
  1436. J:=TJSonObject.Create;
  1437. try
  1438. J.Add(A,0);
  1439. J.Add(B,1);
  1440. TestItemCount(J,2);
  1441. TestJSONType(J[A],jtNumber);
  1442. TestJSONType(J[A],jtNumber);
  1443. TestJSON(J,'{ "a" : 0, "b" : 1 }');
  1444. J.Delete(1);
  1445. TestItemCount(J,1);
  1446. J.Delete(0);
  1447. TestItemCount(J,0);
  1448. finally
  1449. FreeAndNil(J);
  1450. end;
  1451. end;
  1452. procedure TTestObject.TestRemove;
  1453. Const
  1454. A = 'a';
  1455. B = 'b';
  1456. C = 'c';
  1457. Var
  1458. J : TJSONObject;
  1459. I : TJSONData;
  1460. begin
  1461. J:=TJSonObject.Create;
  1462. try
  1463. J.Add(A,1);
  1464. J.Add(B,2);
  1465. J.Add(C,3);
  1466. TestItemCount(J,3);
  1467. TestJSONType(J[A],jtNumber);
  1468. TestJSONType(J[B],jtNumber);
  1469. TestJSONType(J[C],jtNumber);
  1470. TestJSON(J,'{ "a" : 1, "b" : 2, "c" : 3 }');
  1471. I:=J[b];
  1472. J.Remove(I);
  1473. TestItemCount(J,2);
  1474. TestAsInteger(J[a],1);
  1475. TestAsInteger(J[c],3);
  1476. TestAsInt64(J[a],1);
  1477. TestAsInt64(J[c],3);
  1478. finally
  1479. FreeAndNil(J);
  1480. end;
  1481. end;
  1482. procedure TTestObject.TestClone;
  1483. Var
  1484. J,J2 : TJSONObject;
  1485. D : TJSONData;
  1486. begin
  1487. J:=TJSonObject.Create;
  1488. try
  1489. J.Add('p1',1);
  1490. J.Add('p2','aloha');
  1491. D:=J.Clone;
  1492. try
  1493. TestJSONType(D,jtObject);
  1494. J2:=TJSonObject(D);
  1495. TestItemCount(J2,2);
  1496. TestJSONType(J2['p1'],jtNumber);
  1497. TestJSONType(J2['p2'],jtString);
  1498. TestAsInteger(J2['p1'],1);
  1499. TestAsString(J2['p2'],'aloha');
  1500. finally
  1501. D.Free;
  1502. end;
  1503. finally
  1504. FreeAndNil(J);
  1505. end;
  1506. end;
  1507. procedure TTestObject.TestExtract;
  1508. Const
  1509. A = 'a';
  1510. B = 'b';
  1511. Var
  1512. J : TJSONObject;
  1513. JA,JB : TJSONData;
  1514. E : TJSONData;
  1515. begin
  1516. J:=TJSonObject.Create;
  1517. try
  1518. J.Add(A,0);
  1519. J.Add(B,1);
  1520. TestItemCount(J,2);
  1521. JA:=J[A];
  1522. JB:=J[B];
  1523. TestJSONType(JA,jtNumber);
  1524. TestJSONType(JB,jtNumber);
  1525. TestJSON(J,'{ "a" : 0, "b" : 1 }');
  1526. E:=J.Extract(1);
  1527. AssertSame('Extracted JA',JB,E);
  1528. E.Free;
  1529. TestItemCount(J,1);
  1530. E:=J.Extract(0);
  1531. AssertSame('Extracted JB',JA,E);
  1532. E.Free;
  1533. TestItemCount(J,0);
  1534. finally
  1535. FreeAndNil(J);
  1536. end;
  1537. end;
  1538. procedure TTestObject.TestCreateString;
  1539. Const
  1540. A = 'A';
  1541. S = 'A string';
  1542. Var
  1543. J : TJSONObject;
  1544. begin
  1545. J:=TJSONObject.Create([A,S]);
  1546. try
  1547. TestJSONType(J,jtObject);
  1548. TestItemCount(J,1);
  1549. TestJSONType(J[A],jtString);
  1550. TestJSON(J,'{ "A" : "'+S+'" }');
  1551. TestIsNull(J,False);
  1552. finally
  1553. FreeAndNil(J);
  1554. end;
  1555. end;
  1556. procedure TTestObject.TestCreatePChar;
  1557. Const
  1558. A = 'A';
  1559. S = 'A string';
  1560. Var
  1561. J : TJSONObject;
  1562. begin
  1563. J:=TJSONObject.Create([A,Pchar(S)]);
  1564. try
  1565. TestJSONType(J,jtObject);
  1566. TestItemCount(J,1);
  1567. TestJSONType(J[A],jtString);
  1568. TestJSON(J,'{ "A" : "'+S+'" }');
  1569. TestIsNull(J,False);
  1570. finally
  1571. FreeAndNil(J);
  1572. end;
  1573. end;
  1574. procedure TTestObject.TestCreateStrings;
  1575. Const
  1576. A = 'A';
  1577. B = 'B';
  1578. S = 'A string';
  1579. T = 'B string';
  1580. Var
  1581. J : TJSONObject;
  1582. begin
  1583. J:=TJSONObject.Create([A,S,B,T]);
  1584. try
  1585. TestJSONType(J,jtObject);
  1586. TestItemCount(J,2);
  1587. TestJSONType(J[A],jtString);
  1588. TestJSONType(J[B],jtString);
  1589. TestJSON(J,'{ "A" : "'+S+'", "B" : "'+T+'" }');
  1590. TestIsNull(J,False);
  1591. finally
  1592. FreeAndNil(J);
  1593. end;
  1594. end;
  1595. procedure TTestObject.TestCreateInteger;
  1596. Const
  1597. A = 'A';
  1598. S = 3;
  1599. Var
  1600. J : TJSONObject;
  1601. begin
  1602. J:=TJSONObject.Create([A,S]);
  1603. try
  1604. TestJSONType(J,jtObject);
  1605. TestItemCount(J,1);
  1606. TestJSONType(J[A],jtNumber);
  1607. TestJSON(J,'{ "A" : 3 }');
  1608. finally
  1609. FreeAndNil(J);
  1610. end;
  1611. end;
  1612. procedure TTestObject.TestCreateFloat;
  1613. Const
  1614. A = 'A';
  1615. S = 1.2;
  1616. Var
  1617. J : TJSONObject;
  1618. r : String;
  1619. begin
  1620. J:=TJSONObject.Create([A,S]);
  1621. try
  1622. TestJSONType(J,jtObject);
  1623. TestItemCount(J,1);
  1624. TestJSONType(J[A],jtNumber);
  1625. Str(S,R);
  1626. TestJSON(J,'{ "A" : '+R+' }');
  1627. finally
  1628. FreeAndNil(J);
  1629. end;
  1630. end;
  1631. procedure TTestObject.TestCreateInt64;
  1632. Const
  1633. A = 'A';
  1634. S : Int64 = $FFFFFFFFFFFFF;
  1635. Var
  1636. J : TJSONObject;
  1637. begin
  1638. J:=TJSONObject.Create([A,S]);
  1639. try
  1640. TestJSONType(J,jtObject);
  1641. TestItemCount(J,1);
  1642. TestJSONType(J[A],jtNumber);
  1643. TestJSON(J,'{ "A" : '+IntToStr(S)+' }');
  1644. finally
  1645. FreeAndNil(J);
  1646. end;
  1647. end;
  1648. procedure TTestObject.TestCreateBoolean;
  1649. Const
  1650. A = 'A';
  1651. S = True;
  1652. Var
  1653. J : TJSONObject;
  1654. begin
  1655. J:=TJSONObject.Create([A,S]);
  1656. try
  1657. TestJSONType(J,jtObject);
  1658. TestItemCount(J,1);
  1659. TestJSONType(J[A],jtBoolean);
  1660. TestJSON(J,'{ "A" : true }');
  1661. finally
  1662. FreeAndNil(J);
  1663. end;
  1664. end;
  1665. procedure TTestObject.TestCreateJSONObject;
  1666. Const
  1667. A = 'A';
  1668. Var
  1669. J : TJSONObject;
  1670. begin
  1671. J:=TJSONObject.Create([A,TJSONObject.Create]);
  1672. try
  1673. TestItemCount(J,1);
  1674. TestJSONType(J[A],jtObject);
  1675. TestJSON(J,'{ "A" : {} }');
  1676. finally
  1677. FreeAndNil(J);
  1678. end;
  1679. end;
  1680. procedure TTestObject.TestCreateJSONString;
  1681. Const
  1682. A = 'A';
  1683. S = 'A string';
  1684. Var
  1685. J : TJSONObject;
  1686. begin
  1687. J:=TJSONObject.Create([A,TJSONString.Create(S)]);
  1688. try
  1689. TestItemCount(J,1);
  1690. TestJSONType(J[A],jtString);
  1691. TestJSON(J,'{ "A" : "'+S+'" }');
  1692. finally
  1693. FreeAndNil(J);
  1694. end;
  1695. end;
  1696. procedure TTestObject.TestCreateObject;
  1697. Const
  1698. A = 'A';
  1699. Var
  1700. J : TJSONObject;
  1701. begin
  1702. J:=Nil;
  1703. try
  1704. Try
  1705. J:=TJSONObject.Create([A,TObject.Create]);
  1706. Fail('Array constructor accepts only TJSONData');
  1707. finally
  1708. FreeAndNil(J);
  1709. end;
  1710. except
  1711. // Should be OK.
  1712. end;
  1713. end;
  1714. procedure TTestObject.TestCreateNilPointer;
  1715. Const
  1716. A = 'A';
  1717. Var
  1718. J : TJSONObject;
  1719. P : Pointer;
  1720. begin
  1721. J:=Nil;
  1722. P:=Nil;
  1723. Try
  1724. J:=TJSONObject.Create([A,P]);
  1725. TestJSONType(J[A],jtNull);
  1726. finally
  1727. FreeAndNil(J);
  1728. end;
  1729. end;
  1730. procedure TTestObject.TestCreatePointer;
  1731. Const
  1732. A = 'A';
  1733. Var
  1734. J : TJSONObject;
  1735. P : Pointer;
  1736. begin
  1737. J:=Nil;
  1738. P:=@Self;
  1739. try
  1740. Try
  1741. J:=TJSONObject.Create([A,P]);
  1742. Fail('Array constructor accepts only NIL pointers');
  1743. finally
  1744. FreeAndNil(J);
  1745. end;
  1746. except
  1747. // Should be OK.
  1748. end;
  1749. end;
  1750. { TTestJSONString }
  1751. procedure TTestJSONString.TestTo(const Src, Dest: String);
  1752. Var
  1753. S : String;
  1754. begin
  1755. S:='StringToJSONString('''+Src+''')='''+Dest+'''';
  1756. AssertEquals(S,Dest,StringToJSONString(Src));
  1757. end;
  1758. procedure TTestJSONString.TestFrom(const Src, Dest: String);
  1759. Var
  1760. S : String;
  1761. begin
  1762. S:='JSONStringToString('''+Src+''')='''+Dest+'''';
  1763. AssertEquals(S,Dest,JSONStringToString(Src));
  1764. end;
  1765. procedure TTestJSONString.TestJSONStringToString;
  1766. begin
  1767. TestFrom('','');
  1768. TestFrom('A','A');
  1769. TestFrom('AB','AB');
  1770. TestFrom('ABC','ABC');
  1771. TestFrom('\\','\');
  1772. TestFrom('\/','/');
  1773. TestFrom('\"','"');
  1774. TestFrom('\b',#8);
  1775. TestFrom('\t',#9);
  1776. TestFrom('\n',#10);
  1777. TestFrom('\f',#12);
  1778. TestFrom('\r',#13);
  1779. TestFrom('\bBC',#8'BC');
  1780. TestFrom('\tBC',#9'BC');
  1781. TestFrom('\nBC',#10'BC');
  1782. TestFrom('\fBC',#12'BC');
  1783. TestFrom('\rBC',#13'BC');
  1784. TestFrom('A\b','A'#8);
  1785. TestFrom('A\t','A'#9);
  1786. TestFrom('A\n','A'#10);
  1787. TestFrom('A\f','A'#12);
  1788. TestFrom('A\r','A'#13);
  1789. TestFrom('A\bBC','A'#8'BC');
  1790. TestFrom('A\tBC','A'#9'BC');
  1791. TestFrom('A\nBC','A'#10'BC');
  1792. TestFrom('A\fBC','A'#12'BC');
  1793. TestFrom('A\rBC','A'#13'BC');
  1794. TestFrom('\\\\','\\');
  1795. TestFrom('\/\/','//');
  1796. TestFrom('\"\"','""');
  1797. TestFrom('\b\b',#8#8);
  1798. TestFrom('\t\t',#9#9);
  1799. TestFrom('\n\n',#10#10);
  1800. TestFrom('\f\f',#12#12);
  1801. TestFrom('\r\r',#13#13);
  1802. end;
  1803. procedure TTestJSONString.TestStringToJSONString;
  1804. begin
  1805. TestTo('','');
  1806. TestTo('A','A');
  1807. TestTo('AB','AB');
  1808. TestTo('ABC','ABC');
  1809. TestTo('\','\\');
  1810. TestTo('/','\/');
  1811. TestTo('"','\"');
  1812. TestTo(#8,'\b');
  1813. TestTo(#9,'\t');
  1814. TestTo(#10,'\n');
  1815. TestTo(#12,'\f');
  1816. TestTo(#13,'\r');
  1817. TestTo(#8'BC','\bBC');
  1818. TestTo(#9'BC','\tBC');
  1819. TestTo(#10'BC','\nBC');
  1820. TestTo(#12'BC','\fBC');
  1821. TestTo(#13'BC','\rBC');
  1822. TestTo('A'#8,'A\b');
  1823. TestTo('A'#9,'A\t');
  1824. TestTo('A'#10,'A\n');
  1825. TestTo('A'#12,'A\f');
  1826. TestTo('A'#13,'A\r');
  1827. TestTo('A'#8'BC','A\bBC');
  1828. TestTo('A'#9'BC','A\tBC');
  1829. TestTo('A'#10'BC','A\nBC');
  1830. TestTo('A'#12'BC','A\fBC');
  1831. TestTo('A'#13'BC','A\rBC');
  1832. TestTo('\\','\\\\');
  1833. TestTo('//','\/\/');
  1834. TestTo('""','\"\"');
  1835. TestTo(#8#8,'\b\b');
  1836. TestTo(#9#9,'\t\t');
  1837. TestTo(#10#10,'\n\n');
  1838. TestTo(#12#12,'\f\f');
  1839. TestTo(#13#13,'\r\r');
  1840. end;
  1841. initialization
  1842. RegisterTest(TTestJSONString);
  1843. RegisterTest(TTestNull);
  1844. RegisterTest(TTestBoolean);
  1845. RegisterTest(TTestInteger);
  1846. RegisterTest(TTestInt64);
  1847. RegisterTest(TTestFloat);
  1848. RegisterTest(TTestString);
  1849. RegisterTest(TTestArray);
  1850. RegisterTest(TTestObject);
  1851. end.