testjsonrtti.pp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. unit testjsonrtti;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, typinfo, fpjson,
  6. dateutils, testcomps, testjsondata, fpjsonrtti;
  7. type
  8. { TTestJSONStreamer }
  9. TTestJSONStreamer = class(TTestJSON)
  10. private
  11. FRJ : TJSONStreamer;
  12. FSR : TJSONObject;
  13. FToFree : TObject;
  14. FCalled : Boolean;
  15. procedure DoStreamProperty1(Sender: TObject; AObject: TObject; Info: PPropInfo; var Res: TJSONData);
  16. protected
  17. procedure SetUp; override;
  18. procedure TearDown; override;
  19. Procedure AssertEquals(AMessage : String; Expected,Actual : TJSONType); overload;
  20. Procedure AssertPropCount(ACount : Integer);
  21. Function AssertProperty(APropName : String; AType : TJSONType) : TJSONData;
  22. Procedure AssertProp(APropName : String; AValue : Boolean);
  23. Procedure AssertProp(APropName : String; AValue : Integer);
  24. procedure AssertProp(APropName : String; AValue: String);
  25. procedure AssertProp(APropName : String; AValue: TJSONFloat);
  26. procedure AssertProp(APropName : String; AValue : Array of String);
  27. procedure AssertProp(APropName : String; AValue : Array of Integer);
  28. function CreateVariantComp : TVariantComponent;
  29. procedure AssertNullProp(APropName : String);
  30. Function AssertObjectProp(APropName : String) : TJSONObject;
  31. Function AssertArrayProp(APropName : String) : TJSONArray;
  32. Function StreamObject(AObject : TObject) : TJSONObject;
  33. Property RJ : TJSONStreamer read FRJ;
  34. Property SR : TJSONObject Read FSR Write FSR;
  35. published
  36. procedure TestNil;
  37. procedure TestEmpty;
  38. procedure TestEmptyComponent;
  39. procedure TestWriteBoolean;
  40. procedure TestWriteInteger;
  41. procedure TestWriteString;
  42. procedure TestWriteFloat;
  43. procedure TestWriteFloat2;
  44. procedure TestWriteFloat3;
  45. procedure TestWriteFloat4;
  46. procedure TestWriteFloat5;
  47. procedure TestEnum1;
  48. procedure TestEnum2;
  49. Procedure TestSet1;
  50. Procedure TestSet2;
  51. Procedure TestSet3;
  52. Procedure TestSet4;
  53. Procedure TestObjectNil;
  54. Procedure TestComponentProp1;
  55. Procedure TestComponentProp2;
  56. Procedure TestCollectionProp1;
  57. Procedure TestCollectionProp2;
  58. Procedure TestPersistentProp1;
  59. Procedure TestStringsProp1;
  60. Procedure TestStringsProp2;
  61. procedure TestStringsProp3;
  62. procedure TestStringsProp4;
  63. procedure TestStringsArray;
  64. procedure TestStringsObject;
  65. procedure TestStringsStream1;
  66. procedure TestStringsStream2;
  67. procedure TestStringsStream3;
  68. procedure TestStringsStream4;
  69. procedure TestStringsStream5;
  70. procedure TestCollectionStream;
  71. procedure TestCollectionStream2;
  72. procedure TestOnStreamProperty;
  73. Procedure TestDateTimeProp;
  74. Procedure TestDateTimePropDefaultString;
  75. Procedure TestDateTimePropDefaultStringTime;
  76. Procedure TestDateTimeProp2;
  77. Procedure TestDateTimeProp3;
  78. procedure TestDateTimeProp4;
  79. procedure TestDateTimeProp5;
  80. procedure TestDateTimeProp6;
  81. procedure TestDateTimeProp7;
  82. Procedure TestVariantShortint;
  83. Procedure TestVariantbyte;
  84. Procedure TestVariantword;
  85. Procedure TestVariantsmallint;
  86. Procedure TestVariantinteger;
  87. Procedure TestVariantlongword;
  88. Procedure TestVariantint64;
  89. Procedure TestVariantqword;
  90. Procedure TestVariantsingle;
  91. Procedure TestVariantdouble;
  92. Procedure TestVariantCurrency;
  93. Procedure TestVariantString;
  94. Procedure TestVariantolestr;
  95. Procedure TestVariantboolean;
  96. Procedure TestVariantDate;
  97. procedure TestVariantDate2;
  98. Procedure TestVariantArray;
  99. Procedure TestMultipleProps;
  100. Procedure TestObjectToJSONString;
  101. Procedure TestStringsToJSONString;
  102. Procedure TestCollectionToJSONString;
  103. procedure TestTListToJSONString;
  104. Procedure TestChildren;
  105. Procedure TestChildren2;
  106. Procedure TestLowercase;
  107. end;
  108. { TTestJSONDeStreamer }
  109. TTestJSONDeStreamer = class(TTestJSON)
  110. private
  111. FDS : TJSONDeStreamer;
  112. FJD : TJSONData;
  113. FToFree : TObject;
  114. procedure DeStream(JSON: TJSONStringType; AObject: TObject);
  115. procedure DeStream(JSON: TJSONObject; AObject: TObject);
  116. procedure DoDateTimeFormat;
  117. protected
  118. procedure SetUp; override;
  119. procedure TearDown; override;
  120. Procedure AssertVarType(Msg : String; AVarType : TVarType; Const Variant : Variant);
  121. Property DS : TJSONDeStreamer Read FDS;
  122. Property JD : TJSONData Read FJD Write FJD;
  123. Property Tofree : TObject Read FToFree Write FToFree;
  124. published
  125. Procedure TestVariantInteger;
  126. Procedure TestVariantFloat;
  127. Procedure TestVariantInt64;
  128. Procedure TestVariantBoolean;
  129. Procedure TestVariantNull;
  130. Procedure TestVariantString;
  131. Procedure TestVariantArray;
  132. procedure TestEmpty;
  133. procedure TestBoolean;
  134. procedure TestInteger;
  135. procedure TestIntegerCaseInsensitive;
  136. procedure TestIntegerCaseSensitive;
  137. procedure TestString;
  138. procedure TestFloat;
  139. procedure TestFloat2;
  140. procedure TestFloat3;
  141. procedure TestFloat4;
  142. procedure TestFloat5;
  143. procedure TestDateTime;
  144. procedure TestDateTimeFormat;
  145. procedure TestEnum1;
  146. procedure TestEnum2;
  147. Procedure TestSet1;
  148. Procedure TestSet2;
  149. Procedure TestSet3;
  150. Procedure TestSet4;
  151. Procedure TestVariantProp;
  152. procedure TestCollection;
  153. procedure TestCollection2;
  154. procedure TestCollection3;
  155. procedure TestCollection4;
  156. procedure TestCollection5;
  157. procedure TestCollection6;
  158. procedure TestCollectionProp;
  159. procedure TestCollectionProp2;
  160. procedure TestStrings;
  161. procedure TestStrings2;
  162. procedure TestStrings3;
  163. end;
  164. implementation
  165. uses variants;
  166. { TTestJSONDeStreamer }
  167. procedure TTestJSONDeStreamer.SetUp;
  168. begin
  169. inherited SetUp;
  170. FDS:=TJSONDeStreamer.Create(Nil)
  171. end;
  172. procedure TTestJSONDeStreamer.TearDown;
  173. begin
  174. FreeAndNil(FDS);
  175. FreeAndNil(FJD);
  176. FreeAndNil(FToFree);
  177. inherited TearDown;
  178. end;
  179. procedure TTestJSONDeStreamer.AssertVarType(Msg: String; AVarType: TVarType;
  180. const Variant: Variant);
  181. begin
  182. AssertEquals(Msg,VarTypeAsText(AVarType),VarTypeAsText(VarType(Variant)));
  183. end;
  184. procedure TTestJSONDeStreamer.TestVariantInteger;
  185. Var
  186. V : Variant;
  187. begin
  188. JD:=TJSOnIntegerNumber.Create(12);
  189. V:=DS.JSONToVariant(JD);
  190. AssertVarType('Integer data',varInteger,V);
  191. AssertEquals('Integer value',12,V);
  192. end;
  193. procedure TTestJSONDeStreamer.TestVariantFloat;
  194. Var
  195. V : Variant;
  196. begin
  197. JD:=TJSOnFloatNumber.Create(1.2);
  198. V:=DS.JSONToVariant(JD);
  199. AssertVarType('Double data',varDouble,V);
  200. AssertEquals('Float value',1.2,V);
  201. end;
  202. procedure TTestJSONDeStreamer.TestVariantInt64;
  203. Var
  204. V : Variant;
  205. begin
  206. JD:=TJSONInt64Number.Create(123);
  207. V:=DS.JSONToVariant(JD);
  208. AssertVarType('Int64 data',varInt64,V);
  209. AssertEquals('Int64 value',123,V);
  210. end;
  211. procedure TTestJSONDeStreamer.TestVariantBoolean;
  212. Var
  213. V : Variant;
  214. begin
  215. JD:=TJSONBoolean.Create(True);
  216. V:=DS.JSONToVariant(JD);
  217. AssertVarType('Boolean data',varBoolean,V);
  218. AssertEquals('Boolean value',True,V);
  219. end;
  220. procedure TTestJSONDeStreamer.TestVariantNull;
  221. Var
  222. V : Variant;
  223. begin
  224. JD:=TJSONNull.Create();
  225. V:=DS.JSONToVariant(JD);
  226. AssertVarType('Null data',varNull,V);
  227. end;
  228. procedure TTestJSONDeStreamer.TestVariantString;
  229. Var
  230. V : Variant;
  231. begin
  232. JD:=TJSONString.Create('A string');
  233. V:=DS.JSONToVariant(JD);
  234. AssertVarType('String data',varOleStr,V);
  235. AssertEquals('String data','A string',V);
  236. end;
  237. procedure TTestJSONDeStreamer.TestVariantArray;
  238. Var
  239. V : Variant;
  240. begin
  241. JD:=TJSONArray.Create([1,2,3]);
  242. V:=DS.JSONToVariant(JD);
  243. AssertEQuals('Variant is array',true,VarIsArray(V));
  244. AssertEquals('Lower bound is zero ',0,VarArrayLowBound(V,1));
  245. AssertEquals('Higher bound is count-1 ',2,VarArrayHighBound(V,1));
  246. AssertEquals('Element 0 value correct ',1,V[0]);
  247. AssertEquals('Element 1 value correct ',2,V[1]);
  248. AssertEquals('Element 2 value correct ',3,V[2]);
  249. end;
  250. procedure TTestJSONDeStreamer.TestEmpty;
  251. begin
  252. FTofree:=TComponent.Create(Nil);
  253. DS.JSONToObject('{}',FTofree);
  254. AssertEquals('Empty name','',TComponent(FToFree).Name);
  255. AssertEquals('Empty Tag',0,TComponent(FToFree).Tag);
  256. end;
  257. procedure TTestJSONDeStreamer.DeStream(JSON : TJSONStringType; AObject : TObject);
  258. begin
  259. FToFree:=AObject;
  260. DS.JSONToObject(JSON,FTofree);
  261. end;
  262. procedure TTestJSONDeStreamer.DeStream(JSON: TJSONObject; AObject: TObject);
  263. begin
  264. FToFree:=AObject;
  265. JD:=JSON;
  266. DS.JSONToObject(JSON,FTofree);
  267. end;
  268. procedure TTestJSONDeStreamer.TestBoolean;
  269. Var
  270. B : TBooleanComponent;
  271. begin
  272. B:=TBooleanComponent.Create(Nil);
  273. DeStream('{ "BooleanProp" : true }',B);
  274. AssertEquals('Correct boolean value',true,B.BooleanProp);
  275. end;
  276. procedure TTestJSONDeStreamer.TestInteger;
  277. Var
  278. B : TIntegerComponent;
  279. begin
  280. B:=TIntegerComponent.Create(Nil);
  281. DeStream('{ "IntProp" : 22 }',B);
  282. AssertEquals('Correct integer value',22,B.IntProp);
  283. end;
  284. procedure TTestJSONDeStreamer.TestIntegerCaseInsensitive;
  285. Var
  286. B : TIntegerComponent;
  287. begin
  288. DS.Options:=DS.Options+[jdoCaseInsensitive];
  289. B:=TIntegerComponent.Create(Nil);
  290. DeStream('{ "intprop" : 22 }',B);
  291. AssertEquals('Correct integer value',22,B.IntProp);
  292. end;
  293. procedure TTestJSONDeStreamer.TestIntegerCaseSensitive;
  294. Var
  295. B : TIntegerComponent;
  296. begin
  297. DS.Options:=DS.Options;
  298. B:=TIntegerComponent.Create(Nil);
  299. B.IntProp:=0;
  300. DeStream('{ "intprop" : 22 }',B);
  301. AssertEquals('Correct integer value not reas',0,B.IntProp);
  302. end;
  303. procedure TTestJSONDeStreamer.TestString;
  304. Var
  305. B : TStringComponent;
  306. begin
  307. B:=TStringComponent.Create(Nil);
  308. DeStream('{ "StringProp" : "A nice string"}',B);
  309. AssertEquals('Correct string value','A nice string',B.StringProp);
  310. end;
  311. procedure TTestJSONDeStreamer.TestFloat;
  312. Var
  313. B : TSingleComponent;
  314. begin
  315. B:=TSingleComponent.Create(Nil);
  316. DeStream('{ "SingleProp" : 2.34 }',B);
  317. AssertEquals('Correct single value',2.34,B.SingleProp);
  318. end;
  319. procedure TTestJSONDeStreamer.TestFloat2;
  320. Var
  321. B : TDoubleComponent;
  322. begin
  323. B:=TDoubleComponent.Create(Nil);
  324. DeStream('{ "DoubleProp" : 3.45 }',B);
  325. AssertEquals('Correct Double value',3.45,B.DoubleProp);
  326. end;
  327. procedure TTestJSONDeStreamer.TestFloat3;
  328. Var
  329. B : TExtendedComponent;
  330. begin
  331. B:=TExtendedComponent.Create(Nil);
  332. DeStream('{ "ExtendedProp" : 4.56 }',B);
  333. AssertEquals('Correct extended value',4.56,B.ExtendedProp);
  334. end;
  335. procedure TTestJSONDeStreamer.TestFloat4;
  336. Var
  337. B : TCompComponent;
  338. begin
  339. B:=TCompComponent.Create(Nil);
  340. DeStream('{ "CompProp" : 5.67 }',B);
  341. AssertEquals('Correct comp value',round(5.67),B.CompProp);
  342. end;
  343. procedure TTestJSONDeStreamer.TestFloat5;
  344. Var
  345. B : TCurrencyComponent;
  346. begin
  347. B:=TCurrencyComponent.Create(Nil);
  348. DeStream('{ "CurrencyProp" : 5.67 }',B);
  349. AssertEquals('Correct string value',5.67,B.CurrencyProp);
  350. end;
  351. procedure TTestJSONDeStreamer.TestDateTime;
  352. Var
  353. E : TDateTimeComponent;
  354. D : TDateTime;
  355. begin
  356. E:=TDateTimeComponent.Create(Nil);
  357. D:= RecodeMillisecond(Now,0);
  358. DeStream('{"DateTimeProp" : "'+FormatDateTime(RFC3339DateTimeFormat,D)+'"}',E);
  359. AssertEquals('Correct value',D,E.DateTimeProp);
  360. end;
  361. procedure TTestJSONDeStreamer.DoDateTimeFormat;
  362. begin
  363. DeStream('{"DateTimeProp" : "'+DateTimeToStr(RecodeMillisecond(Now,0))+'"}',FToFree);
  364. end;
  365. procedure TTestJSONDeStreamer.TestDateTimeFormat;
  366. Const
  367. ISO8601 = 'yyyymmdd"T"hhnnss';
  368. Var
  369. E : TDateTimeComponent;
  370. D : TDateTime;
  371. begin
  372. E:=TDateTimeComponent.Create(Nil);
  373. D:=RecodeMillisecond(Now,0);
  374. DS.DateTimeFormat:=ISO8601;
  375. DeStream('{"DateTimeProp" : "'+FormatDateTime(Iso8601,D)+'"}',E);
  376. AssertEquals('Correct value',D,E.DateTimeProp);
  377. AssertException('Error if string does not correspond to specified format',EConvertError,@DoDateTimeFormat);
  378. end;
  379. procedure TTestJSONDeStreamer.TestEnum1;
  380. Var
  381. E : TEnumcomponent;
  382. begin
  383. E:=TEnumComponent.Create(Nil);
  384. DeStream('{ "Dice" : 2 }',E);
  385. AssertEquals('Correct value',2,Ord(E.Dice));
  386. end;
  387. procedure TTestJSONDeStreamer.TestEnum2;
  388. Var
  389. E : TEnumcomponent;
  390. begin
  391. E:=TEnumComponent.Create(Nil);
  392. DeStream('{ "Dice" : "three" }',E);
  393. AssertEquals('Correct value',GetEnumName(TypeInfo(TDice),Ord(Three)),GetEnumName(TypeInfo(TDice),Ord(E.Dice)));
  394. end;
  395. procedure TTestJSONDeStreamer.TestSet1;
  396. Var
  397. T : TSetComponent;
  398. begin
  399. T:=TSetComponent.Create(Nil);
  400. DeStream('{ "Throw" : "one,two" }',T);
  401. If not (T.Throw=[one,two]) then
  402. Fail('Correct value for throw');
  403. end;
  404. procedure TTestJSONDeStreamer.TestSet2;
  405. Var
  406. T : TSetComponent;
  407. begin
  408. T:=TSetComponent.Create(Nil);
  409. DeStream('{ "Throw" : "[one,two]" }',T);
  410. If not (T.Throw=[one,two]) then
  411. Fail('Correct value for throw');
  412. end;
  413. procedure TTestJSONDeStreamer.TestSet3;
  414. Var
  415. T : TSetComponent;
  416. begin
  417. T:=TSetComponent.Create(Nil);
  418. DeStream('{ "Throw" : [ "one", "two"] }',T);
  419. If not (T.Throw=[one,two]) then
  420. Fail('Correct value for throw');
  421. end;
  422. procedure TTestJSONDeStreamer.TestSet4;
  423. Var
  424. T : TSetComponent;
  425. begin
  426. T:=TSetComponent.Create(Nil);
  427. DeStream('{ "Throw" : [ 0 , 1 ] }',T);
  428. If not (T.Throw=[one,two]) then
  429. Fail('Correct value for throw');
  430. end;
  431. procedure TTestJSONDeStreamer.TestVariantProp;
  432. Var
  433. V : TVariantComponent;
  434. begin
  435. V:=TVariantComponent.Create(Nil);
  436. DeStream('{ "VariantProp" : "A string" }',V);
  437. AssertEquals('Variant property value','A string',V.VariantProp);
  438. end;
  439. procedure TTestJSONDeStreamer.TestCollection;
  440. Var
  441. C : TTestCollection;
  442. begin
  443. C:=TTestCollection.Create;
  444. DeStream('[ { "StrProp" : "one" }, { "StrProp" : "two" } ]',C);
  445. AssertEquals('Item count',2,C.Count);
  446. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  447. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  448. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  449. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  450. end;
  451. procedure TTestJSONDeStreamer.TestCollection2;
  452. Var
  453. C : TTestCollection;
  454. begin
  455. C:=TTestCollection.Create;
  456. DeStream('{ "Items" : [ { "StrProp" : "one" }, { "StrProp" : "two" } ] }',C);
  457. AssertEquals('Item count',2,C.Count);
  458. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  459. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  460. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  461. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  462. end;
  463. procedure TTestJSONDeStreamer.TestCollection3;
  464. Var
  465. C : TTestCollection;
  466. begin
  467. C:=TTestCollection.Create;
  468. FTofree:=C;
  469. DS.JSONToCollection('{ "Items" : [ { "StrProp" : "one" }, { "StrProp" : "two" } ] }',C);
  470. AssertEquals('Item count',2,C.Count);
  471. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  472. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  473. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  474. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  475. end;
  476. procedure TTestJSONDeStreamer.TestCollection4;
  477. Var
  478. C : TTestCollection;
  479. begin
  480. C:=TTestCollection.Create;
  481. FTofree:=C;
  482. DS.JSONToCollection('[ { "StrProp" : "one" }, { "StrProp" : "two" } ]',C);
  483. AssertEquals('Item count',2,C.Count);
  484. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  485. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  486. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  487. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  488. end;
  489. procedure TTestJSONDeStreamer.TestCollection5;
  490. Var
  491. C : TTestCollection;
  492. begin
  493. C:=TTestCollection.Create;
  494. FTofree:=C;
  495. JD:=TJSONArray.Create([TJSONObject.Create(['StrProp','one']),TJSONObject.Create(['StrProp','two'])]);
  496. DS.JSONToCollection(JD,C);
  497. AssertEquals('Item count',2,C.Count);
  498. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  499. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  500. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  501. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  502. end;
  503. procedure TTestJSONDeStreamer.TestCollection6;
  504. Var
  505. C : TTestCollection;
  506. begin
  507. C:=TTestCollection.Create;
  508. FTofree:=C;
  509. JD:=TJSONObject.Create(['Items',TJSONArray.Create([TJSONObject.Create(['StrProp','one']),TJSONObject.Create(['StrProp','two'])])]);
  510. DS.JSONToCollection(JD,C);
  511. AssertEquals('Item count',2,C.Count);
  512. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  513. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  514. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  515. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  516. end;
  517. procedure TTestJSONDeStreamer.TestCollectionProp;
  518. Var
  519. C : TCollection;
  520. begin
  521. JD:=TJSONObject.Create(['Coll',TJSONArray.Create([TJSONObject.Create(['StrProp','one']),TJSONObject.Create(['StrProp','two'])])]);
  522. DeStream(JD as TJSONObject,TCollectionComponent.Create(Nil));
  523. C:=TCollectionComponent(ToFree).Coll;
  524. AssertEquals('Item count',2,C.Count);
  525. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  526. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  527. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  528. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  529. end;
  530. procedure TTestJSONDeStreamer.TestCollectionProp2;
  531. Var
  532. C : TCollection;
  533. begin
  534. DeStream('{ "Coll" : [ { "StrProp" : "one" }, { "StrProp" : "two" } ]}',TCollectionComponent.Create(Nil));
  535. C:=TCollectionComponent(ToFree).Coll;
  536. AssertEquals('Item count',2,C.Count);
  537. AssertEquals('Class item 0',TTestItem,C.Items[0].ClassType);
  538. AssertEquals('Class item 1',TTestItem,C.Items[1].ClassType);
  539. AssertEquals('Class item 0','one',TTestItem(C.Items[0]).StrProp);
  540. AssertEquals('Class item 1','two',TTestItem(C.Items[1]).StrProp);
  541. end;
  542. procedure TTestJSONDeStreamer.TestStrings;
  543. Var
  544. S : TStrings;
  545. begin
  546. S:=TStringList.Create;
  547. FTofree:=S;
  548. DS.JSONToStrings('[ "one" , "two" ]',S);
  549. AssertEquals('Item count',2,S.Count);
  550. AssertEquals('First item','one',S[0]);
  551. AssertEquals('First item','two',S[1]);
  552. end;
  553. procedure TTestJSONDeStreamer.TestStrings2;
  554. Var
  555. S : TStrings;
  556. begin
  557. S:=TStringList.Create;
  558. FTofree:=S;
  559. DS.JSONToStrings('{ "Strings" : [ "one" , "two" ] }',S);
  560. AssertEquals('Item count',2,S.Count);
  561. AssertEquals('First item','one',S[0]);
  562. AssertEquals('First item','two',S[1]);
  563. end;
  564. procedure TTestJSONDeStreamer.TestStrings3;
  565. Var
  566. S : TStrings;
  567. begin
  568. S:=TStringList.Create;
  569. FTofree:=S;
  570. DS.JSONToStrings('{ "Strings" : [ "one" , "two" ] }',S);
  571. AssertEquals('Item count',2,S.Count);
  572. AssertEquals('First item','one',S[0]);
  573. AssertEquals('First item','two',S[1]);
  574. end;
  575. { TTestJSONStreamer }
  576. function TTestJSONStreamer.StreamObject(AObject: TObject): TJSONObject;
  577. begin
  578. FToFree:=AObject;
  579. FSR:=FRJ.ObjectToJSON(AObject);
  580. Result:=FSR;
  581. end;
  582. procedure TTestJSONStreamer.DoStreamProperty1(Sender: TObject; AObject: TObject;
  583. Info: PPropInfo; var Res: TJSONData);
  584. begin
  585. If (info^.name<>'IntProp') and (info^.name<>'Name') and (info^.name<>'Tag') then
  586. Fail('Wrong property');
  587. If (info^.name='IntProp') then
  588. FreeAndNil(Res);
  589. FCalled:=true;
  590. end;
  591. procedure TTestJSONStreamer.SetUp;
  592. begin
  593. Inherited;
  594. FRJ:=TJSONStreamer.Create(Nil);
  595. end;
  596. procedure TTestJSONStreamer.TearDown;
  597. begin
  598. FreeAndNil(FSR);
  599. FreeAndNil(FRJ);
  600. FreeAndNil(FToFree);
  601. Inherited;
  602. end;
  603. procedure TTestJSONStreamer.AssertEquals(AMessage: String; Expected, Actual: TJSONType);
  604. begin
  605. AssertEquals(AMessage,GetEnumName(TypeInfo(TJSONType),Ord(Expected)),
  606. GetEnumName(TypeInfo(TJSONType),Ord(Actual)));
  607. end;
  608. procedure TTestJSONStreamer.AssertPropCount(ACount: Integer);
  609. begin
  610. AssertNotNull('Result of streaming available',FSR);
  611. If FToFree is TComponent then
  612. ACount:=ACount+2; // Tag + Name
  613. Writeln(FSR.ASJSON);
  614. AssertEquals('Property count correct',ACount,FSR.Count);
  615. end;
  616. function TTestJSONStreamer.AssertProperty(APropName: String; AType: TJSONType
  617. ): TJSONData;
  618. Var
  619. i : Integer;
  620. begin
  621. I:=FSR.IndexOfName(APropName);
  622. If (I=-1) then
  623. Fail('No property "'+APropName+'" available');
  624. Result:=FSR.Items[i];
  625. AssertEquals('Property "'+APropName+'" has correct type',GetEnumName(TypeInfo(TJSONType),Ord(AType)),
  626. GetEnumName(TypeInfo(TJSONType),Ord(Result.JSONType)));
  627. end;
  628. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: Boolean);
  629. begin
  630. AssertNotNull('Result of streaming available',FSR);
  631. AssertEquals('Result of streaming is TJSONObject',TJSONObject,FSR.ClassType);
  632. AssertEquals('Correct value',AValue,AssertProperty(APropName,jtBoolean).AsBoolean);
  633. end;
  634. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: Integer);
  635. begin
  636. AssertNotNull('Result of streaming available',FSR);
  637. AssertEquals('Result of streaming is TJSONObject',TJSONObject,FSR.ClassType);
  638. AssertEquals('Correct value',AValue,AssertProperty(APropName,jtNumber).AsInteger);
  639. end;
  640. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: String);
  641. begin
  642. AssertNotNull('Result of streaming available',FSR);
  643. AssertEquals('Result of streaming is TJSONObject',TJSONObject,FSR.ClassType);
  644. AssertEquals('Correct value',AValue,AssertProperty(APropName,jtString).AsString);
  645. end;
  646. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: TJSONFloat);
  647. begin
  648. AssertNotNull('Result of streaming available',FSR);
  649. AssertEquals('Result of streaming is TJSONObject',TJSONObject,FSR.ClassType);
  650. AssertEquals('Correct value',AValue,AssertProperty(APropName,jtNumber).AsFloat);
  651. end;
  652. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: array of String
  653. );
  654. Var
  655. a : TJSONArray;
  656. i : integer;
  657. begin
  658. a:=AssertArrayProp(APropName);
  659. AssertEquals('Correct count ',Length(AValue),A.Count);
  660. For I:=Low(AValue) to High(Avalue) do
  661. begin
  662. AssertEquals('Array element type',jtString,A.Types[i]);
  663. AssertEquals('Array value',AValue[i],A.strings[i]);
  664. end;
  665. end;
  666. procedure TTestJSONStreamer.AssertProp(APropName: String; AValue: array of Integer
  667. );
  668. Var
  669. a : TJSONArray;
  670. i : integer;
  671. begin
  672. a:=AssertArrayProp(APropName);
  673. For I:=Low(AValue) to High(Avalue) do
  674. begin
  675. AssertEquals('Array element type',jtNumber,A.Types[i]);
  676. AssertEquals('Array value',AValue[i],A.Integers[i]);
  677. end;
  678. end;
  679. function TTestJSONStreamer.CreateVariantComp: TVariantComponent;
  680. begin
  681. Result:=TVariantComponent.Create(Nil);
  682. FTofree:=Result;
  683. end;
  684. procedure TTestJSONStreamer.AssertNullProp(APropName: String);
  685. begin
  686. AssertProperty(APropName,jtNull);
  687. end;
  688. function TTestJSONStreamer.AssertObjectProp(APropName: String): TJSONObject;
  689. begin
  690. Result:=AssertProperty(APropName,jtObject) as TJSONObject;
  691. end;
  692. function TTestJSONStreamer.AssertArrayProp(APropName: String): TJSONArray;
  693. begin
  694. Result:=AssertProperty(APropName,jtArray) as TJSONArray;
  695. end;
  696. procedure TTestJSONStreamer.TestNil;
  697. begin
  698. AssertNull('Nil returns nil',StreamObject(Nil));
  699. end;
  700. procedure TTestJSONStreamer.TestEmpty;
  701. begin
  702. StreamObject(TemptyPersistent.Create);
  703. AssertPropCount(0);
  704. end;
  705. procedure TTestJSONStreamer.TestEmptyComponent;
  706. begin
  707. StreamObject(TComponent.Create(nil));
  708. AssertPropCount(0);
  709. end;
  710. procedure TTestJSONStreamer.TestWriteBoolean;
  711. begin
  712. StreamObject(TBooleanComponent.Create(nil));
  713. AssertPropCount(1);
  714. AssertProp('BooleanProp',False);
  715. end;
  716. procedure TTestJSONStreamer.TestWriteInteger;
  717. begin
  718. StreamObject(TIntegerComponent.Create(Nil));
  719. AssertPropCount(1);
  720. AssertProp('IntProp',3);
  721. end;
  722. procedure TTestJSONStreamer.TestWriteString;
  723. begin
  724. StreamObject(TStringComponent.Create(Nil));
  725. AssertPropCount(1);
  726. AssertProp('StringProp','A string');
  727. end;
  728. procedure TTestJSONStreamer.TestWriteFloat;
  729. begin
  730. StreamObject(TSingleComponent.Create(Nil));
  731. AssertPropCount(1);
  732. AssertProp('SingleProp',1.23);
  733. end;
  734. procedure TTestJSONStreamer.TestWriteFloat2;
  735. begin
  736. StreamObject(TDoubleComponent.Create(Nil));
  737. AssertPropCount(1);
  738. AssertProp('DoubleProp',2.34);
  739. end;
  740. procedure TTestJSONStreamer.TestWriteFloat3;
  741. begin
  742. StreamObject(TExtendedComponent.Create(Nil));
  743. AssertPropCount(1);
  744. AssertProp('ExtendedProp',3.45);
  745. end;
  746. procedure TTestJSONStreamer.TestWriteFloat4;
  747. begin
  748. StreamObject(TCompComponent.Create(Nil));
  749. AssertPropCount(1);
  750. AssertProp('CompProp',5);
  751. end;
  752. procedure TTestJSONStreamer.TestWriteFloat5;
  753. begin
  754. StreamObject(TCurrencyComponent.Create(Nil));
  755. AssertPropCount(1);
  756. AssertProp('CurrencyProp',5.67);
  757. end;
  758. procedure TTestJSONStreamer.TestEnum1;
  759. begin
  760. StreamObject(TEnumComponent3.Create(Nil));
  761. AssertPropCount(1);
  762. AssertProp('Dice',GetEnumName(TypeInfo(TDice),Ord(three)));
  763. end;
  764. procedure TTestJSONStreamer.TestEnum2;
  765. begin
  766. RJ.Options:=[jsoEnumeratedAsInteger];
  767. StreamObject(TEnumComponent3.Create(Nil));
  768. AssertProp('Dice',Ord(three));
  769. end;
  770. procedure TTestJSONStreamer.TestSet1;
  771. begin
  772. StreamObject(TSetComponent.Create(Nil));
  773. AssertPropCount(1);
  774. AssertProp('Throw',['two','five']);
  775. end;
  776. procedure TTestJSONStreamer.TestSet2;
  777. begin
  778. RJ.Options:=[jsoSetAsString];
  779. StreamObject(TSetComponent.Create(Nil));
  780. AssertPropCount(1);
  781. AssertProp('Throw','two,five');
  782. end;
  783. procedure TTestJSONStreamer.TestSet3;
  784. begin
  785. RJ.Options:=[jsoSetAsString,jsoSetBrackets];
  786. StreamObject(TSetComponent.Create(Nil));
  787. AssertPropCount(1);
  788. AssertProp('Throw','[two,five]');
  789. end;
  790. procedure TTestJSONStreamer.TestSet4;
  791. begin
  792. RJ.Options:=[jsoSetEnumeratedAsInteger];
  793. StreamObject(TSetComponent.Create(Nil));
  794. AssertPropCount(1);
  795. AssertProp('Throw',[Ord(two),Ord(five)]);
  796. end;
  797. procedure TTestJSONStreamer.TestObjectNil;
  798. Var
  799. P : TOwnedComponent;
  800. begin
  801. P:=TOwnedComponent.Create(Nil);
  802. P.CompProp.Free;
  803. P.CompProp:=Nil;
  804. StreamObject(P);
  805. AssertPropCount(1);
  806. AssertNullProp('CompProp');
  807. end;
  808. procedure TTestJSONStreamer.TestComponentProp1;
  809. begin
  810. StreamObject(TOwnedComponent.Create(Nil));
  811. AssertPropCount(1);
  812. AssertProp('CompProp','SubComponent');
  813. end;
  814. procedure TTestJSONStreamer.TestComponentProp2;
  815. Var
  816. C : TOwnedComponent;
  817. F : TJSONObject;
  818. begin
  819. RJ.Options:=[jsoComponentsInline];
  820. C:=TOwnedComponent.Create(Nil);
  821. StreamObject(C);
  822. AssertPropCount(1);
  823. F:=SR;
  824. try
  825. SR:=AssertObjectProp('CompProp');
  826. AssertPropCount(1);
  827. AssertProp('Name','SubComponent');
  828. Assertprop('Tag',0);
  829. AssertProp('IntProp',3);
  830. finally
  831. SR:=F;
  832. end;
  833. end;
  834. procedure TTestJSONStreamer.TestCollectionProp1;
  835. Var
  836. C : TCollectionComponent;
  837. F : TJSONObject;
  838. A : TJSONArray;
  839. begin
  840. C:=TCollectionComponent2.Create(Nil);
  841. StreamObject(C);
  842. AssertPropCount(1);
  843. F:=SR;
  844. try
  845. A:=AssertArrayProp('Coll');
  846. AssertEquals('Collection item cound',3,A.Count);
  847. AssertEquals('Item 0 is object',jtObject,A.Types[0]);
  848. SR:=A.Objects[0];
  849. FToFree:=SR;
  850. AssertPropCount(1);
  851. AssertProp('StrProp','First');
  852. AssertEquals('Item 1 is object',jtObject,A.Types[1]);
  853. SR:=A.Objects[1];
  854. FToFree:=SR;
  855. AssertPropCount(1);
  856. AssertProp('StrProp','Second');
  857. AssertEquals('Item 2 is object',jtObject,A.Types[2]);
  858. SR:=A.Objects[2];
  859. FToFree:=SR;
  860. AssertPropCount(1);
  861. AssertProp('StrProp','Third');
  862. finally
  863. SR:=F;
  864. FToFree:=C;
  865. end;
  866. end;
  867. procedure TTestJSONStreamer.TestCollectionProp2;
  868. Var
  869. C : TCollectionComponent;
  870. A : TJSONArray;
  871. begin
  872. C:=TCollectionComponent.Create(Nil);
  873. StreamObject(C);
  874. AssertPropCount (1);
  875. A:=AssertArrayProp('Coll');
  876. AssertEquals('Collection item count',0,A.Count);
  877. end;
  878. procedure TTestJSONStreamer.TestPersistentProp1;
  879. var
  880. P : TPersistentComponent;
  881. F : TJSONObject;
  882. begin
  883. P:=TPersistentComponent.Create(Nil);
  884. StreamObject(P);
  885. AssertPropCount(1);
  886. F:=SR;
  887. try
  888. SR:=AssertObjectProp('Persist');
  889. FToFree:=P.Persist;
  890. AssertPropCount(2);
  891. AssertProp('AString','A persistent string');
  892. AssertProp('AInteger',3);
  893. finally
  894. FToFree:=P;
  895. SR:=F;
  896. end;
  897. end;
  898. procedure TTestJSONStreamer.TestStringsProp1;
  899. begin
  900. RJ.Options:=[jsoTstringsAsArray];
  901. StreamObject(TStringsCOmponent.Create(Nil));
  902. AssertPropCount(1);
  903. AssertProp('StringsProp',['One','Two','Three']);
  904. end;
  905. procedure TTestJSONStreamer.TestStringsProp2;
  906. begin
  907. StreamObject(TStringsCOmponent.Create(Nil));
  908. AssertPropCount(1);
  909. AssertProp('StringsProp','One'+sLineBreak+'Two'+sLineBreak+'Three'+sLineBreak);
  910. end;
  911. procedure TTestJSONStreamer.TestStringsProp3;
  912. Var
  913. O : TJSONObject;
  914. S : TStringsComponent;
  915. begin
  916. S:=TStringsCOmponent.Create(Nil);
  917. RJ.Options:=[jsoTstringsAsObject];
  918. StreamObject(S);
  919. AssertPropCount(1);
  920. O:=SR;
  921. SR:=AssertObjectprop('StringsProp');
  922. FTofree:=Nil;
  923. try
  924. AssertNullProp('One');
  925. AssertNullProp('Two');
  926. AssertNullProp('Three');
  927. finally
  928. SR:=o;
  929. FToFree:=S;
  930. end;
  931. end;
  932. procedure TTestJSONStreamer.TestStringsProp4;
  933. Var
  934. O,SP : TJSONObject;
  935. S : TStringsComponent;
  936. begin
  937. S:=TStringsCOmponent.Create(Nil);
  938. S.StringsProp.Objects[0]:=TEmptyPersistent.Create;
  939. S.StringsProp.Objects[1]:=TEmptyPersistent.Create;
  940. S.StringsProp.Objects[2]:=TEmptyPersistent.Create;
  941. try
  942. RJ.Options:=[jsoTstringsAsObject];
  943. StreamObject(S);
  944. AssertPropCount(1);
  945. O:=SR;
  946. SP:=AssertObjectprop('StringsProp');
  947. SR:=SP;
  948. FTofree:=Nil;
  949. try
  950. SR:=AssertObjectProp('One');
  951. AssertPropCount(0);
  952. SR:=SP;
  953. SR:=AssertObjectProp('Two');
  954. AssertPropCount(0);
  955. SR:=SP;
  956. SR:=AssertObjectProp('Three');
  957. AssertPropCount(0);
  958. finally
  959. SR:=o;
  960. FToFree:=S;
  961. end;
  962. finally
  963. S.StringsProp.Objects[0].Free;
  964. S.StringsProp.Objects[1].Free;
  965. S.StringsProp.Objects[2].Free;
  966. end;
  967. end;
  968. procedure TTestJSONStreamer.TestStringsArray;
  969. Var
  970. O : TJSONArray;
  971. S : TStringList;
  972. begin
  973. S:=TStringList.create;
  974. try
  975. S.Add('one');
  976. O:=RJ.StreamTStringsArray(S);
  977. try
  978. AssertEquals('one element',1,O.Count);
  979. AssertEquals('string type',jtString,O.Types[0]);
  980. AssertEquals('string value','one',O.Strings[0]);
  981. finally
  982. FreeAndNil(O);
  983. end;
  984. finally
  985. S.Free;
  986. end;
  987. end;
  988. procedure TTestJSONStreamer.TestStringsObject;
  989. Var
  990. O : TJSONObject;
  991. S : TStringList;
  992. begin
  993. S:=TStringList.create;
  994. try
  995. S.AddObject('one',TEmptyPersistent.Create);
  996. O:=RJ.StreamTStringsObject(S);
  997. try
  998. AssertEquals('one element',1,O.Count);
  999. AssertEquals('Have property',0,O.IndexOfName('one'));
  1000. AssertEquals('string type',jtObject,O.Types['one']);
  1001. AssertEquals('string value','one',O.Names[0]);
  1002. finally
  1003. FreeAndNil(O);
  1004. end;
  1005. finally
  1006. S.Objects[0].FRee;
  1007. S.Free;
  1008. end;
  1009. end;
  1010. procedure TTestJSONStreamer.TestStringsStream1;
  1011. Var
  1012. D: TJSONData;
  1013. O : TJSONArray;
  1014. S : TStringList;
  1015. begin
  1016. S:=TStringList.create;
  1017. try
  1018. S.Add('one');
  1019. RJ.Options:=[jsoTstringsAsArray];
  1020. D:=RJ.StreamTStrings(S);
  1021. try
  1022. AssertEquals('Correct type',jtArray,D.JSONType);
  1023. O:=D as TJSONArray;
  1024. AssertEquals('one element',1,O.Count);
  1025. AssertEquals('string type',jtString,O.Types[0]);
  1026. AssertEquals('string value','one',O.Strings[0]);
  1027. finally
  1028. FreeAndNil(O);
  1029. end;
  1030. finally
  1031. S.Free;
  1032. end;
  1033. end;
  1034. procedure TTestJSONStreamer.TestStringsStream2;
  1035. Var
  1036. D : TJSONData;
  1037. O : TJSONObject;
  1038. S : TStringList;
  1039. begin
  1040. S:=TStringList.create;
  1041. try
  1042. S.AddObject('one',TEmptyPersistent.Create);
  1043. RJ.Options:=[jsoTstringsAsObject];
  1044. D:=RJ.StreamTstrings(S);
  1045. try
  1046. AssertEquals('Correct type',jtObject,D.JSONType);
  1047. O:=D as TJSONObject;
  1048. AssertEquals('one element',1,O.Count);
  1049. AssertEquals('Have property',0,O.IndexOfName('one'));
  1050. AssertEquals('string type',jtObject,O.Types['one']);
  1051. AssertEquals('string value','one',O.Names[0]);
  1052. finally
  1053. SR:=O;
  1054. end;
  1055. finally
  1056. S.Objects[0].FRee;
  1057. S.Free;
  1058. end;
  1059. end;
  1060. procedure TTestJSONStreamer.TestStringsStream3;
  1061. Var
  1062. O : TJSONObject;
  1063. S : TStringList;
  1064. begin
  1065. S:=TStringList.create;
  1066. try
  1067. S.AddObject('one',TEmptyPersistent.Create);
  1068. RJ.Options:=[jsoTstringsAsObject];
  1069. SR:=RJ.ObjectToJSON(S);
  1070. O:=AssertObjectProp('Strings');
  1071. AssertEquals('one element',1,O.Count);
  1072. AssertEquals('Have property',0,O.IndexOfName('one'));
  1073. AssertEquals('string type',jtObject,O.Types['one']);
  1074. AssertEquals('string value','one',O.Names[0]);
  1075. finally
  1076. S.Objects[0].FRee;
  1077. S.Free;
  1078. end;
  1079. end;
  1080. procedure TTestJSONStreamer.TestStringsStream4;
  1081. Var
  1082. S : TStringList;
  1083. begin
  1084. S:=TStringList.create;
  1085. try
  1086. S.AddObject('one',TEmptyPersistent.Create);
  1087. SR:=RJ.ObjectToJSON(S);
  1088. AssertProp('Strings','one'+sLinebreak);
  1089. finally
  1090. S.Objects[0].FRee;
  1091. S.Free;
  1092. end;
  1093. end;
  1094. procedure TTestJSONStreamer.TestStringsStream5;
  1095. Var
  1096. D : TJSONData;
  1097. S : TStringList;
  1098. begin
  1099. S:=TStringList.create;
  1100. try
  1101. S.AddObject('one',TEmptyPersistent.Create);
  1102. D:=RJ.StreamTstrings(S);
  1103. try
  1104. AssertEquals('String data',jtString,D.JSONType);
  1105. AssertEquals('String value','one'+sLineBreak,D.AsString);
  1106. finally
  1107. D.free;
  1108. end;
  1109. finally
  1110. S.Objects[0].FRee;
  1111. S.Free;
  1112. end;
  1113. end;
  1114. procedure TTestJSONStreamer.TestCollectionStream;
  1115. Var
  1116. C : TTestCollection;
  1117. A : TJSONArray;
  1118. begin
  1119. C:=TTestCollection.Create;
  1120. FToFree:=C;
  1121. TTestItem(C.Add).StrProp:='One';
  1122. TTestItem(C.Add).StrProp:='Two';
  1123. A:=RJ.StreamCollection(C);
  1124. try
  1125. AssertNotNull('Have result',A);
  1126. AssertEquals('2 items',2,A.Count);
  1127. AssertEquals('Type item 0,',jtObject,A.Types[0]);
  1128. AssertEquals('Type item 1,',jtObject,A.Types[1]);
  1129. SR:=A.Objects[0];
  1130. AssertPropCount(1);
  1131. AssertProp('StrProp','One');
  1132. SR:=A.Objects[1];
  1133. AssertPropCount(1);
  1134. AssertProp('StrProp','Two');
  1135. SR:=Nil;
  1136. finally
  1137. FreeAndNil(A);
  1138. end;
  1139. end;
  1140. procedure TTestJSONStreamer.TestCollectionStream2;
  1141. Var
  1142. C : TTestCollection;
  1143. A : TJSONArray;
  1144. o : TJSONObject;
  1145. begin
  1146. C:=TTestCollection.Create;
  1147. TTestItem(C.Add).StrProp:='One';
  1148. TTestItem(C.Add).StrProp:='Two';
  1149. FToFree:=C;
  1150. StreamObject(C);
  1151. O:=SR;
  1152. try
  1153. A:=AssertProperty('Items',jtArray) as TJSONArray;
  1154. AssertNotNull('Have result',A);
  1155. AssertEquals('2 items',2,A.Count);
  1156. AssertEquals('Type item 0,',jtObject,A.Types[0]);
  1157. AssertEquals('Type item 1,',jtObject,A.Types[1]);
  1158. SR:=A.Objects[0];
  1159. AssertPropCount(1);
  1160. AssertProp('StrProp','One');
  1161. SR:=A.Objects[1];
  1162. AssertPropCount(1);
  1163. AssertProp('StrProp','Two');
  1164. SR:=Nil;
  1165. finally
  1166. SR:=O;
  1167. end;
  1168. end;
  1169. procedure TTestJSONStreamer.TestOnStreamProperty;
  1170. begin
  1171. RJ.OnStreamProperty:=@DoStreamProperty1;
  1172. StreamObject(TIntegerComponent.Create(Nil));
  1173. AssertPropCount(0);
  1174. end;
  1175. procedure TTestJSONStreamer.TestDateTimeProp;
  1176. Var
  1177. D : Double;
  1178. begin
  1179. StreamObject(TDateTimeComponent.Create(Nil));
  1180. D:=EncodeDate(1996,8,1);
  1181. AssertPropCount(1);
  1182. AssertProp('DateTimeProp',D);
  1183. end;
  1184. procedure TTestJSONStreamer.TestDateTimePropDefaultString;
  1185. Var
  1186. D : Double;
  1187. begin
  1188. RJ.Options:=[jsoDateTimeAsString];
  1189. StreamObject(TDateTimeComponent.Create(Nil));
  1190. D:=EncodeDate(1996,8,1);
  1191. AssertPropCount(1);
  1192. AssertProp('DateTimeProp',FormatDateTime(RFC3339DateTimeFormat,D));
  1193. end;
  1194. procedure TTestJSONStreamer.TestDateTimePropDefaultStringTime;
  1195. Var
  1196. D : Double;
  1197. begin
  1198. RJ.Options:=[jsoDateTimeAsString];
  1199. StreamObject(TDateTimeComponent3.Create(Nil));
  1200. D:=EncodeDate(1996,8,1)+EncodeTime(23,20,0,0);
  1201. AssertPropCount(1);
  1202. AssertProp('DateTimeProp',FormatDateTime(RFC3339DateTimeFormat,D));
  1203. end;
  1204. procedure TTestJSONStreamer.TestDateTimeProp2;
  1205. Var
  1206. D : Double;
  1207. begin
  1208. StreamObject(TDateTimeComponent2.Create(Nil));
  1209. D:=EncodeTime(23,20,0,0);
  1210. AssertPropCount(1);
  1211. AssertProp('DateTimeProp',D);
  1212. end;
  1213. procedure TTestJSONStreamer.TestDateTimeProp3;
  1214. Var
  1215. D : Double;
  1216. begin
  1217. StreamObject(TDateTimeComponent3.Create(Nil));
  1218. D:=EncodeDate(1996,8,1)+EncodeTime(23,20,0,0);
  1219. AssertPropCount(1);
  1220. AssertProp('DateTimeProp',D);
  1221. end;
  1222. procedure TTestJSONStreamer.TestDateTimeProp4;
  1223. begin
  1224. RJ.Options:=[jsoDateTimeAsString,jsoLegacyDateTime];
  1225. StreamObject(TDateTimeComponent.Create(Nil));
  1226. AssertPropCount(1);
  1227. AssertProp('DateTimeProp',DateToStr(EncodeDate(1996,8,1)));
  1228. end;
  1229. procedure TTestJSONStreamer.TestDateTimeProp5;
  1230. begin
  1231. RJ.Options:=[jsoDateTimeAsString,jsoLegacyDateTime];
  1232. StreamObject(TDateTimeComponent2.Create(Nil));
  1233. AssertPropCount(1);
  1234. AssertProp('DateTimeProp',TimeToStr(EncodeTime(23,20,0,0)));
  1235. end;
  1236. procedure TTestJSONStreamer.TestDateTimeProp6;
  1237. begin
  1238. RJ.Options:=[jsoDateTimeAsString,jsoLegacyDateTime];
  1239. StreamObject(TDateTimeComponent3.Create(Nil));
  1240. AssertPropCount(1);
  1241. AssertProp('DateTimeProp',DateTimeToStr(EncodeDate(1996,8,1)+EncodeTime(23,20,0,0)));
  1242. end;
  1243. procedure TTestJSONStreamer.TestDateTimeProp7;
  1244. begin
  1245. RJ.Options:=[jsoDateTimeAsString];
  1246. RJ.DateTimeFormat:='hh:nn';
  1247. StreamObject(TDateTimeComponent3.Create(Nil));
  1248. AssertPropCount(1);
  1249. AssertProp('DateTimeProp',FormatDateTime('hh:nn',EncodeDate(1996,8,1)+EncodeTime(23,20,0,0)));
  1250. end;
  1251. procedure TTestJSONStreamer.TestVariantShortint;
  1252. Var
  1253. i : ShortInt;
  1254. C : TVariantComponent;
  1255. begin
  1256. i:=3;
  1257. C:=CreateVariantComp;
  1258. C.VariantProp:=i;
  1259. AssertEquals('Variant type',VarTypeAsText(varshortint),VarTypeAsText(VarType(C.VariantProp)));
  1260. StreamObject(FTofree);
  1261. AssertPropCount(1);
  1262. AssertProp('VariantProp',3);
  1263. end;
  1264. procedure TTestJSONStreamer.TestVariantbyte;
  1265. Var
  1266. i : Byte;
  1267. C : TVariantComponent;
  1268. begin
  1269. i:=3;
  1270. C:=CreateVariantComp;
  1271. C.VariantProp:=i;
  1272. AssertEquals('Variant type',VarTypeAsText(varByte),VarTypeAsText(VarType(C.VariantProp)));
  1273. StreamObject(FTofree);
  1274. AssertPropCount(1);
  1275. AssertProp('VariantProp',3);
  1276. end;
  1277. procedure TTestJSONStreamer.TestVariantword;
  1278. Var
  1279. i : Word;
  1280. C : TVariantComponent;
  1281. begin
  1282. i:=3;
  1283. C:=CreateVariantComp;
  1284. C.VariantProp:=i;
  1285. AssertEquals('Variant type',VarTypeAsText(varWord),VarTypeAsText(VarType(C.VariantProp)));
  1286. StreamObject(FTofree);
  1287. AssertPropCount(1);
  1288. AssertProp('VariantProp',3);
  1289. end;
  1290. procedure TTestJSONStreamer.TestVariantsmallint;
  1291. Var
  1292. i : Smallint;
  1293. C : TVariantComponent;
  1294. begin
  1295. i:=3;
  1296. C:=CreateVariantComp;
  1297. C.VariantProp:=i;
  1298. AssertEquals('Variant type',VarTypeAsText(varSmallint),VarTypeAsText(VarType(C.VariantProp)));
  1299. StreamObject(FTofree);
  1300. AssertPropCount(1);
  1301. AssertProp('VariantProp',3);
  1302. end;
  1303. procedure TTestJSONStreamer.TestVariantinteger;
  1304. Var
  1305. i : Integer;
  1306. C : TVariantComponent;
  1307. begin
  1308. i:=3;
  1309. C:=CreateVariantComp;
  1310. C.VariantProp:=i;
  1311. AssertEquals('Variant type',VarTypeAsText(varInteger),VarTypeAsText(VarType(C.VariantProp)));
  1312. StreamObject(FTofree);
  1313. AssertPropCount(1);
  1314. AssertProp('VariantProp',3);
  1315. end;
  1316. procedure TTestJSONStreamer.TestVariantlongword;
  1317. Var
  1318. i : Cardinal;
  1319. C : TVariantComponent;
  1320. begin
  1321. i:=3;
  1322. C:=CreateVariantComp;
  1323. C.VariantProp:=i;
  1324. AssertEquals('Variant type',VarTypeAsText(varLongword),VarTypeAsText(VarType(C.VariantProp)));
  1325. StreamObject(FTofree);
  1326. AssertPropCount(1);
  1327. AssertProp('VariantProp',3);
  1328. end;
  1329. procedure TTestJSONStreamer.TestVariantint64;
  1330. Var
  1331. i : Int64;
  1332. C : TVariantComponent;
  1333. begin
  1334. i:=3;
  1335. C:=CreateVariantComp;
  1336. C.VariantProp:=i;
  1337. AssertEquals('Variant type',VarTypeAsText(varInt64),VarTypeAsText(VarType(C.VariantProp)));
  1338. StreamObject(FTofree);
  1339. AssertPropCount(1);
  1340. AssertProp('VariantProp',3);
  1341. end;
  1342. procedure TTestJSONStreamer.TestVariantqword;
  1343. Var
  1344. i : QWord;
  1345. C : TVariantComponent;
  1346. begin
  1347. i:=3;
  1348. C:=CreateVariantComp;
  1349. C.VariantProp:=i;
  1350. AssertEquals('Variant type',VarTypeAsText(varQWord),VarTypeAsText(VarType(C.VariantProp)));
  1351. StreamObject(FTofree);
  1352. AssertPropCount(1);
  1353. AssertProp('VariantProp',3);
  1354. end;
  1355. procedure TTestJSONStreamer.TestVariantsingle;
  1356. Var
  1357. i : Single;
  1358. C : TVariantComponent;
  1359. begin
  1360. i:=3.14;
  1361. C:=CreateVariantComp;
  1362. C.VariantProp:=VarAsType(3.14,varSingle);
  1363. AssertEquals('Variant type',VarTypeAsText(varSingle),VarTypeAsText(VarType(C.VariantProp)));
  1364. StreamObject(FTofree);
  1365. AssertPropCount(1);
  1366. AssertProp('VariantProp',i);
  1367. end;
  1368. procedure TTestJSONStreamer.TestVariantdouble;
  1369. Var
  1370. i : Double;
  1371. C : TVariantComponent;
  1372. begin
  1373. i:=3.14;
  1374. C:=CreateVariantComp;
  1375. C.VariantProp:=i;
  1376. AssertEquals('Variant type',VarTypeAsText(varDouble),VarTypeAsText(VarType(C.VariantProp)));
  1377. StreamObject(FTofree);
  1378. AssertPropCount(1);
  1379. AssertProp('VariantProp',3.14);
  1380. end;
  1381. procedure TTestJSONStreamer.TestVariantCurrency;
  1382. Var
  1383. i : Currency;
  1384. C : TVariantComponent;
  1385. begin
  1386. i:=3.14;
  1387. C:=CreateVariantComp;
  1388. C.VariantProp:=i;
  1389. AssertEquals('Variant type',VarTypeAsText(varCurrency),VarTypeAsText(VarType(C.VariantProp)));
  1390. StreamObject(FTofree);
  1391. AssertPropCount(1);
  1392. AssertProp('VariantProp',3.14);
  1393. end;
  1394. procedure TTestJSONStreamer.TestVariantString;
  1395. Var
  1396. i : String;
  1397. C : TVariantComponent;
  1398. begin
  1399. i:='3.14';
  1400. C:=CreateVariantComp;
  1401. C.VariantProp:=i;
  1402. AssertEquals('Variant type',VarTypeAsText(varString),VarTypeAsText(VarType(C.VariantProp)));
  1403. StreamObject(FTofree);
  1404. AssertPropCount(1);
  1405. AssertProp('VariantProp','3.14');
  1406. end;
  1407. procedure TTestJSONStreamer.TestVariantolestr;
  1408. Var
  1409. i : String;
  1410. C : TVariantComponent;
  1411. begin
  1412. i:='3.14';
  1413. C:=CreateVariantComp;
  1414. C.VariantProp:=VarAsType(i,varOleStr);
  1415. AssertEquals('Variant type',VarTypeAsText(varOleStr),VarTypeAsText(VarType(C.VariantProp)));
  1416. StreamObject(FTofree);
  1417. AssertPropCount(1);
  1418. AssertProp('VariantProp','3.14');
  1419. end;
  1420. procedure TTestJSONStreamer.TestVariantboolean;
  1421. Var
  1422. i : Boolean;
  1423. C : TVariantComponent;
  1424. begin
  1425. i:=True;
  1426. C:=CreateVariantComp;
  1427. C.VariantProp:=i;
  1428. AssertEquals('Variant type',VarTypeAsText(varBoolean),VarTypeAsText(VarType(C.VariantProp)));
  1429. StreamObject(FTofree);
  1430. AssertPropCount(1);
  1431. AssertProp('VariantProp',True);
  1432. end;
  1433. procedure TTestJSONStreamer.TestVariantDate;
  1434. Var
  1435. i : TDateTime;
  1436. C : TVariantComponent;
  1437. begin
  1438. i:=EncodeDate(2010,12,23);
  1439. C:=CreateVariantComp;
  1440. C.VariantProp:=i;
  1441. AssertEquals('Variant type',VarTypeAsText(varDate),VarTypeAsText(VarType(C.VariantProp)));
  1442. StreamObject(FTofree);
  1443. AssertPropCount(1);
  1444. AssertProp('VariantProp',EncodeDate(2010,12,23));
  1445. end;
  1446. procedure TTestJSONStreamer.TestVariantDate2;
  1447. Var
  1448. i : TDateTime;
  1449. C : TVariantComponent;
  1450. begin
  1451. RJ.Options:=[jsoDateTimeAsString,jsoLegacyDateTime];
  1452. i:=EncodeDate(2010,12,23);
  1453. C:=CreateVariantComp;
  1454. C.VariantProp:=i;
  1455. AssertEquals('Variant type',VarTypeAsText(varDate),VarTypeAsText(VarType(C.VariantProp)));
  1456. StreamObject(FTofree);
  1457. AssertPropCount(1);
  1458. AssertProp('VariantProp',DateToStr(EncodeDate(2010,12,23)));
  1459. end;
  1460. procedure TTestJSONStreamer.TestVariantArray;
  1461. Var
  1462. i : Integer;
  1463. V : Variant;
  1464. C : TVariantComponent;
  1465. A : TJSONArray;
  1466. begin
  1467. V:=VarArrayCreate([1,10],varInteger);
  1468. For I:=1 to 10 do
  1469. V[i]:=11-I;
  1470. C:=CreateVariantComp;
  1471. C.VariantProp:=V;
  1472. StreamObject(FTofree);
  1473. AssertPropCount(1);
  1474. A:=AssertProperty('VariantProp',jtArray) as TJSONArray;
  1475. AssertEquals('10 elements in array',10,A.Count);
  1476. For I:=1 to 10 do
  1477. begin
  1478. assertEquals('Type of element',jtNumber,A.Types[i-1]);
  1479. AssertEquals('Value of element',11-i,A.Integers[i-1]);
  1480. end;
  1481. end;
  1482. procedure TTestJSONStreamer.TestMultipleProps;
  1483. begin
  1484. StreamObject(TMultipleComponent.Create(Nil));
  1485. AssertPropCount(5);
  1486. AssertProp('IntProp',1);
  1487. Assertprop('StringProp','A String');
  1488. AssertProp('CurrencyProp',2.3);
  1489. AssertProp('Throw',['three','four']);
  1490. AssertProp('Dice','two');
  1491. end;
  1492. procedure TTestJSONStreamer.TestObjectToJSONString;
  1493. begin
  1494. StreamObject(TIntegerComponent.Create(Nil));
  1495. AssertEquals('Correct stream',SR.AsJSON,RJ.ObjectToJSONString(FToFree));
  1496. end;
  1497. procedure TTestJSONStreamer.TestStringsToJSONString;
  1498. Var
  1499. S : TStrings;
  1500. begin
  1501. S:=TStringList.Create;
  1502. try
  1503. S.Add('one');
  1504. S.Add('two');
  1505. S.Add('three');
  1506. AssertEquals('StringsToJSONString','["one", "two", "three"]',RJ.StringsToJSON(S));
  1507. AssertEquals('StringsToJSONString','{ "one" : null, "two" : null, "three" : null }',RJ.StringsToJSON(S,True));
  1508. finally
  1509. FreeAndNil(S);
  1510. end;
  1511. end;
  1512. procedure TTestJSONStreamer.TestTListToJSONString ;
  1513. Var
  1514. C : TList;
  1515. D : TJSONData;
  1516. P : Pointer;
  1517. Function Add : TTestItem;
  1518. begin
  1519. Result:=TTestItem.Create(Nil);
  1520. C.Add(Result);
  1521. end;
  1522. begin
  1523. RJ.Options:=RJ.Options + [jsoStreamTList];
  1524. C:=TList.Create;
  1525. try
  1526. Add.StrProp:='one';
  1527. Add.StrProp:='two';
  1528. Add.StrProp:='three';
  1529. D:=RJ.StreamTList(C);
  1530. AssertEquals('StreamTlist','[{ "StrProp" : "one" }, { "StrProp" : "two" }, { "StrProp" : "three" }]',D.AsJSON);
  1531. finally
  1532. D.Free;
  1533. For P in C do
  1534. TObject(P).Free;
  1535. FreeAndNil(C);
  1536. end;
  1537. end;
  1538. procedure TTestJSONStreamer.TestCollectionToJSONString;
  1539. Var
  1540. C : TTestCollection;
  1541. begin
  1542. C:=TTestCollection.Create;
  1543. try
  1544. (C.Add as TTestItem).StrProp:='one';
  1545. (C.Add as TTestItem).StrProp:='two';
  1546. (C.Add as TTestItem).StrProp:='three';
  1547. AssertEquals('CollectionToJSON','[{ "StrProp" : "one" }, { "StrProp" : "two" }, { "StrProp" : "three" }]',RJ.CollectionToJSON(C));
  1548. finally
  1549. FreeAndNil(C);
  1550. end;
  1551. end;
  1552. procedure TTestJSONStreamer.TestChildren;
  1553. Var
  1554. C : TChildrenComponent;
  1555. begin
  1556. C:=TChildrenComponent.Create(Nil);
  1557. TComponent.Create(C).Name:='Child1';
  1558. TComponent.Create(C).Name:='Child2';
  1559. StreamObject(C);
  1560. If SR.IndexOfName('Children')<>-1 then
  1561. Fail('Children streamed with default options');
  1562. end;
  1563. procedure TTestJSONStreamer.TestChildren2;
  1564. Var
  1565. C : TChildrenComponent;
  1566. A : TJSONArray;
  1567. O : TJSONObject;
  1568. begin
  1569. C:=TChildrenComponent.Create(Nil);
  1570. TComponent.Create(C).Name:='Child1';
  1571. TComponent.Create(C).Name:='Child2';
  1572. RJ.Options:=[jsoStreamChildren];
  1573. StreamObject(C);
  1574. AssertPropCount(1);
  1575. A:=AssertProperty('Children',jtArray) as TJSONArray;
  1576. O:=SR;
  1577. try
  1578. AssertEquals('2 Elements in array',2,A.Count);
  1579. AssertEquals('First in array is object',jtObject,A.Types[0]);
  1580. AssertEquals('Second in array is object',jtObject,A.Types[1]);
  1581. SR:=A.Objects[0];
  1582. AssertProp('Name','Child1');
  1583. SR:=A.Objects[1];
  1584. AssertProp('Name','Child2');
  1585. finally
  1586. SR:=O;
  1587. end;
  1588. end;
  1589. procedure TTestJSONStreamer.TestLowercase;
  1590. begin
  1591. RJ.Options:=RJ.Options+[jsoLowerPropertyNames];
  1592. StreamObject(TBooleanComponent.Create(nil));
  1593. AssertPropCount(1);
  1594. AssertProp('booleanprop',False);
  1595. end;
  1596. initialization
  1597. RegisterTests([TTestJSONStreamer,TTestJSONDeStreamer]);
  1598. end.