testjsonrtti.pas 46 KB

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