testjsonrtti.pas 46 KB

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