summaryrefslogtreecommitdiff
path: root/packages/fcl-json/demo/simpledemo.pp
blob: 5f53173474cbe75ce609bf631cc1ff17a9de88fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
    This file is part of the Free Component Library

    JSON Data structures demo
    Copyright (c) 2007 by Michael Van Canneyt michael@freepascal.org

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
program simpledemo;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, fpjson;

Procedure DumpJSONData(J : TJSonData; DOEOLN : Boolean = True);

Var
  I : Integer;

begin
  // JSONType property determines kind of value.
  Case J.jsontype of
    jtNull   : Write('Null');
    jtBoolean : If J.AsBoolean then
                  Write('True')
                else
                  Write('False');
    jtNumber : {JSONNumber has extra NumberType property
                which determines kind of value (int/float).}
               Case TJSONNumber(J).NumberType of
                 ntInteger : Write(J.AsInteger);
                 ntFloat   : Write(J.AsFloat:10:2);
               end;
    jtString : Write('"',J.AsString,'"');
    jtArray  : begin
               Write('[ ');
               For I:=0 to J.Count-1 do
                 begin
                 DumpJSONData(J.Items[I],False);
                 If I<J.Count-1 then
                   Write(', ');
                 end;
               Write(' ]');
               end;
    jtObject : begin
               Write('{ ');
               For I:=0 to J.Count-1 do
                 begin
                 Writeln('"',TJSONObject(J).Names[i],'" : ');
                 DumpJSONData(J.Items[I],False);
                 If I<J.Count-1 then
                   Write(',')
                 end;
               Write('}');
               end;
   end;
   If DOEOLN then
     Writeln;
end;


Procedure EndTest(Msg : String;J : TJSOnData);

begin
  Write(Msg, ' : ');
  DumpJSONData(J);
  FreeAndNil(J);
end;

Procedure DoTestCreate;


begin
  Writeln('Constructor tests');
  EndTest('Null value',TJSOnNull.Create);
  EndTest('Boolean true',TJSONBoolean.Create(True));
  EndTest('Boolean false',TJSONBoolean.Create(False));
  EndTest('Integer value',TJSONIntegerNumber.Create(100));
  EndTest('Float value',TJSONFloatNumber.Create(1.2e3));
  EndTest('String value',TJSONString.Create('Some weird JSON string'));
  EndTest('Empty Array value',TJSONArray.Create);
  EndTest('Array value from array of const',TJSONArray.Create([1,'a',2,'b']));
  EndTest('Empty Object value',TJSONObject.Create);
  // Name, Value, name, value
  EndTest('Object from array of const',TJSONArray.Create(['a',1,'b',True,'C',Nil]));

end;

Procedure DoTestAs;

Var
  J : TJsonData;

begin
  Writeln('AsNNN value accessing tests, number with value 123:');
  J:=TJSonIntegerNumber.Create(123);
  Writeln('IsNull    : ',J.IsNull);
  Writeln('AsInteger : ',J.AsInteger);
  Writeln('AsBoolean : ',J.AsBoolean);
  Writeln('AsString  : ',J.AsString);
  Writeln('AsFloat   : ',J.AsFloat:5:3);
  FreeAndNil(J);
  Writeln('Test IsNull');
  J:=TJSonNull.Create;
  Writeln('Test for null with IsNull');
  Writeln('IsNull : ',J.ISNull);
  Writeln('Test number of children :');
  Writeln('Count (0) : ',J.Count);
  FreeAndNil(J);
  J:=TJSONArray.Create(['a','b','c']);
  Writeln('Count (3): ',J.Count);
  FreeAndNil(J);
  J:=TJSONObject.Create(['a',1,'b',2]);
  Writeln('Count (2): ',J.Count);
  FreeAndNil(J);
end;

Procedure DoTestArray;

Var
  J : TJSOnArray;
  I : Integer;

