testjsondata.pp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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 TestAsString(J : TJSONData; Expected : String; ExpectError : boolean = False);
  37. Procedure TestAsFloat(J : TJSONData; Expected : TJSONFloat; ExpectError : boolean = False);
  38. end;
  39. { TTestNull }
  40. TTestNull = class(TTestJSON)
  41. published
  42. procedure TestNull;
  43. end;
  44. { TTestBoolean }
  45. TTestBoolean = class(TTestJSON)
  46. published
  47. procedure TestTrue;
  48. procedure TestFalse;
  49. end;
  50. { TTestInteger }
  51. TTestInteger = class(TTestJSON)
  52. Private
  53. Procedure DoTest(I : Integer);
  54. published
  55. procedure TestPositive;
  56. procedure TestNegative;
  57. procedure TestZero;
  58. end;
  59. { TTestFloat }
  60. TTestFloat = class(TTestJSON)
  61. Private
  62. Procedure DoTest(F : TJSONFloat);
  63. published
  64. procedure TestPositive;
  65. procedure TestNegative;
  66. procedure TestZero;
  67. end;
  68. { TTestString }
  69. TTestString = class(TTestJSON)
  70. private
  71. procedure DoTestFloat(F: TJSOnFloat; S: String; OK: Boolean);
  72. published
  73. procedure TestString;
  74. procedure TestInteger;
  75. procedure TestNegativeInteger;
  76. procedure TestFloat;
  77. procedure TestNegativeFloat;
  78. Procedure TestBooleanTrue;
  79. Procedure TestBooleanFalse;
  80. end;
  81. { TTestArray }
  82. TTestArray = class(TTestJSON)
  83. private
  84. procedure TestAddBoolean(B : Boolean);
  85. published
  86. Procedure TestCreate;
  87. Procedure TestCreateString;
  88. Procedure TestCreatePchar;
  89. procedure TestCreateStrings;
  90. procedure TestCreateInteger;
  91. procedure TestCreateInt64;
  92. procedure TestCreateFloat;
  93. procedure TestCreateBoolean;
  94. procedure TestCreateObject;
  95. procedure TestCreateJSONString;
  96. procedure TestCreateJSONObject;
  97. procedure TestCreateNilPointer;
  98. procedure TestCreatePointer;
  99. procedure TestAddInteger;
  100. procedure TestAddFloat;
  101. procedure TestAddBooleanTrue;
  102. procedure TestAddBooleanFalse;
  103. procedure TestAddString;
  104. procedure TestAddNull;
  105. procedure TestAddObject;
  106. procedure TestAddArray;
  107. procedure TestDelete;
  108. procedure TestRemove;
  109. end;
  110. { TTestObject }
  111. TTestObject = class(TTestJSON)
  112. private
  113. procedure TestAddBoolean(B : Boolean);
  114. published
  115. Procedure TestCreate;
  116. Procedure TestCreateString;
  117. Procedure TestCreatePchar;
  118. procedure TestCreateStrings;
  119. procedure TestCreateInteger;
  120. procedure TestCreateInt64;
  121. procedure TestCreateFloat;
  122. procedure TestCreateBoolean;
  123. procedure TestCreateObject;
  124. procedure TestCreateJSONString;
  125. procedure TestCreateJSONObject;
  126. procedure TestCreateNilPointer;
  127. procedure TestCreatePointer;
  128. procedure TestAddInteger;
  129. procedure TestAddFloat;
  130. procedure TestAddBooleanTrue;
  131. procedure TestAddBooleanFalse;
  132. procedure TestAddString;
  133. procedure TestAddNull;
  134. procedure TestAddObject;
  135. procedure TestAddArray;
  136. procedure TestDelete;
  137. procedure TestRemove;
  138. end;
  139. implementation
  140. { TTestJSON }
  141. procedure TTestJSON.TestItemCount(J: TJSONData; Expected: Integer);
  142. begin
  143. AssertEquals(J.ClassName+'.ItemCount',Expected,J.Count);
  144. end;
  145. procedure TTestJSON.TestJSONType(J: TJSONData; Expected: TJSONType);
  146. begin
  147. AssertEquals(J.ClassName+'.JSONType',Ord(Expected),Ord(J.JSONType));
  148. end;
  149. procedure TTestJSON.TestJSON(J: TJSONData; Expected: String);
  150. begin
  151. AssertEquals(J.ClassName+'.AsJSON',Expected,J.AsJSON);
  152. end;
  153. procedure TTestJSON.TestIsNull(J: TJSONData; Expected: Boolean);
  154. begin
  155. AssertEquals(J.ClassName+'.IsNull',Expected,J.IsNull);
  156. end;
  157. procedure TTestJSON.TestAsBoolean(J: TJSONData; Expected: Boolean; ExpectError: boolean = False);
  158. Var
  159. B : Boolean;
  160. AssignOK : Boolean;
  161. Msg : String;
  162. begin
  163. AssignOK:=False;
  164. Try
  165. B:=J.AsBoolean;
  166. AssignOK:=True;
  167. If Not ExpectError then
  168. AssertEquals(J.Classname+'.AsBoolean',Expected,B);
  169. except
  170. On E : Exception do
  171. begin
  172. AssignOK:=False;
  173. Msg:=E.Message;
  174. end;
  175. end;
  176. If ExpectError then
  177. begin
  178. If AssignOK then
  179. Fail(J.ClassName+'.AsBoolean must raise error');
  180. end
  181. else
  182. begin
  183. If not AssignOK then
  184. Fail(J.ClassName+'.AsBoolean raised unexpected exception: '+Msg)
  185. end;
  186. end;
  187. procedure TTestJSON.TestAsInteger(J: TJSONData; Expected: Integer;
  188. ExpectError: boolean);
  189. Var
  190. I : Integer;
  191. AssignOK : Boolean;
  192. Msg : String;
  193. begin
  194. AssignOK:=False;
  195. Try
  196. I:=J.AsInteger;
  197. AssignOK:=True;
  198. If Not ExpectError then
  199. AssertEquals(J.Classname+'.AsInteger',Expected,I);
  200. except
  201. On E : Exception do
  202. begin
  203. AssignOK:=False;
  204. Msg:=E.Message;
  205. end;
  206. end;
  207. If ExpectError then
  208. begin
  209. If AssignOK then
  210. Fail(J.ClassName+'.AsInteger must raise error');
  211. end
  212. else
  213. begin
  214. If not AssignOK then
  215. Fail(J.ClassName+'.AsInteger raised unexpected exception: '+Msg)
  216. end;
  217. end;
  218. procedure TTestJSON.TestAsString(J: TJSONData; Expected: String;
  219. ExpectError: boolean);
  220. Var
  221. S : String;
  222. AssignOK : Boolean;
  223. Msg : String;
  224. begin
  225. AssignOK:=False;
  226. Try
  227. S:=J.AsString;
  228. AssignOK:=True;
  229. If Not ExpectError then
  230. AssertEquals(J.Classname+'.AsString',Expected,S);
  231. except
  232. On E : Exception do
  233. begin
  234. AssignOK:=False;
  235. Msg:=E.Message;
  236. end;
  237. end;
  238. If ExpectError then
  239. begin
  240. If AssignOK then
  241. Fail(J.ClassName+'.AsString must raise error');
  242. end
  243. else
  244. begin
  245. If not AssignOK then
  246. Fail(J.ClassName+'.AsString raised unexpected exception: '+Msg)
  247. end;
  248. end;
  249. procedure TTestJSON.TestAsFloat(J: TJSONData; Expected: TJSONFloat;
  250. ExpectError: boolean);
  251. Var
  252. F : TJSONFloat;
  253. AssignOK : Boolean;
  254. Msg : String;
  255. begin
  256. AssignOK:=False;
  257. Try
  258. F:=J.AsFloat;
  259. AssignOK:=True;
  260. If Not ExpectError then
  261. AssertEquals(J.Classname+'.AsFloat',Expected,F);
  262. except
  263. On E : Exception do
  264. begin
  265. AssignOK:=False;
  266. Msg:=E.Message;
  267. end;
  268. end;
  269. If ExpectError then
  270. begin
  271. If AssignOK then
  272. Fail(J.ClassName+'.AsFloat must raise error');
  273. end
  274. else
  275. begin
  276. If not AssignOK then
  277. Fail(J.ClassName+'.AsFloat raised unexpected exception: '+Msg)
  278. end;
  279. end;
  280. { TTestBoolean }
  281. procedure TTestBoolean.TestTrue;
  282. Var
  283. J : TJSONBoolean;
  284. begin
  285. J:=TJSONBoolean.Create(True);
  286. try
  287. TestJSONType(J,jtBoolean);
  288. TestItemCount(J,0);
  289. TestJSON(J,'True');
  290. TestIsNull(J,False);
  291. TestAsBoolean(J,True);
  292. TestAsInteger(J,1);
  293. TestAsString(J,BoolToStr(True));
  294. TestAsFloat(J,1.0);
  295. finally
  296. FreeAndNil(J);
  297. end;
  298. end;
  299. procedure TTestBoolean.TestFalse;
  300. Var
  301. J : TJSONBoolean;
  302. begin
  303. J:=TJSONBoolean.Create(False);
  304. try
  305. TestJSONType(J,jtBoolean);
  306. TestItemCount(J,0);
  307. TestJSON(J,'False');
  308. TestIsNull(J,False);
  309. TestAsBoolean(J,False);
  310. TestAsInteger(J,0);
  311. TestAsString(J,BoolToStr(False));
  312. TestAsFloat(J,0.0);
  313. finally
  314. FreeAndNil(J);
  315. end;
  316. end;
  317. { TTestNull }
  318. procedure TTestNull.TestNull;
  319. Var
  320. J : TJSONNull;
  321. begin
  322. J:=TJSONNull.Create;
  323. try
  324. TestJSONType(J,jtNull);
  325. TestItemCount(J,0);
  326. TestJSON(J,'Null');
  327. TestIsNull(J,True);
  328. TestAsBoolean(J,False,True);
  329. TestAsInteger(J,0,true);
  330. TestAsString(J,BoolToStr(False),true);
  331. TestAsFloat(J,0.0,true);
  332. finally
  333. FreeAndNil(J);
  334. end;
  335. end;
  336. { TTestString }
  337. procedure TTestString.TestString;
  338. Const
  339. S = 'A string';
  340. Var
  341. J : TJSONString;
  342. begin
  343. J:=TJSONString.Create(S);
  344. try
  345. TestJSONType(J,jtString);
  346. TestItemCount(J,0);
  347. TestJSON(J,'"'+S+'"');
  348. TestIsNull(J,False);
  349. TestAsBoolean(J,False,True);
  350. TestAsInteger(J,0,true);
  351. TestAsString(J,S);
  352. TestAsFloat(J,0.0,true);
  353. finally
  354. FreeAndNil(J);
  355. end;
  356. end;
  357. procedure TTestString.TestInteger;
  358. Const
  359. S = '1';
  360. Var
  361. J : TJSONString;
  362. begin
  363. J:=TJSONString.Create(S);
  364. try
  365. TestJSONType(J,jtString);
  366. TestItemCount(J,0);
  367. TestJSON(J,'"'+S+'"');
  368. TestIsNull(J,False);
  369. TestAsBoolean(J,True,False);
  370. TestAsInteger(J,1,False);
  371. TestAsString(J,S);
  372. TestAsFloat(J,1.0,False);
  373. finally
  374. FreeAndNil(J);
  375. end;
  376. end;
  377. procedure TTestString.TestNegativeInteger;
  378. Const
  379. S = '-1';
  380. Var
  381. J : TJSONString;
  382. begin
  383. J:=TJSONString.Create(S);
  384. try
  385. TestJSONType(J,jtString);
  386. TestItemCount(J,0);
  387. TestJSON(J,'"'+S+'"');
  388. TestIsNull(J,False);
  389. TestAsBoolean(J,True,False);
  390. TestAsInteger(J,-1,False);
  391. TestAsString(J,S);
  392. TestAsFloat(J,-1.0,False);
  393. finally
  394. FreeAndNil(J);
  395. end;
  396. end;
  397. procedure TTestString.TestFloat;
  398. begin
  399. DoTestFloat(1.0,'1.0',True);
  400. DoTestFloat(1.0,'1',True);
  401. DoTestFloat(1.0,'1e0',True);
  402. DoTestFloat(1.2,'1.2',True);
  403. DoTestFloat(12.0,'1.2e1',True);
  404. end;
  405. procedure TTestString.TestNegativeFloat;
  406. begin
  407. DoTestFloat(-1.0,'-1.0',True);
  408. DoTestFloat(-1.0,'-1',True);
  409. DoTestFloat(-1.0,'-1e0',True);
  410. DoTestFloat(-1.2,'-1.2',True);
  411. DoTestFloat(-12.0,'-1.2e1',True);
  412. end;
  413. procedure TTestString.TestBooleanTrue;
  414. Const
  415. S = 'True';
  416. Var
  417. J : TJSONString;
  418. begin
  419. J:=TJSONString.Create(S);
  420. try
  421. TestJSONType(J,jtString);
  422. TestItemCount(J,0);
  423. TestJSON(J,'"'+S+'"');
  424. TestIsNull(J,False);
  425. TestAsBoolean(J,True,False);
  426. TestAsInteger(J,-1,True);
  427. TestAsString(J,S);
  428. TestAsFloat(J,-1.0,True);
  429. finally
  430. FreeAndNil(J);
  431. end;
  432. end;
  433. procedure TTestString.TestBooleanFalse;
  434. Const
  435. S = 'False';
  436. Var
  437. J : TJSONString;
  438. begin
  439. J:=TJSONString.Create(S);
  440. try
  441. TestJSONType(J,jtString);
  442. TestItemCount(J,0);
  443. TestJSON(J,'"'+S+'"');
  444. TestIsNull(J,False);
  445. TestAsBoolean(J,False,False);
  446. TestAsInteger(J,0,True);
  447. TestAsString(J,S);
  448. TestAsFloat(J,0,True);
  449. finally
  450. FreeAndNil(J);
  451. end;
  452. end;
  453. procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean);
  454. Var
  455. J : TJSONString;
  456. begin
  457. J:=TJSONString.Create(S);
  458. try
  459. TestJSONType(J,jtString);
  460. TestItemCount(J,0);
  461. TestJSON(J,'"'+S+'"');
  462. TestIsNull(J,False);
  463. TestAsBoolean(J,(F<>0),Not OK);
  464. TestAsInteger(J,Round(F),(Pos('.',S)<>0) or (Pos('E',UpperCase(S))<>0));
  465. TestAsString(J,S);
  466. TestAsFloat(J,F,Not OK);
  467. finally
  468. FreeAndNil(J);
  469. end;
  470. end;
  471. { TTestInteger }
  472. procedure TTestInteger.DoTest(I: Integer);
  473. Var
  474. J : TJSONIntegerNumber;
  475. begin
  476. J:=TJSONIntegerNumber.Create(I);
  477. try
  478. TestJSONType(J,jtNumber);
  479. TestItemCount(J,0);
  480. AssertEquals('Numbertype is ntInteger',ord(ntInteger),Ord(J.NumberType));
  481. TestJSON(J,IntToStr(i));
  482. TestIsNull(J,False);
  483. TestAsBoolean(J,(I<>0));
  484. TestAsInteger(J,I);
  485. TestAsString(J,IntToStr(I));
  486. TestAsFloat(J,I);
  487. finally
  488. FreeAndNil(J);
  489. end;
  490. end;
  491. procedure TTestInteger.TestPositive;
  492. begin
  493. DoTest(1);
  494. end;
  495. procedure TTestInteger.TestNegative;
  496. begin
  497. DoTest(-1);
  498. end;
  499. procedure TTestInteger.TestZero;
  500. begin
  501. DoTest(0);
  502. end;
  503. { TTestFloat }
  504. procedure TTestFloat.DoTest(F: TJSONFloat);
  505. Var
  506. J : TJSONFloatNumber;
  507. S : String;
  508. begin
  509. Str(F,S);
  510. J:=TJSONFloatNumber.Create(F);
  511. try
  512. TestJSONType(J,jtNumber);
  513. TestItemCount(J,0);
  514. AssertEquals('Numbertype is ntFloat',ord(ntFloat),Ord(J.NumberType));
  515. TestJSON(J,S);
  516. TestIsNull(J,False);
  517. TestAsBoolean(J,(F<>0));
  518. TestAsInteger(J,Round(F));
  519. TestAsString(J,S);
  520. TestAsFloat(J,F);
  521. finally
  522. FreeAndNil(J);
  523. end;
  524. end;
  525. procedure TTestFloat.TestPositive;
  526. begin
  527. DoTest(1.0);
  528. DoTest(1.2);
  529. DoTest(1.2e1);
  530. DoTest(1.2e-1);
  531. DoTest(1.2e10);
  532. DoTest(1.2e-10);
  533. end;
  534. procedure TTestFloat.TestNegative;
  535. begin
  536. DoTest(-1.0);
  537. DoTest(-1.2);
  538. DoTest(-1.2e1);
  539. DoTest(-1.2e-1);
  540. DoTest(-1.2e10);
  541. DoTest(-1.2e-10);
  542. end;
  543. procedure TTestFloat.TestZero;
  544. begin
  545. DoTest(0.0);
  546. end;
  547. { TTestArray }
  548. procedure TTestArray.TestCreate;
  549. Var
  550. J : TJSONArray;
  551. begin
  552. J:=TJSonArray.Create;
  553. try
  554. TestJSONType(J,jtArray);
  555. TestItemCount(J,0);
  556. TestJSON(J,'[]');
  557. TestIsNull(J,False);
  558. TestAsBoolean(J,False,True);
  559. TestAsInteger(J,1,True);
  560. TestAsString(J,'',True);
  561. TestAsFloat(J,0.0,True);
  562. finally
  563. FreeAndNil(J);
  564. end;
  565. end;
  566. procedure TTestArray.TestCreateString;
  567. Const
  568. S = 'A string';
  569. Var
  570. J : TJSONArray;
  571. begin
  572. J:=TJSonArray.Create([S]);
  573. try
  574. TestJSONType(J,jtArray);
  575. TestItemCount(J,1);
  576. TestJSONType(J[0],jtString);
  577. TestJSON(J,'["'+S+'"]');
  578. TestIsNull(J,False);
  579. finally
  580. FreeAndNil(J);
  581. end;
  582. end;
  583. procedure TTestArray.TestCreatePChar;
  584. Const
  585. S = 'A string';
  586. Var
  587. J : TJSONArray;
  588. begin
  589. J:=TJSonArray.Create([Pchar(S)]);
  590. try
  591. TestJSONType(J,jtArray);
  592. TestItemCount(J,1);
  593. TestJSONType(J[0],jtString);
  594. TestJSON(J,'["'+S+'"]');
  595. TestIsNull(J,False);
  596. finally
  597. FreeAndNil(J);
  598. end;
  599. end;
  600. procedure TTestArray.TestCreateStrings;
  601. Const
  602. S = 'A string';
  603. T = 'B string';
  604. Var
  605. J : TJSONArray;
  606. begin
  607. J:=TJSONArray.Create([S,T]);
  608. try
  609. TestJSONType(J,jtArray);
  610. TestItemCount(J,2);
  611. TestJSONType(J[0],jtString);
  612. TestJSONType(J[1],jtString);
  613. TestJSON(J,'["'+S+'", "'+T+'"]');
  614. TestIsNull(J,False);
  615. finally
  616. FreeAndNil(J);
  617. end;
  618. end;
  619. procedure TTestArray.TestCreateInteger;
  620. Const
  621. S = 3;
  622. Var
  623. J : TJSONArray;
  624. begin
  625. J:=TJSonArray.Create([S]);
  626. try
  627. TestJSONType(J,jtArray);
  628. TestItemCount(J,1);
  629. TestJSONType(J[0],jtNumber);
  630. TestJSON(J,'[3]');
  631. finally
  632. FreeAndNil(J);
  633. end;
  634. end;
  635. procedure TTestArray.TestCreateFloat;
  636. Const
  637. S = 1.2;
  638. Var
  639. J : TJSONArray;
  640. r : String;
  641. begin
  642. J:=TJSonArray.Create([S]);
  643. try
  644. TestJSONType(J,jtArray);
  645. TestItemCount(J,1);
  646. TestJSONType(J[0],jtNumber);
  647. Str(S,R);
  648. TestJSON(J,'['+R+']');
  649. finally
  650. FreeAndNil(J);
  651. end;
  652. end;
  653. procedure TTestArray.TestCreateInt64;
  654. Const
  655. S : Int64 = $FFFFFF;
  656. Var
  657. J : TJSONArray;
  658. r : String;
  659. F : TJSONFloat;
  660. begin
  661. J:=TJSonArray.Create([S]);
  662. try
  663. TestJSONType(J,jtArray);
  664. TestItemCount(J,1);
  665. TestJSONType(J[0],jtNumber);
  666. F:=S;
  667. Str(F,R);
  668. TestJSON(J,'['+R+']');
  669. finally
  670. FreeAndNil(J);
  671. end;
  672. end;
  673. procedure TTestArray.TestCreateBoolean;
  674. Const
  675. S = True;
  676. Var
  677. J : TJSONArray;
  678. begin
  679. J:=TJSonArray.Create([S]);
  680. try
  681. TestJSONType(J,jtArray);
  682. TestItemCount(J,1);
  683. TestJSONType(J[0],jtBoolean);
  684. TestJSON(J,'[True]');
  685. finally
  686. FreeAndNil(J);
  687. end;
  688. end;
  689. procedure TTestArray.TestCreateJSONObject;
  690. Var
  691. J : TJSONArray;
  692. begin
  693. J:=TJSonArray.Create([TJSONObject.Create]);
  694. try
  695. TestItemCount(J,1);
  696. TestJSONType(J[0],jtObject);
  697. TestJSON(J,'[{}]');
  698. finally
  699. FreeAndNil(J);
  700. end;
  701. end;
  702. procedure TTestArray.TestCreateJSONString;
  703. Const
  704. S = 'A string';
  705. Var
  706. J : TJSONArray;
  707. begin
  708. J:=TJSonArray.Create([TJSONString.Create(S)]);
  709. try
  710. TestItemCount(J,1);
  711. TestJSONType(J[0],jtString);
  712. TestJSON(J,'["'+S+'"]');
  713. finally
  714. FreeAndNil(J);
  715. end;
  716. end;
  717. procedure TTestArray.TestCreateObject;
  718. Var
  719. J : TJSONArray;
  720. begin
  721. J:=Nil;
  722. try
  723. Try
  724. J:=TJSONArray.Create([TObject.Create]);
  725. Fail('Array constructor accepts only TJSONData');
  726. finally
  727. FreeAndNil(J);
  728. end;
  729. except
  730. // Should be OK.
  731. end;
  732. end;
  733. procedure TTestArray.TestCreateNilPointer;
  734. Var
  735. J : TJSONArray;
  736. P : Pointer;
  737. begin
  738. J:=Nil;
  739. P:=Nil;
  740. Try
  741. J:=TJSONArray.Create([P]);
  742. TestJSONType(J[0],jtNull);
  743. finally
  744. FreeAndNil(J);
  745. end;
  746. end;
  747. procedure TTestArray.TestCreatePointer;
  748. Var
  749. J : TJSONArray;
  750. P : Pointer;
  751. begin
  752. J:=Nil;
  753. P:=@Self;
  754. try
  755. Try
  756. J:=TJSONArray.Create([P]);
  757. Fail('Array constructor accepts only NIL pointers');
  758. finally
  759. FreeAndNil(J);
  760. end;
  761. except
  762. // Should be OK.
  763. end;
  764. end;
  765. procedure TTestArray.TestAddInteger;
  766. Var
  767. J : TJSONArray;
  768. begin
  769. J:=TJSonArray.Create;
  770. try
  771. J.Add(0);
  772. TestItemCount(J,1);
  773. TestJSONType(J[0],jtNumber);
  774. AssertEquals('J[0] is TJSONIntegerNumber',J[0].ClassType,TJSONIntegerNumber);
  775. AssertEquals('j.Types[0]=jtNumber',ord(J.Types[0]),Ord(jtNumber));
  776. AssertEquals('J.Integers[0]=0',0,J.integers[0]);
  777. TestAsInteger(J[0],0);
  778. TestJSON(J,'[0]');
  779. finally
  780. FreeAndNil(J);
  781. end;
  782. end;
  783. procedure TTestArray.TestAddFloat;
  784. Var
  785. J : TJSONArray;
  786. S : String;
  787. F : TJSONFloat;
  788. begin
  789. F:=1.2;
  790. J:=TJSonArray.Create;
  791. try
  792. J.Add(F);
  793. TestItemCount(J,1);
  794. TestJSONType(J[0],jtNumber);
  795. AssertEquals('J[0] is TJSONFloatNumber',TJSONfloatNumber,J[0].ClassType);
  796. AssertEquals('j.Types[0]=jtNumber',Ord(jtNumber),ord(J.Types[0]));
  797. AssertEquals('J.Floats[0]='+FloatToStr(F),F,J.Floats[0]);
  798. TestAsFloat(J[0],F);
  799. Str(F,S);
  800. TestJSON(J,'['+S+']');
  801. finally
  802. FreeAndNil(J);
  803. end;
  804. end;
  805. procedure TTestArray.TestAddBoolean(B : Boolean);
  806. Var
  807. J : TJSONArray;
  808. begin
  809. B:=True;
  810. J:=TJSonArray.Create;
  811. try
  812. J.Add(B);
  813. TestItemCount(J,1);
  814. TestJSONType(J[0],jtBoolean);
  815. AssertEquals('J[0] is TJSONBoolean',TJSONBoolean,J[0].ClassType);
  816. TestAsBoolean(J[0],B);
  817. AssertEquals('J.Booleans[0]='+BoolToStr(B)+'"',B,J.Booleans[0]);
  818. If B then
  819. TestJSON(J,'[True]')
  820. else
  821. TestJSON(J,'[False]');
  822. finally
  823. FreeAndNil(J);
  824. end;
  825. end;
  826. procedure TTestArray.TestAddBooleanTrue;
  827. begin
  828. TestAddBoolean(True);
  829. end;
  830. procedure TTestArray.TestAddBooleanFalse;
  831. begin
  832. TestAddBoolean(False);
  833. end;
  834. procedure TTestArray.TestAddString;
  835. Var
  836. J : TJSONArray;
  837. S : String;
  838. F : TJSONFloat;
  839. begin
  840. S:='A string';
  841. J:=TJSonArray.Create;
  842. try
  843. J.Add(S);
  844. TestItemCount(J,1);
  845. TestJSONType(J[0],jtString);
  846. AssertEquals('J[0] is TJSONString',TJSONString,J[0].ClassType);
  847. TestAsString(J[0],S);
  848. AssertEquals('J.Strings[0]="'+S+'"',S,J.Strings[0]);
  849. TestJSON(J,'["'+StringToJSONString(S)+'"]');
  850. finally
  851. FreeAndNil(J);
  852. end;
  853. end;
  854. procedure TTestArray.TestAddNull;
  855. Var
  856. J : TJSONArray;
  857. S : String;
  858. F : TJSONFloat;
  859. begin
  860. J:=TJSonArray.Create;
  861. try
  862. J.Add;
  863. TestItemCount(J,1);
  864. TestJSONType(J[0],jtNull);
  865. AssertEquals('J[0] is TJSONNull',TJSONNull,J[0].ClassType);
  866. AssertEquals('J.Nulls[0]=True',True,J.Nulls[0]);
  867. TestIsNull(J[0],true);
  868. TestJSON(J,'[Null]');
  869. finally
  870. FreeAndNil(J);
  871. end;
  872. end;
  873. procedure TTestArray.TestAddArray;
  874. Var
  875. J,J2 : TJSONArray;
  876. begin
  877. J:=TJSonArray.Create;
  878. try
  879. J2:=TJSonArray.Create;
  880. J2.Add(0);
  881. J2.Add(1);
  882. J.Add(J2);
  883. TestItemCount(J,1);
  884. TestJSONType(J[0],jtArray);
  885. AssertEquals('J[0] is TJSONArray',TJSONArray,J[0].ClassType);
  886. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[0].ClassType);
  887. TestAsInteger(J.Arrays[0][0],0);
  888. TestAsInteger(J.Arrays[0][1],1);
  889. TestJSON(J,'[[0, 1]]');
  890. finally
  891. FreeAndNil(J);
  892. end;
  893. end;
  894. procedure TTestArray.TestAddObject;
  895. Const
  896. A = 'a';
  897. B = 'b';
  898. Var
  899. J : TJSONArray;
  900. J2 : TJSONObject;
  901. begin
  902. J:=TJSonArray.Create;
  903. try
  904. J2:=TJSonObject.Create;
  905. J2.Add(A,0);
  906. J2.Add(B,1);
  907. J.Add(J2);
  908. TestItemCount(J,1);
  909. TestJSONType(J[0],jtObject);
  910. AssertEquals('J[0] is TJSONObject',TJSONObject,J[0].ClassType);
  911. AssertEquals('J.Objects[0] is TJSONObject',TJSONObject,J.Objects[0].ClassType);
  912. TestAsInteger(J.Objects[0][A],0);
  913. TestAsInteger(J.Objects[0][B],1);
  914. TestJSON(J,'[{ "a" : 0, "b" : 1 }]');
  915. finally
  916. FreeAndNil(J);
  917. end;
  918. end;
  919. procedure TTestArray.TestDelete;
  920. Var
  921. J : TJSONArray;
  922. begin
  923. J:=TJSonArray.Create;
  924. try
  925. J.Add(0);
  926. J.Add(1);
  927. TestItemCount(J,2);
  928. TestJSONType(J[0],jtNumber);
  929. TestJSONType(J[1],jtNumber);
  930. TestJSON(J,'[0, 1]');
  931. J.Delete(1);
  932. TestItemCount(J,1);
  933. J.Delete(0);
  934. TestItemCount(J,0);
  935. finally
  936. FreeAndNil(J);
  937. end;
  938. end;
  939. procedure TTestArray.TestRemove;
  940. Var
  941. J : TJSONArray;
  942. I : TJSONData;
  943. begin
  944. J:=TJSonArray.Create;
  945. try
  946. J.Add(0);
  947. J.Add(1);
  948. J.Add(2);
  949. TestItemCount(J,3);
  950. TestJSONType(J[0],jtNumber);
  951. TestJSONType(J[1],jtNumber);
  952. TestJSONType(J[2],jtNumber);
  953. TestJSON(J,'[0, 1, 2]');
  954. I:=J[1];
  955. J.Remove(I);
  956. TestItemCount(J,2);
  957. TestAsInteger(J[0],0);
  958. TestAsInteger(J[1],2);
  959. finally
  960. FreeAndNil(J);
  961. end;
  962. end;
  963. { TTestObject }
  964. procedure TTestObject.TestCreate;
  965. Var
  966. J : TJSONObject;
  967. begin
  968. J:=TJSONObject.Create;
  969. try
  970. TestJSONType(J,jtObject);
  971. TestItemCount(J,0);
  972. TestJSON(J,'{}');
  973. TestIsNull(J,False);
  974. TestAsBoolean(J,False,True);
  975. TestAsInteger(J,1,True);
  976. TestAsString(J,'',True);
  977. TestAsFloat(J,0.0,True);
  978. finally
  979. FreeAndNil(J);
  980. end;
  981. end;
  982. procedure TTestObject.TestAddInteger;
  983. Const
  984. A = 'a';
  985. Var
  986. J : TJSONObject;
  987. begin
  988. J:=TJSonObject.Create;
  989. try
  990. J.Add(A,0);
  991. TestItemCount(J,1);
  992. TestJSONType(J[A],jtNumber);
  993. AssertEquals('J[''a''] is TJSONIntegerNumber',J[A].ClassType,TJSONIntegerNumber);
  994. AssertEquals('j.Types[''a'']=jtNumber',ord(J.Types[A]),Ord(jtNumber));
  995. AssertEquals('J.Integers[''a'']=0',0,J.integers[A]);
  996. TestAsInteger(J[A],0);
  997. TestJSON(J,'{ "'+A+'" : 0 }');
  998. finally
  999. FreeAndNil(J);
  1000. end;
  1001. end;
  1002. procedure TTestObject.TestAddFloat;
  1003. Const
  1004. A = 'a';
  1005. Var
  1006. J : TJSONObject;
  1007. S : String;
  1008. F : TJSONFloat;
  1009. begin
  1010. F:=1.2;
  1011. J:=TJSonObject.Create;
  1012. try
  1013. J.Add(A,F);
  1014. TestItemCount(J,1);
  1015. TestJSONType(J[A],jtNumber);
  1016. AssertEquals('J[''a''] is TJSONFloatNumber',TJSONfloatNumber,J[a].ClassType);
  1017. AssertEquals('j.Types[''a'']=jtNumber',Ord(jtNumber),ord(J.Types[a]));
  1018. AssertEquals('J.Floats[''a'']='+FloatToStr(F),F,J.Floats[a]);
  1019. TestAsFloat(J[A],F);
  1020. Str(F,S);
  1021. TestJSON(J,'{ "'+a+'" : '+S+' }');
  1022. finally
  1023. FreeAndNil(J);
  1024. end;
  1025. end;
  1026. procedure TTestObject.TestAddBoolean(B : Boolean);
  1027. Const
  1028. A = 'a';
  1029. Var
  1030. J : TJSONObject;
  1031. begin
  1032. B:=True;
  1033. J:=TJSonObject.Create;
  1034. try
  1035. J.Add(A,B);
  1036. TestItemCount(J,1);
  1037. TestJSONType(J[A],jtBoolean);
  1038. AssertEquals('J[''a''] is TJSONBoolean',TJSONBoolean,J[a].ClassType);
  1039. TestAsBoolean(J[a],B);
  1040. AssertEquals('J.Booleans[''a'']='+BoolToStr(B)+'"',B,J.Booleans[a]);
  1041. If B then
  1042. TestJSON(J,'{ "'+a+'" : True }')
  1043. else
  1044. TestJSON(J,'{ "'+a+'" : False }');
  1045. finally
  1046. FreeAndNil(J);
  1047. end;
  1048. end;
  1049. procedure TTestObject.TestAddBooleanTrue;
  1050. begin
  1051. TestAddBoolean(True);
  1052. end;
  1053. procedure TTestObject.TestAddBooleanFalse;
  1054. begin
  1055. TestAddBoolean(False);
  1056. end;
  1057. procedure TTestObject.TestAddString;
  1058. Const
  1059. A = 'a';
  1060. Var
  1061. J : TJSONObject;
  1062. S : String;
  1063. F : TJSONFloat;
  1064. begin
  1065. S:='A string';
  1066. J:=TJSonObject.Create;
  1067. try
  1068. J.Add(A,S);
  1069. TestItemCount(J,1);
  1070. TestJSONType(J[a],jtString);
  1071. AssertEquals('J[''a''] is TJSONString',TJSONString,J[A].ClassType);
  1072. TestAsString(J[a],S);
  1073. AssertEquals('J.Strings[''a'']="'+S+'"',S,J.Strings[A]);
  1074. TestJSON(J,'{ "'+a+'" : "'+StringToJSONString(S)+'" }');
  1075. finally
  1076. FreeAndNil(J);
  1077. end;
  1078. end;
  1079. procedure TTestObject.TestAddNull;
  1080. Const
  1081. A = 'a';
  1082. Var
  1083. J : TJSONObject;
  1084. S : String;
  1085. F : TJSONFloat;
  1086. begin
  1087. J:=TJSonObject.Create;
  1088. try
  1089. J.Add(a);
  1090. TestItemCount(J,1);
  1091. TestJSONType(J[a],jtNull);
  1092. AssertEquals('J[''a''] is TJSONNull',TJSONNull,J[A].ClassType);
  1093. AssertEquals('J.Nulls[''a'']=True',True,J.Nulls[A]);
  1094. TestIsNull(J[a],true);
  1095. TestJSON(J,'{ "'+a+'" : Null }');
  1096. finally
  1097. FreeAndNil(J);
  1098. end;
  1099. end;
  1100. procedure TTestObject.TestAddObject;
  1101. Const
  1102. A = 'a';
  1103. B = 'b';
  1104. C = 'c';
  1105. Var
  1106. J,J2 : TJSONObject;
  1107. begin
  1108. J:=TJSonObject.Create;
  1109. try
  1110. J2:=TJSonObject.Create;
  1111. J2.Add(B,0);
  1112. J2.Add(C,1);
  1113. J.Add(A,J2);
  1114. TestItemCount(J,1);
  1115. TestJSONType(J[A],jtObject);
  1116. AssertEquals('J[''a''] is TJSONObject',TJSONObject,J[A].ClassType);
  1117. AssertEquals('J.Objects[''a''] is TJSONObject',TJSONObject,J.Objects[A].ClassType);
  1118. TestAsInteger(J.Objects[A][B],0);
  1119. TestAsInteger(J.Objects[A][C],1);
  1120. TestJSON(J,'{ "a" : { "b" : 0, "c" : 1 } }');
  1121. finally
  1122. FreeAndNil(J);
  1123. end;
  1124. end;
  1125. procedure TTestObject.TestAddArray;
  1126. Const
  1127. A = 'a';
  1128. Var
  1129. J : TJSONObject;
  1130. J2 : TJSONArray;
  1131. begin
  1132. J:=TJSONObject.Create;
  1133. try
  1134. J2:=TJSonArray.Create;
  1135. J2.Add(0);
  1136. J2.Add(1);
  1137. J.Add(A,J2);
  1138. TestItemCount(J,1);
  1139. TestJSONType(J[A],jtArray);
  1140. AssertEquals('J[''a''] is TJSONArray',TJSONArray,J[A].ClassType);
  1141. AssertEquals('J.Arrays[0] is TJSONArray',TJSONArray,J.Arrays[A].ClassType);
  1142. TestAsInteger(J.Arrays[A][0],0);
  1143. TestAsInteger(J.Arrays[A][1],1);
  1144. TestJSON(J,'{ "a" : [0, 1] }');
  1145. finally
  1146. FreeAndNil(J);
  1147. end;
  1148. end;
  1149. procedure TTestObject.TestDelete;
  1150. Const
  1151. A = 'a';
  1152. B = 'b';
  1153. Var
  1154. J : TJSONObject;
  1155. begin
  1156. J:=TJSonObject.Create;
  1157. try
  1158. J.Add(A,0);
  1159. J.Add(B,1);
  1160. TestItemCount(J,2);
  1161. TestJSONType(J[A],jtNumber);
  1162. TestJSONType(J[A],jtNumber);
  1163. TestJSON(J,'{ "a" : 0, "b" : 1 }');
  1164. J.Delete(1);
  1165. TestItemCount(J,1);
  1166. J.Delete(0);
  1167. TestItemCount(J,0);
  1168. finally
  1169. FreeAndNil(J);
  1170. end;
  1171. end;
  1172. procedure TTestObject.TestRemove;
  1173. Const
  1174. A = 'a';
  1175. B = 'b';
  1176. C = 'c';
  1177. Var
  1178. J : TJSONObject;
  1179. I : TJSONData;
  1180. begin
  1181. J:=TJSonObject.Create;
  1182. try
  1183. J.Add(A,1);
  1184. J.Add(B,2);
  1185. J.Add(C,3);
  1186. TestItemCount(J,3);
  1187. TestJSONType(J[A],jtNumber);
  1188. TestJSONType(J[B],jtNumber);
  1189. TestJSONType(J[C],jtNumber);
  1190. TestJSON(J,'{ "a" : 1, "b" : 2, "c" : 3 }');
  1191. I:=J[b];
  1192. J.Remove(I);
  1193. TestItemCount(J,2);
  1194. TestAsInteger(J[a],1);
  1195. TestAsInteger(J[c],3);
  1196. finally
  1197. FreeAndNil(J);
  1198. end;
  1199. end;
  1200. procedure TTestObject.TestCreateString;
  1201. Const
  1202. A = 'A';
  1203. S = 'A string';
  1204. Var
  1205. J : TJSONObject;
  1206. begin
  1207. J:=TJSONObject.Create([A,S]);
  1208. try
  1209. TestJSONType(J,jtObject);
  1210. TestItemCount(J,1);
  1211. TestJSONType(J[A],jtString);
  1212. TestJSON(J,'{ "A" : "'+S+'" }');
  1213. TestIsNull(J,False);
  1214. finally
  1215. FreeAndNil(J);
  1216. end;
  1217. end;
  1218. procedure TTestObject.TestCreatePChar;
  1219. Const
  1220. A = 'A';
  1221. S = 'A string';
  1222. Var
  1223. J : TJSONObject;
  1224. begin
  1225. J:=TJSONObject.Create([A,Pchar(S)]);
  1226. try
  1227. TestJSONType(J,jtObject);
  1228. TestItemCount(J,1);
  1229. TestJSONType(J[A],jtString);
  1230. TestJSON(J,'{ "A" : "'+S+'" }');
  1231. TestIsNull(J,False);
  1232. finally
  1233. FreeAndNil(J);
  1234. end;
  1235. end;
  1236. procedure TTestObject.TestCreateStrings;
  1237. Const
  1238. A = 'A';
  1239. B = 'B';
  1240. S = 'A string';
  1241. T = 'B string';
  1242. Var
  1243. J : TJSONObject;
  1244. begin
  1245. J:=TJSONObject.Create([A,S,B,T]);
  1246. try
  1247. TestJSONType(J,jtObject);
  1248. TestItemCount(J,2);
  1249. TestJSONType(J[A],jtString);
  1250. TestJSONType(J[B],jtString);
  1251. TestJSON(J,'{ "A" : "'+S+'", "B" : "'+T+'" }');
  1252. TestIsNull(J,False);
  1253. finally
  1254. FreeAndNil(J);
  1255. end;
  1256. end;
  1257. procedure TTestObject.TestCreateInteger;
  1258. Const
  1259. A = 'A';
  1260. S = 3;
  1261. Var
  1262. J : TJSONObject;
  1263. begin
  1264. J:=TJSONObject.Create([A,S]);
  1265. try
  1266. TestJSONType(J,jtObject);
  1267. TestItemCount(J,1);
  1268. TestJSONType(J[A],jtNumber);
  1269. TestJSON(J,'{ "A" : 3 }');
  1270. finally
  1271. FreeAndNil(J);
  1272. end;
  1273. end;
  1274. procedure TTestObject.TestCreateFloat;
  1275. Const
  1276. A = 'A';
  1277. S = 1.2;
  1278. Var
  1279. J : TJSONObject;
  1280. r : String;
  1281. begin
  1282. J:=TJSONObject.Create([A,S]);
  1283. try
  1284. TestJSONType(J,jtObject);
  1285. TestItemCount(J,1);
  1286. TestJSONType(J[A],jtNumber);
  1287. Str(S,R);
  1288. TestJSON(J,'{ "A" : '+R+' }');
  1289. finally
  1290. FreeAndNil(J);
  1291. end;
  1292. end;
  1293. procedure TTestObject.TestCreateInt64;
  1294. Const
  1295. A = 'A';
  1296. S : Int64 = $FFFFFF;
  1297. Var
  1298. J : TJSONObject;
  1299. r : String;
  1300. F : TJSONFloat;
  1301. begin
  1302. J:=TJSONObject.Create([A,S]);
  1303. try
  1304. TestJSONType(J,jtObject);
  1305. TestItemCount(J,1);
  1306. TestJSONType(J[A],jtNumber);
  1307. F:=S;
  1308. Str(F,R);
  1309. TestJSON(J,'{ "A" : '+R+' }');
  1310. finally
  1311. FreeAndNil(J);
  1312. end;
  1313. end;
  1314. procedure TTestObject.TestCreateBoolean;
  1315. Const
  1316. A = 'A';
  1317. S = True;
  1318. Var
  1319. J : TJSONObject;
  1320. begin
  1321. J:=TJSONObject.Create([A,S]);
  1322. try
  1323. TestJSONType(J,jtObject);
  1324. TestItemCount(J,1);
  1325. TestJSONType(J[A],jtBoolean);
  1326. TestJSON(J,'{ "A" : True }');
  1327. finally
  1328. FreeAndNil(J);
  1329. end;
  1330. end;
  1331. procedure TTestObject.TestCreateJSONObject;
  1332. Const
  1333. A = 'A';
  1334. Var
  1335. J : TJSONObject;
  1336. begin
  1337. J:=TJSONObject.Create([A,TJSONObject.Create]);
  1338. try
  1339. TestItemCount(J,1);
  1340. TestJSONType(J[A],jtObject);
  1341. TestJSON(J,'{ "A" : {} }');
  1342. finally
  1343. FreeAndNil(J);
  1344. end;
  1345. end;
  1346. procedure TTestObject.TestCreateJSONString;
  1347. Const
  1348. A = 'A';
  1349. S = 'A string';
  1350. Var
  1351. J : TJSONObject;
  1352. begin
  1353. J:=TJSONObject.Create([A,TJSONString.Create(S)]);
  1354. try
  1355. TestItemCount(J,1);
  1356. TestJSONType(J[A],jtString);
  1357. TestJSON(J,'{ "A" : "'+S+'" }');
  1358. finally
  1359. FreeAndNil(J);
  1360. end;
  1361. end;
  1362. procedure TTestObject.TestCreateObject;
  1363. Const
  1364. A = 'A';
  1365. Var
  1366. J : TJSONObject;
  1367. begin
  1368. J:=Nil;
  1369. try
  1370. Try
  1371. J:=TJSONObject.Create([A,TObject.Create]);
  1372. Fail('Array constructor accepts only TJSONData');
  1373. finally
  1374. FreeAndNil(J);
  1375. end;
  1376. except
  1377. // Should be OK.
  1378. end;
  1379. end;
  1380. procedure TTestObject.TestCreateNilPointer;
  1381. Const
  1382. A = 'A';
  1383. Var
  1384. J : TJSONObject;
  1385. P : Pointer;
  1386. begin
  1387. J:=Nil;
  1388. P:=Nil;
  1389. Try
  1390. J:=TJSONObject.Create([A,P]);
  1391. TestJSONType(J[A],jtNull);
  1392. finally
  1393. FreeAndNil(J);
  1394. end;
  1395. end;
  1396. procedure TTestObject.TestCreatePointer;
  1397. Const
  1398. A = 'A';
  1399. Var
  1400. J : TJSONObject;
  1401. P : Pointer;
  1402. begin
  1403. J:=Nil;
  1404. P:=@Self;
  1405. try
  1406. Try
  1407. J:=TJSONObject.Create([A,P]);
  1408. Fail('Array constructor accepts only NIL pointers');
  1409. finally
  1410. FreeAndNil(J);
  1411. end;
  1412. except
  1413. // Should be OK.
  1414. end;
  1415. end;
  1416. { TTestJSONString }
  1417. procedure TTestJSONString.TestTo(const Src, Dest: String);
  1418. Var
  1419. S : String;
  1420. begin
  1421. S:='StringToJSONString('''+Src+''')='''+Dest+'''';
  1422. AssertEquals(S,Dest,StringToJSONString(Src));
  1423. end;
  1424. procedure TTestJSONString.TestFrom(const Src, Dest: String);
  1425. Var
  1426. S : String;
  1427. begin
  1428. S:='JSONStringToString('''+Src+''')='''+Dest+'''';
  1429. AssertEquals(S,Dest,JSONStringToString(Src));
  1430. end;
  1431. procedure TTestJSONString.TestJSONStringToString;
  1432. begin
  1433. TestFrom('','');
  1434. TestFrom('A','A');
  1435. TestFrom('AB','AB');
  1436. TestFrom('ABC','ABC');
  1437. TestFrom('\\','\');
  1438. TestFrom('\/','/');
  1439. TestFrom('\"','"');
  1440. TestFrom('\b',#8);
  1441. TestFrom('\t',#9);
  1442. TestFrom('\n',#10);
  1443. TestFrom('\f',#12);
  1444. TestFrom('\r',#13);
  1445. TestFrom('\bBC',#8'BC');
  1446. TestFrom('\tBC',#9'BC');
  1447. TestFrom('\nBC',#10'BC');
  1448. TestFrom('\fBC',#12'BC');
  1449. TestFrom('\rBC',#13'BC');
  1450. TestFrom('A\b','A'#8);
  1451. TestFrom('A\t','A'#9);
  1452. TestFrom('A\n','A'#10);
  1453. TestFrom('A\f','A'#12);
  1454. TestFrom('A\r','A'#13);
  1455. TestFrom('A\bBC','A'#8'BC');
  1456. TestFrom('A\tBC','A'#9'BC');
  1457. TestFrom('A\nBC','A'#10'BC');
  1458. TestFrom('A\fBC','A'#12'BC');
  1459. TestFrom('A\rBC','A'#13'BC');
  1460. TestFrom('\\\\','\\');
  1461. TestFrom('\/\/','//');
  1462. TestFrom('\"\"','""');
  1463. TestFrom('\b\b',#8#8);
  1464. TestFrom('\t\t',#9#9);
  1465. TestFrom('\n\n',#10#10);
  1466. TestFrom('\f\f',#12#12);
  1467. TestFrom('\r\r',#13#13);
  1468. end;
  1469. procedure TTestJSONString.TestStringToJSONString;
  1470. begin
  1471. TestTo('','');
  1472. TestTo('A','A');
  1473. TestTo('AB','AB');
  1474. TestTo('ABC','ABC');
  1475. TestTo('\','\\');
  1476. TestTo('/','\/');
  1477. TestTo('"','\"');
  1478. TestTo(#8,'\b');
  1479. TestTo(#9,'\t');
  1480. TestTo(#10,'\n');
  1481. TestTo(#12,'\f');
  1482. TestTo(#13,'\r');
  1483. TestTo(#8'BC','\bBC');
  1484. TestTo(#9'BC','\tBC');
  1485. TestTo(#10'BC','\nBC');
  1486. TestTo(#12'BC','\fBC');
  1487. TestTo(#13'BC','\rBC');
  1488. TestTo('A'#8,'A\b');
  1489. TestTo('A'#9,'A\t');
  1490. TestTo('A'#10,'A\n');
  1491. TestTo('A'#12,'A\f');
  1492. TestTo('A'#13,'A\r');
  1493. TestTo('A'#8'BC','A\bBC');
  1494. TestTo('A'#9'BC','A\tBC');
  1495. TestTo('A'#10'BC','A\nBC');
  1496. TestTo('A'#12'BC','A\fBC');
  1497. TestTo('A'#13'BC','A\rBC');
  1498. TestTo('\\','\\\\');
  1499. TestTo('//','\/\/');
  1500. TestTo('""','\"\"');
  1501. TestTo(#8#8,'\b\b');
  1502. TestTo(#9#9,'\t\t');
  1503. TestTo(#10#10,'\n\n');
  1504. TestTo(#12#12,'\f\f');
  1505. TestTo(#13#13,'\r\r');
  1506. end;
  1507. initialization
  1508. RegisterTest(TTestJSONString);
  1509. RegisterTest(TTestNull);
  1510. RegisterTest(TTestBoolean);
  1511. RegisterTest(TTestInteger);
  1512. RegisterTest(TTestFloat);
  1513. RegisterTest(TTestString);
  1514. RegisterTest(TTestArray);
  1515. RegisterTest(TTestObject);
  1516. end.