simpledemo.pp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. {
  2. This file is part of the Free Component Library
  3. JSON Data structures demo
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program simpledemo;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes, SysUtils, fpjson;
  15. Procedure DumpJSONData(J : TJSonData; DOEOLN : Boolean = True);
  16. Var
  17. I : Integer;
  18. begin
  19. // JSONType property determines kind of value.
  20. Case J.jsontype of
  21. jtNull : Write('Null');
  22. jtBoolean : If J.AsBoolean then
  23. Write('True')
  24. else
  25. Write('False');
  26. jtNumber : {JSONNumber has extra NumberType property
  27. which determines kind of value (int/float).}
  28. Case TJSONNumber(J).NumberType of
  29. ntInteger : Write(J.AsInteger);
  30. ntFloat : Write(J.AsFloat:10:2);
  31. end;
  32. jtString : Write('"',J.AsString,'"');
  33. jtArray : begin
  34. Write('[ ');
  35. For I:=0 to J.Count-1 do
  36. begin
  37. DumpJSONData(J.Items[I],False);
  38. If I<J.Count-1 then
  39. Write(', ');
  40. end;
  41. Write(' ]');
  42. end;
  43. jtObject : begin
  44. Write('{ ');
  45. For I:=0 to J.Count-1 do
  46. begin
  47. Writeln('"',TJSONObject(J).Names[i],'" : ');
  48. DumpJSONData(J.Items[I],False);
  49. If I<J.Count-1 then
  50. Write(',')
  51. end;
  52. Write('}');
  53. end;
  54. end;
  55. If DOEOLN then
  56. Writeln;
  57. end;
  58. Procedure EndTest(Msg : String;J : TJSOnData);
  59. begin
  60. Write(Msg, ' : ');
  61. DumpJSONData(J);
  62. FreeAndNil(J);
  63. end;
  64. Procedure DoTestCreate;
  65. begin
  66. Writeln('Constructor tests');
  67. EndTest('Null value',TJSOnNull.Create);
  68. EndTest('Boolean true',TJSONBoolean.Create(True));
  69. EndTest('Boolean false',TJSONBoolean.Create(False));
  70. EndTest('Integer value',TJSONIntegerNumber.Create(100));
  71. EndTest('Float value',TJSONFloatNumber.Create(1.2e3));
  72. EndTest('String value',TJSONString.Create('Some weird JSON string'));
  73. EndTest('Empty Array value',TJSONArray.Create);
  74. EndTest('Array value from array of const',TJSONArray.Create([1,'a',2,'b']));
  75. EndTest('Empty Object value',TJSONObject.Create);
  76. // Name, Value, name, value
  77. EndTest('Object from array of const',TJSONObject.Create(['a',1,'b',True,'C',Nil]));
  78. end;
  79. Procedure DoTestAs;
  80. Var
  81. J : TJsonData;
  82. begin
  83. Writeln('AsNNN value accessing tests, number with value 123:');
  84. J:=TJSonIntegerNumber.Create(123);
  85. Writeln('IsNull : ',J.IsNull);
  86. Writeln('AsInteger : ',J.AsInteger);
  87. Writeln('AsBoolean : ',J.AsBoolean);
  88. Writeln('AsString : ',J.AsString);
  89. Writeln('AsFloat : ',J.AsFloat:5:3);
  90. FreeAndNil(J);
  91. Writeln('Test IsNull');
  92. J:=TJSonNull.Create;
  93. Writeln('Test for null with IsNull');
  94. Writeln('IsNull : ',J.ISNull);
  95. Writeln('Test number of children :');
  96. Writeln('Count (0) : ',J.Count);
  97. FreeAndNil(J);
  98. J:=TJSONArray.Create(['a','b','c']);
  99. Writeln('Count (3): ',J.Count);
  100. FreeAndNil(J);
  101. J:=TJSONObject.Create(['a',1,'b',2]);
  102. Writeln('Count (2): ',J.Count);
  103. FreeAndNil(J);
  104. end;
  105. Procedure DoTestArray;
  106. Var
  107. J : TJSOnArray;
  108. I : Integer;
  109. begin
  110. Writeln('JSON array with elements 0,1,2,3');
  111. J:=TJSONArray.Create([0,1,2,3]);
  112. Write('Access through Elements[] (default) array property : ');
  113. For I:=0 to J.Count-1 do
  114. begin
  115. Write(J[I].AsString);
  116. If I<J.Count-1 then
  117. Write(', ');
  118. end;
  119. Writeln;
  120. Write('Access through Nulls[] array property : ');
  121. For I:=0 to J.Count-1 do
  122. begin
  123. Write(J.Nulls[I]);
  124. If I<J.Count-1 then
  125. Write(', ');
  126. end;
  127. Writeln;
  128. Write('Access through Booleans[] array property : ');
  129. For I:=0 to J.Count-1 do
  130. begin
  131. Write(J.Booleans[I]);
  132. If I<J.Count-1 then
  133. Write(', ');
  134. end;
  135. Writeln;
  136. Write('Access through Integers[] array property : ');
  137. For I:=0 to J.Count-1 do
  138. begin
  139. Write(J.Integers[I]);
  140. If I<J.Count-1 then
  141. Write(', ');
  142. end;
  143. Writeln;
  144. Write('Access through Floats[] array property : ');
  145. For I:=0 to J.Count-1 do
  146. begin
  147. Write(J.Floats[I]:5:2);
  148. If I<J.Count-1 then
  149. Write(', ');
  150. end;
  151. Writeln;
  152. Write('Access through Strings[] array property : ');
  153. For I:=0 to J.Count-1 do
  154. begin
  155. Write(J.Strings[I]);
  156. If I<J.Count-1 then
  157. Write(', ');
  158. end;
  159. Writeln;
  160. FreeAndNil(J);
  161. Writeln('Create with 3 empty TJSONObjects in array constructor');
  162. Write('Access through Objects[] array property : ');
  163. J:=TJSONArray.Create([TJSOnObject.Create,TJSOnObject.Create,TJSOnObject.Create]);
  164. For I:=0 to J.Count-1 do
  165. begin
  166. DumpJSONData(J.Objects[I],False);
  167. If I<J.Count-1 then
  168. Write(', ');
  169. end;
  170. Writeln;
  171. FreeAndNil(J);
  172. Writeln('Create with 3 empty TJSONArrays in array constructor');
  173. Write('Access through Arrays[] array property : ');
  174. J:=TJSONArray.Create([TJSOnArray.Create,TJSOnArray.Create,TJSOnArray.Create]);
  175. For I:=0 to J.Count-1 do
  176. begin
  177. DumpJSONData(J.Arrays[I],False);
  178. If I<J.Count-1 then
  179. Write(', ');
  180. end;
  181. Writeln;
  182. FreeAndNil(J);
  183. Writeln('Create empty array. Add elements with overloaded Add() method');
  184. J:=TJSONArray.Create;
  185. J.Add; // Null
  186. J.Add(True);
  187. J.Add(False);
  188. J.Add(123);
  189. J.Add(2.34);
  190. J.Add('A string');
  191. J.Add(TJSOnArray.Create);
  192. J.Add(TJSOnObject.Create);
  193. DumpJSONData(J);
  194. FreeAndNil(J);
  195. end;
  196. Procedure DoTestObject;
  197. Var
  198. J : TJSONObject;
  199. I : Char;
  200. k : Integer;
  201. begin
  202. Writeln('JSON object with elements a=0,b=1,c=2,d=3');
  203. J:=TJSONObject.Create(['a',0,'b',1,'c',2,'d',3]);
  204. Write('Get element names with Names[] array property : ');
  205. For K:=1 to J.Count-1 do
  206. begin
  207. Write(J.Names[k]);
  208. If K<J.Count-1 then
  209. Write(', ');
  210. end;
  211. Writeln;
  212. Write('Access through Elements[] (default) array property : ');
  213. For I:='a' to 'd' do
  214. begin
  215. Write(i,' : ',J[I].AsString);
  216. If I<'d' then
  217. Write(', ');
  218. end;
  219. Writeln;
  220. Write('Access through Nulls[] array property : ');
  221. For I:='a' to 'd' do
  222. begin
  223. Write(i,' : ',J.Nulls[I]);
  224. If I<'d' then
  225. Write(', ');
  226. end;
  227. Writeln;
  228. Write('Access through Booleans[] array property : ');
  229. For I:='a' to 'd' do
  230. begin
  231. Write(i,' : ',J.Booleans[I]);
  232. If I<'d' then
  233. Write(', ');
  234. end;
  235. Writeln;
  236. Write('Access through Integers[] array property : ');
  237. For I:='a' to 'd' do
  238. begin
  239. Write(i,' : ',J.Integers[I]);
  240. If I<'d' then
  241. Write(', ');
  242. end;
  243. Writeln;
  244. Write('Access through Floats[] array property : ');
  245. For I:='a' to 'd' do
  246. begin
  247. Write(i,' : ',J.Floats[I]:5:2);
  248. If I<'d' then
  249. Write(', ');
  250. end;
  251. Writeln;
  252. Write('Access through Strings[] array property : ');
  253. For I:='a' to 'd' do
  254. begin
  255. Write(i,' : ',J.Strings[I]);
  256. If I<'d' then
  257. Write(', ');
  258. end;
  259. Writeln;
  260. FreeAndNil(J);
  261. Writeln('Create with 3 empty TJSONObjects in array constructor');
  262. Write('Access through Objects[] array property : ');
  263. J:=TJSONObject.Create(['a',TJSOnObject.Create,'b',TJSOnObject.Create,'c',TJSOnObject.Create]);
  264. For I:='a' to 'c' do
  265. begin
  266. Write(i,' : ');
  267. DumpJSONData(J.Objects[i],False);
  268. If I<'c' then
  269. Write(', ');
  270. end;
  271. Writeln;
  272. FreeAndNil(J);
  273. Writeln('Create with 3 empty TJSONArrays in array constructor');
  274. Write('Access through Arrays[] array property : ');
  275. J:=TJSONObject.Create(['a',TJSONArray.Create,'b',TJSONArray.Create,'c',TJSONArray.Create]);
  276. For I:='a' to 'c' do
  277. begin
  278. Write(i,' : ');
  279. DumpJSONData(J.Arrays[I],False);
  280. If I<'c' then
  281. Write(', ');
  282. end;
  283. Writeln;
  284. FreeAndNil(J);
  285. Writeln('Create empty object. Add elements with overloaded Add() method');
  286. J:=TJSONObject.Create;
  287. J.Add('a'); // Null
  288. J.Add('b',True);
  289. J.Add('c',False);
  290. J.Add('d',123);
  291. J.Add('e',2.34);
  292. J.Add('f','A string');
  293. J.Add('g',TJSONArray.Create);
  294. J.Add('h',TJSOnObject.Create);
  295. DumpJSONData(J);
  296. FreeAndNil(J);
  297. end;
  298. begin
  299. DoTestCreate;
  300. DoTestAs;
  301. DoTestArray;
  302. DoTestObject;
  303. end.