demortti.pp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. program demortti;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, fpjson, fpjsonrtti, variants;
  5. {$R *.res}
  6. Var
  7. JS : TJSONStreamer;
  8. Type
  9. { TTestItem }
  10. TTestItem = Class(TCollectionItem)
  11. private
  12. FStrProp: String;
  13. Published
  14. Property StrProp : String Read FStrProp Write FStrProp;
  15. end;
  16. { TCollComponent }
  17. TCollComponent = Class(TComponent)
  18. private
  19. FCollProp: TCollection;
  20. Published
  21. Property CollProp : TCollection Read FCollProp Write FCollProp;
  22. end;
  23. { TCompComponent }
  24. TCompComponent = Class(TComponent)
  25. private
  26. FCompProp: TComponent;
  27. Published
  28. Property CompProp : TComponent Read FCompProp Write FCompProp;
  29. end;
  30. TDice = (one,two,three,four,five,six);
  31. TThrow = Set of TDice;
  32. { TEnumComponent }
  33. TEnumComponent = Class(TComponent)
  34. private
  35. FDice: TDice;
  36. Published
  37. Property Dice : TDice Read FDice Write FDice;
  38. end;
  39. { TSetComponent }
  40. TSetComponent = Class(TComponent)
  41. private
  42. FThrow: TThrow;
  43. Published
  44. Property Throw : TThrow Read FThrow Write FThrow;
  45. end;
  46. { TChildComponent }
  47. TChildComponent = Class(TComponent)
  48. Protected
  49. procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  50. end;
  51. { TChildComponent }
  52. procedure TChildComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
  53. Var
  54. I : Integer;
  55. begin
  56. Writeln('Children',ComponentCount);
  57. For I:=0 to ComponentCount-1 do
  58. Proc(Components[i]);
  59. end;
  60. Procedure DumpObject(const Header : String; var O : TJSONData);
  61. begin
  62. Writeln(Header,' : ');
  63. Writeln(O.FormatJSON());
  64. writeln();
  65. FreeAndNil(O);
  66. JS.Options:=[];
  67. end;
  68. Procedure DemoObject;
  69. Var
  70. C : TComponent;
  71. O : TJSONData;
  72. begin
  73. C:=TComponent.Create(Nil);
  74. try
  75. C.Name:='DemoComponent';
  76. C.Tag:=23;
  77. O:=JS.ObjectToJSON(C);
  78. DumpObject('Complete component',O);
  79. finally
  80. FreeAndNil(C);
  81. end;
  82. end;
  83. Procedure DemoComponentObject;
  84. Var
  85. C : TCompComponent;
  86. O : TJSONData;
  87. begin
  88. C:=TCompComponent.Create(Nil);
  89. try
  90. C.Name:='DemoComponent';
  91. C.Tag:=23;
  92. C.CompProp:=TCompComponent.Create(C);
  93. C.CompProp.Name:='SubComponent';
  94. C.CompProp.Tag:=45;
  95. O:=JS.ObjectToJSON(C);
  96. DumpObject('Component-valued property',O);
  97. TCompComponent(C.CompProp).FComponentStyle:=[csSubComponent];
  98. O:=JS.ObjectToJSON(C);
  99. DumpObject('Component-valued property, csSubComponent in Componentstyle',O);
  100. TCompComponent(C.CompProp).FComponentStyle:=[];
  101. JS.options:=[jsoComponentsInline];
  102. O:=JS.ObjectToJSON(C);
  103. DumpObject('Component-valued property, options:=[jsoComponentsInline] ',O);
  104. finally
  105. FreeAndNil(C);
  106. end;
  107. end;
  108. Procedure DemoChildObject;
  109. Var
  110. C : TChildComponent;
  111. O : TJSONData;
  112. begin
  113. C:=TChildComponent.Create(Nil);
  114. try
  115. C.Name:='ParentComponent';
  116. C.Tag:=23;
  117. With TComponent.Create(C) do
  118. begin
  119. Name:='Child1';
  120. Tag:=1;
  121. end;
  122. With TComponent.Create(C) do
  123. begin
  124. Name:='Child2';
  125. Tag:=2;
  126. end;
  127. O:=JS.ObjectToJSON(C);
  128. DumpObject('Component with children, default options',O);
  129. JS.Options:=[jsoStreamChildren];
  130. O:=JS.ObjectToJSON(C);
  131. DumpObject('Component with children, Options:=[jsoStreamChildren]',O);
  132. finally
  133. FreeAndNil(C);
  134. end;
  135. end;
  136. Procedure DemoEnumObject;
  137. Var
  138. C : TEnumComponent;
  139. O : TJSONData;
  140. begin
  141. C:=TEnumComponent.Create(Nil);
  142. try
  143. C.Dice:=Three;
  144. O:=JS.ObjectToJSON(C);
  145. DumpObject('Enumerated-typed property, default settings',O);
  146. JS.Options:=[jsoEnumeratedAsInteger];
  147. O:=JS.ObjectToJSON(C);
  148. DumpObject('Enumerated-typed property, Options:=[jsoEnumeratedAsInteger];',O);
  149. finally
  150. FreeAndNil(C);
  151. end;
  152. end;
  153. Procedure DemoSetObject;
  154. Var
  155. C : TSetComponent;
  156. O : TJSONData;
  157. begin
  158. C:=TSetComponent.Create(Nil);
  159. try
  160. C.Throw:=[two,five];
  161. O:=JS.ObjectToJSON(C);
  162. DumpObject('set-typed property, default settings',O);
  163. JS.Options:=[jsoSetAsString];
  164. O:=JS.ObjectToJSON(C);
  165. DumpObject('Set-typed property, Options:=[jsoSetAsString];',O);
  166. JS.Options:=[jsoSetAsString,jsoSetBrackets];
  167. O:=JS.ObjectToJSON(C);
  168. DumpObject('Set-typed property, Options:=[jsoSetAsString,jsoSetBrackets];',O);
  169. JS.Options:=[jsoSetEnumeratedAsInteger];
  170. O:=JS.ObjectToJSON(C);
  171. DumpObject('Set-typed property, Options:=[jsoSetEnumeratedAsInteger];',O);
  172. finally
  173. FreeAndNil(C);
  174. end;
  175. end;
  176. Procedure DemoObjectAsString;
  177. Var
  178. C : TComponent;
  179. begin
  180. C:=TComponent.Create(Nil);
  181. try
  182. C.Name:='DemoComponent';
  183. C.Tag:=23;
  184. Writeln('Complete component, directly as string :');
  185. Writeln(JS.ObjectToJSONString(C));
  186. JS.Options:=[jsoUseFormatString];
  187. Writeln('Complete component, directly as string (Options:=[jsoUseFormatString]):');
  188. Writeln(JS.ObjectToJSONString(C));
  189. JS.Options:=[];
  190. Writeln('');
  191. finally
  192. FreeAndNil(C);
  193. end;
  194. end;
  195. Procedure DemoStrings;
  196. Var
  197. S : TStrings;
  198. O : TJSONData;
  199. C : TComponent;
  200. begin
  201. S:=TStringList.Create;
  202. try
  203. S.Add('One');
  204. S.Add('two');
  205. S.Add('Three');
  206. O:=JS.StreamTStrings(S);
  207. DumpObject('Default TStrings',O);
  208. O:=JS.StreamTStringsArray(S);
  209. DumpObject('TStrings as array',O);
  210. C:=TComponent.Create(Nil);
  211. try
  212. C.Name:='SubComponent';
  213. C.Tag:=12;
  214. S.Objects[0]:=C;
  215. O:=JS.StreamTStringsObject(S);
  216. DumpObject('TStrings as object',O);
  217. Writeln('TStrings Directly as JSON string, array');
  218. Writeln(JS.StringsToJSON(S,False));
  219. Writeln();
  220. Writeln('TStrings Directly as JSON string, object');
  221. Writeln(JS.StringsToJSON(S,True));
  222. Writeln();
  223. O:=JS.ObjectToJSON(S);
  224. DumpObject('Passing TStrings to ObjctToJSON',O);
  225. JS.Options:=[jsoTstringsAsArray];
  226. O:=JS.ObjectToJSON(S);
  227. DumpObject('Passing TStrings to ObjctToJSON (Options:=[jsoTstringsAsArray])',O);
  228. JS.Options:=[jsoTstringsAsObject];
  229. O:=JS.ObjectToJSON(S);
  230. DumpObject('Passing TStrings to ObjctToJSON (Options:=[jsoTstringsAsObject])',O);
  231. finally
  232. FreeAndNil(C);
  233. end;
  234. finally
  235. FreeAndNil(S);
  236. end;
  237. end;
  238. Procedure DemoCollection;
  239. Var
  240. C : TCollection;
  241. CC : TCollComponent;
  242. O : TJSONData;
  243. begin
  244. C:=TCollection.Create(TTestItem);
  245. try
  246. (C.Add as TTestItem).StrProp:='One';
  247. (C.Add as TTestItem).StrProp:='Two';
  248. (C.Add as TTestItem).StrProp:='Three';
  249. CC:=TCollComponent.Create(Nil);
  250. try
  251. CC.CollProp:=C;
  252. O:=JS.ObjectToJSON(CC);
  253. DumpObject('Collection property',O);
  254. O:=JS.StreamCollection(C);
  255. DumpObject('StreamCollection result',O);
  256. O:=JS.ObjectToJSON(C);
  257. DumpObject('Passing collection to ObjectToJSON (returns an object)',O);
  258. Writeln('Direct Collection to JSON String :');
  259. Writeln(JS.CollectionToJSON(C));
  260. Writeln;
  261. finally
  262. FreeAndNil(CC);
  263. end;
  264. finally
  265. FreeAndNil(C);
  266. end;
  267. end;
  268. Procedure DemoVariant;
  269. Var
  270. V : Variant;
  271. O : TJSONData;
  272. I : integer;
  273. begin
  274. V:=3;
  275. O:=JS.StreamVariant(V);
  276. DumpObject('Simple integer variant streaming',O);
  277. V:=EncodeDate(2010,12,24);
  278. O:=JS.StreamVariant(V);
  279. DumpObject('Date variant streaming',O);
  280. JS.Options:=[jsoDateTimeAsString];
  281. O:=JS.StreamVariant(V);
  282. DumpObject('Date variant streaming (Options:=[jsoDateTimeAsString];)',O);
  283. V:=VarArrayCreate([1,10],varInteger);
  284. For I:=1 to 10 do
  285. V[i]:=11-I;
  286. O:=JS.StreamVariant(V);
  287. DumpObject('Variant arrays also work',O);
  288. Writeln('Variant to JSON string :');
  289. Writeln(JS.VariantToJSON(V));
  290. Writeln('Variant to JSON string, with formatting :');
  291. JS.Options:=[jsoUseFormatString];
  292. Writeln(JS.VariantToJSON(V));
  293. JS.Options:=[];
  294. end;
  295. begin
  296. JS:=TJSONStreamer.Create(Nil);
  297. try
  298. DemoObject;
  299. DemoObjectAsString;
  300. DemoComponentObject;
  301. DemoEnumObject;
  302. DemoSetObject;
  303. DemoStrings;
  304. DemoCollection;
  305. DemoVariant;
  306. DemoChildObject;
  307. Finally
  308. FreeAndNil(JS);
  309. end;
  310. end.