Skip to content

Commit 3d61dc9

Browse files
committed
The native json support
Yep, this is native json support for Simba, crossplatform, with Lape tests.
1 parent 5248db0 commit 3d61dc9

File tree

4 files changed

+3911
-1
lines changed

4 files changed

+3911
-1
lines changed

Tests/lape/JsonTests.simba

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
program new;
2+
3+
procedure testCreateString;
4+
var
5+
json: TJSONObject;
6+
begin
7+
json.Init('{exemplo:"valor", inteiro:1}');
8+
try
9+
if (json.opt('exemplo') <> nil) then
10+
WriteLn(json.optString('exemplo'));
11+
finally
12+
json.free;
13+
end;
14+
end;
15+
16+
procedure testUnicodeCharacters;
17+
var
18+
json: TJSONObject;
19+
begin
20+
json.Init('{exemplo:"\u0046\u0041\u0042\u0049\u004F"}');
21+
try
22+
if (json.opt('exemplo') <> nil) then
23+
WriteLn(json.optString('exemplo'));
24+
finally
25+
json.free;
26+
end;
27+
end;
28+
29+
procedure testOptInt;
30+
var
31+
json: TJSONObject;
32+
begin
33+
try
34+
json.Init('{exemplo:"valor", inteiro:"1"}');
35+
if (json.optInt('inteiro') = 1) then
36+
WriteLn('OptInt Success!');
37+
finally
38+
if Assigned(json) then
39+
json.free;
40+
end;
41+
end;
42+
43+
procedure testNames;
44+
var
45+
json: TJSONObject;
46+
a: TJSONArray;
47+
begin
48+
try
49+
json.Init('{exemplo:"valor", inteiro:1}');
50+
a := json.names;
51+
if a.length = 2 then
52+
WriteLn('testNames success!');
53+
finally
54+
if Assigned(json) then
55+
json.free;
56+
if Assigned(a) then
57+
a.free;
58+
end;
59+
end;
60+
61+
procedure testAssignTo;
62+
var
63+
json, jo: TJSONObject;
64+
begin
65+
try
66+
jo.Init('{exemplo:"valor", inteiro:1}');
67+
json.Init;
68+
jo.assignTo(json);
69+
if json.getInt('inteiro') = 1 then
70+
WriteLn('AssignTo Success!');
71+
finally
72+
if (Assigned(json)) then
73+
json.free;
74+
if (Assigned(jo)) then
75+
jo.free
76+
end;
77+
end;
78+
79+
procedure testRSJsonAPI;
80+
var
81+
json,Obj: TJsonObject;
82+
Arr: TJsonArray;
83+
i: integer;
84+
Len: integer;
85+
begin
86+
try
87+
Json.Init(getPage('http://services.runescape.com/m=itemdb_rs/api/catalogue/category.json?category=1'));
88+
Arr := json.getJSONArray('alpha');
89+
Len := Arr.length();
90+
WriteLn('Array length = '+IntToStr(Len));
91+
//Obj := Arr.getJSONObject(1);
92+
//WriteLn(Obj.GetString('letter'));
93+
WriteLn(Arr.ToString);
94+
For i := 0 to Len-1 do
95+
begin
96+
Obj := Arr.getJSONObject(i);
97+
WriteLn('Letter' + Obj.GetString('letter'));
98+
WriteLn('Items count = ' + Obj.GetInt('items'));
99+
end;
100+
//WriteLn(Json.ToString);}
101+
finally
102+
if Assigned(Json) then
103+
Json.free;
104+
If Assigned(Arr) then
105+
Arr.Free;
106+
end;
107+
end;
108+
109+
begin
110+
testCreateString;
111+
testUnicodeCharacters;
112+
testOptInt;
113+
testNames;
114+
testAssignTo;
115+
testRSJsonAPI;
116+
end;

0 commit comments

Comments
 (0)