testjsonrtti.pp 42 KB

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