testjsonrtti.pp 44 KB

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