-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathceostypes.pas
348 lines (280 loc) · 8.59 KB
/
ceostypes.pas
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
{*****************************************************************}
{ ceostypes is part of Ceos middleware/n-tier JSONRPC components }
{ }
{ Beta version }
{ }
{ This library 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. }
{ }
{ by Jose Benedito - [email protected] }
{ www.jbsolucoes.net }
{*****************************************************************}
unit ceostypes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, variants, fpjson, jsonparser;
type
TCeosArgsType = array of variant;
{ TCeosRequestContent }
TCeosRequestContent = class(TJSONObject)
private
FParams: TJSONArray;
function GetArgs: TJSONArray;
function GetID: integer;
function GetMethod: string;
procedure SetID(AValue: integer);
procedure SetMethod(AValue: string);
public
constructor create;
destructor destroy; override;
property ID: integer read GetID write SetID;
property Method: string read GetMethod write SetMethod;
property Args: TJSONArray read GetArgs;
end;
{ TCeosResponseContent }
TCeosResponseContent = class(TJSONObject)
private
function GetID: integer;
function GetResult: TJSONData;
function GetVersion: string;
procedure SetID(AValue: integer);
procedure SetResult(AValue: TJSONData);
procedure SetVersion(AValue: string);
public
procedure SetResultContent(const AResult: TJSONData; const AID: integer);
constructor Create;
destructor Destroy; override;
property ID: integer read GetID write SetID;
property ResultContent: TJSONData read GetResult write SetResult;
property Version: string read GetVersion write SetVersion;
end;
function GetVariantType(const v: variant): string;
function GetJSONArray(AArray: array of variant): TJSONArray;
//default jsonrpc result response
function JSONRPCResult(const AResult: TJSONData; const AID: integer = -1): TCeosResponseContent;
//default jsonrpc error response
function JSONRPCError(const ACode: integer; const AMessage: string; AID: integer = -1): TCeosResponseContent;
implementation
uses ceosconsts;
function JSONRPCResult(const AResult: TJSONData; const AID: integer = -1): TCeosResponseContent;
var
joResult: TCeosResponseContent;
//s: string;
begin
joResult := TCeosResponseContent.create;
joResult.Version := JSONRPC_VERSION;
joResult.ResultContent := AResult;
joResult.ID := AID;
result := joResult;
//s := joResult.AsJSON;
//s := '';
end;
function JSONRPCError(const ACode: integer; const AMessage: string; AID: integer = -1): TCeosResponseContent;
var
jsonerror: TJSONObject;
joResult: TCeosResponseContent;
begin
jsonerror := TJSONObject.Create();
(jsonerror as TJSONObject).Add('code',ACode);
(jsonerror as TJSONObject).Add('message',AMessage);
joResult := TCeosResponseContent.create;
//joResult.Add('jsonrpc',JSONRPC_VERSION);
joResult.Add('error',jsonerror);
joResult.ID := AID;
result := joResult;
end;
function GetVariantType(const v: variant): string;
begin
case TVarData(v).vType of
varEmpty: result := 'Empty';
varNull: result := 'Null';
varSmallInt: result := 'SmallInt';
varInteger: result := 'Integer';
varSingle: result := 'Single';
varDouble: result := 'Double';
varCurrency: result := 'Currency';
varDate: result := 'Date';
varOleStr: result := 'OleStr';
varDispatch: result := 'Dispatch';
varError: result := 'Error';
varBoolean: result := 'Boolean';
varVariant: result := 'Variant';
varUnknown: result := 'Unknown';
varByte: result := 'Byte';
varString: result := 'String';
varTypeMask: result := 'TypeMask';
varArray: result := 'Array';
varByRef: result := 'ByRef';
end; // case
end;
function GetJSONArray(AArray: array of variant): TJSONArray;
var
joArray: TJSONArray;
i, iLen: integer;
vtype: tvartype;
begin
joArray := TJSONArray.create;
iLen := High(AArray);
for i := 0 to iLen do
begin
vtype := TVarData(AArray[i]).vType;
case vtype of
varEmpty: joArray.Add('');
varNull: joArray.Add;
varSmallInt: joArray.Add(integer(AArray[i]));
varshortint: joArray.Add(integer(AArray[i]));
varInteger: joArray.Add(integer(AArray[i]));
varSingle: joArray.Add(integer(AArray[i]));
varDouble: joArray.Add(TJSONFloat(AArray[i]));
varCurrency: joArray.Add(TJSONFloat(AArray[i]));
varDate: joArray.Add(datetostr(AArray[i]));
varOleStr: joArray.Add(string(AArray[i]));
varDispatch: joArray.Add(string(AArray[i]));
varError: joArray.Add('error');
varBoolean: joArray.Add(boolean(AArray[i]));
varVariant: joArray.Add(string(AArray[i]));
varUnknown: joArray.Add(string(AArray[i]));
varByte: joArray.Add(integer(AArray[i]));
varString: joArray.Add(string(AArray[i]));
varTypeMask: joArray.Add(string(AArray[i]));
varArray: joArray.Add(GetJSONArray(AArray[i]));
varByRef: joArray.Add(integer(AArray[i]));
end;
end;
result := joArray;
end;
{ TCeosResponseContent }
function TCeosResponseContent.GetID: integer;
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('id') <> nil then
result := TJSONObject(Self).Find('id').AsInteger
else
result := -1;
{$WARNINGS ON}
end;
function TCeosResponseContent.GetResult: TJSONData;
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('result') <> nil then
result := TJSONObject(Self).Find('result')
else
result := nil;
{$WARNINGS ON}
end;
function TCeosResponseContent.GetVersion: string;
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('jsonrpc') <> nil then
result := TJSONObject(Self).Find('jsonrpc').AsString
else
result := JSONRPC_VERSION;
{$WARNINGS ON}
end;
procedure TCeosResponseContent.SetID(AValue: integer);
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('id') <> nil then
TJSONObject(Self).Find('id').AsInteger := AValue
else
TJSONObject(Self).Add('id',AValue);
{$WARNINGS ON}
end;
procedure TCeosResponseContent.SetResult(AValue: TJSONData);
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('result') <> nil then
TJSONObject(Self).Delete('result');
TJSONObject(Self).Add('result',AValue);
//TJSONObject(Self).Add('result',AValue as TJSONObject);
{$WARNINGS ON}
end;
procedure TCeosResponseContent.SetVersion(AValue: string);
begin
{$WARNINGS OFF}
if TJSONObject(Self).Find('jsonrpc') <> nil then
TJSONObject(Self).Find('jsonrpc').AsString := AValue
else
TJSONObject(Self).Add('jsonrpc',AValue);
{$WARNINGS ON}
end;
procedure TCeosResponseContent.SetResultContent(const AResult: TJSONData;
const AID: integer);
begin
Self.Clear;
Self.Version := JSONRPC_VERSION;
Self.ResultContent := AResult;
Self.ID := AID;
end;
constructor TCeosResponseContent.Create;
begin
inherited Create;
Self.Version := JSONRPC_VERSION;
Self.ID := 0;
end;
destructor TCeosResponseContent.Destroy;
begin
inherited Destroy;
end;
{ TCeosRequestContent }
function TCeosRequestContent.GetArgs: TJSONArray;
begin
if Find('params') <> nil then
result := (Find('params') as TJSONArray)
else
begin
FParams := TJSONArray.Create;
self.Add('params',FParams);
result := (Find('params') as TJSONArray);
end;
end;
function TCeosRequestContent.GetID: integer;
begin
if Self.Find('id') <> nil then
result := Self.Find('id').AsInteger
else
begin
Self.Add('id',0);
result := 0;
end;
end;
function TCeosRequestContent.GetMethod: string;
begin
if Self.Find('method') <> nil then
result := Self.Find('method').AsString
else
begin
Self.Add('method','');
result := '';
end;
end;
procedure TCeosRequestContent.SetID(AValue: integer);
begin
if Self.Find('id') <> nil then
Self.Find('id').AsInteger := AValue
else
Self.Add('id',AValue);
end;
procedure TCeosRequestContent.SetMethod(AValue: string);
begin
if Self.Find('method') <> nil then
Self.Find('method').AsString := AValue
else
Self.Add('method',AValue);
end;
constructor TCeosRequestContent.create;
begin
inherited create;
FParams := TJSONArray.create;
Self.Add('jsonrpc',JSONRPC_VERSION);
Self.Add('method','');
Self.Add('params',FParams);
Self.Add('id',0);
end;
destructor TCeosRequestContent.destroy;
begin
inherited destroy;
end;
end.