demortti.pp 7.7 KB

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