testjsonrtti.pp 44 KB

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