begin
  Writeln('JSON array with elements 0,1,2,3');
  J:=TJSONArray.Create([0,1,2,3]);
  Write('Access through Elements[] (default) array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J[I].AsString);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Nulls[] array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J.Nulls[I]);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Booleans[] array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J.Booleans[I]);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Integers[] array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J.Integers[I]);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Floats[] array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J.Floats[I]:5:2);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Strings[] array property : ');
  For I:=0 to J.Count-1 do
    begin
    Write(J.Strings[I]);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create with 3 empty TJSONObjects in array constructor');
  Write('Access through Objects[] array property : ');
  J:=TJSONArray.Create([TJSOnObject.Create,TJSOnObject.Create,TJSOnObject.Create]);
  For I:=0 to J.Count-1 do
    begin
    DumpJSONData(J.Objects[I],False);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create with 3 empty TJSONArrays in array constructor');
  Write('Access through Arrays[] array property : ');
  J:=TJSONArray.Create([TJSOnArray.Create,TJSOnArray.Create,TJSOnArray.Create]);
  For I:=0 to J.Count-1 do
    begin
    DumpJSONData(J.Arrays[I],False);
    If I<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create empty array. Add elements with overloaded Add() method');
  J:=TJSONArray.Create;
  J.Add; // Null
  J.Add(True);
  J.Add(False);
  J.Add(123);
  J.Add(2.34);
  J.Add('A string');
  J.Add(TJSOnArray.Create);
  J.Add(TJSOnObject.Create);
  DumpJSONData(J);
  FreeAndNil(J);
end;

Procedure DoTestObject;

Var
  J : TJSONObject;
  I : Char;
  k : Integer;
  
begin
  Writeln('JSON object with elements a=0,b=1,c=2,d=3');
  J:=TJSONObject.Create(['a',0,'b',1,'c',2,'d',3]);
  Write('Get element names with Names[] array property : ');
  For K:=1 to J.Count-1 do
    begin
    Write(J.Names[k]);
    If K<J.Count-1 then
      Write(', ');
    end;
  Writeln;
  Write('Access through Elements[] (default) array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J[I].AsString);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  Write('Access through Nulls[] array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J.Nulls[I]);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  Write('Access through Booleans[] array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J.Booleans[I]);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  Write('Access through Integers[] array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J.Integers[I]);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  Write('Access through Floats[] array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J.Floats[I]:5:2);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  Write('Access through Strings[] array property : ');
  For I:='a' to 'd' do
    begin
    Write(i,' : ',J.Strings[I]);
    If I<'d' then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create with 3 empty TJSONObjects in array constructor');
  Write('Access through Objects[] array property : ');
  J:=TJSONObject.Create(['a',TJSOnObject.Create,'b',TJSOnObject.Create,'c',TJSOnObject.Create]);
  For I:='a' to 'c' do
    begin
    Write(i,' : ');
    DumpJSONData(J.Objects[i],False);
    If I<'c' then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create with 3 empty TJSONArrays in array constructor');
  Write('Access through Arrays[] array property : ');
  J:=TJSONObject.Create(['a',TJSONArray.Create,'b',TJSONArray.Create,'c',TJSONArray.Create]);
  For I:='a' to 'c' do
    begin
    Write(i,' : ');
    DumpJSONData(J.Arrays[I],False);
    If I<'c' then
      Write(', ');
    end;
  Writeln;
  FreeAndNil(J);
  Writeln('Create empty object. Add elements with overloaded Add() method');
  J:=TJSONObject.Create;
  J.Add('a'); // Null
  J.Add('b',True);
  J.Add('c',False);
  J.Add('d',123);
  J.Add('e',2.34);
  J.Add('f','A string');
  J.Add('g',TJSONArray.Create);
  J.Add('h',TJSOnObject.Create);
  DumpJSONData(J);
  FreeAndNil(J);
end;


begin
  DoTestCreate;
  DoTestAs;
  DoTestArray;
  DoTestObject;
end.