-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
web3.json.pas
347 lines (307 loc) · 10.8 KB
/
web3.json.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <svanas@runbox.com> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ 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. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.json;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3;
type
TCustomDeserialized = class abstract(TInterfacedObject)
public
constructor Create(const aJsonValue: TJsonValue); virtual;
end;
TDeserialized = class abstract(TCustomDeserialized)
protected
FJsonValue: TJsonValue;
public
constructor Create(const aJsonValue: TJsonValue); override;
destructor Destroy; override;
function ToString: string; override;
end;
IDeserializedArray<T: IInterface> = interface
function Count: Integer;
procedure Delete(const Index: Integer);
function Item(const Index: Integer): T;
function ToString: string;
end;
TDeserializedArray<T: IInterface> = class abstract(TDeserialized, IDeserializedArray<T>)
public
function Count: Integer;
procedure Delete(const Index: Integer);
function Item(const Index: Integer): T; virtual; abstract;
end;
function marshal(const value: TJsonValue): string;
function unmarshal(const value: string): TJsonValue;
function getPropAsStr(const obj: TJsonValue; const name: string; const def: string = ''): string;
function getPropAsInt(const obj: TJsonValue; const name: string; const def: Integer = 0): Integer;
function getPropAsUInt64(const obj: TJsonValue; const name: string; const def: UInt64 = 0): UInt64;
function getPropAsDouble(const obj: TJsonValue; const name: string; const def: Double = 0): Double;
function getPropAsBigInt(const obj: TJsonValue; const name: string): BigInteger; overload;
function getPropAsBigInt(const obj: TJsonValue; const name: string; const def: BigInteger): BigInteger; overload;
function getPropAsObj(const obj: TJsonValue; const name: string): TJsonObject;
function getPropAsArr(const obj: TJsonValue; const name: string): TJsonArray;
function getPropAsBool(const obj: TJsonValue; const name: string; const def: Boolean = False): Boolean;
function quoteString(const S: string; const Quote: Char = '"'): string;
implementation
{---------------------------- TCustomDeserialized -----------------------------}
constructor TCustomDeserialized.Create(const aJsonValue: TJsonValue);
begin
inherited Create;
end;
{------------------------------- TDeserialized --------------------------------}
constructor TDeserialized.Create(const aJsonValue: TJsonValue);
begin
inherited Create(aJsonValue);
if Assigned(aJsonValue) then
FJsonValue := aJsonValue.Clone as TJsonValue
else
FJsonValue := nil;
end;
destructor TDeserialized.Destroy;
begin
if Assigned(FJsonValue) then FJsonValue.Free;
inherited Destroy;
end;
function TDeserialized.ToString: string;
begin
Result := web3.json.marshal(Self.FJsonValue);
end;
{--------------------------- TDeserializedArray<T> ----------------------------}
function TDeserializedArray<T>.Count: Integer;
begin
if Assigned(Self.FJsonValue) and (Self.FJsonValue is TJsonArray) then
Result := TJsonArray(Self.FJsonValue).Count
else
Result := 0;
end;
procedure TDeserializedArray<T>.Delete(const Index: Integer);
begin
if not Assigned(Self.FJsonValue) then
EXIT;
if not(Self.FJsonValue is TJsonArray) then
EXIT;
TJsonArray(Self.FJsonValue).Remove(Index);
end;
{------------------------------ global functions ------------------------------}
function marshal(const value: TJsonValue): string;
begin
Result := '';
if not Assigned(value) then
EXIT;
var I := value.EstimatedByteSize;
if I <= 0 then
EXIT;
var B: TBytes;
SetLength(B, I);
I := value.ToBytes(B, 0);
if I <= 0 then
SetLength(B, 0)
else
SetLength(B, I);
Result := TEncoding.UTF8.GetString(B);
end;
function unmarshal(const value: string): TJsonValue;
begin
Result := TJsonObject.ParseJsonValue(value.Trim);
end;
function getPropAsStr(const obj: TJsonValue; const name: string; const def: string): string;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
begin
if P.JsonValue is TJsonString then
Result := TJsonString(P.JsonValue).Value
else
Result := P.JsonValue.ToString;
if SameText(Result, 'null') or SameText(Result, 'undefined') then
Result := def;
end;
end;
function getPropAsInt(const obj: TJsonValue; const name: string; const def: Integer): Integer;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonNumber then
Result := TJsonNumber(P.JsonValue).AsInt
else
if P.JsonValue is TJsonString then
Result := StrToIntDef(TJsonString(P.JsonValue).Value, def)
else
Result := def;
end;
function getPropAsUInt64(const obj: TJsonValue; const name: string; const def: UInt64): UInt64;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) and Assigned(P.JsonValue) then
if P.JsonValue is TJsonNumber then
{$IF CompilerVersion < 35}
Result := StrToUInt64Def(P.JsonValue.Value, def)
{$ELSE}
Result := TJsonNumber(P.JsonValue).AsUInt64
{$IFEND}
else if P.JsonValue is TJsonString then
Result := StrToUInt64Def(TJsonString(P.JsonValue).Value, def);
end;
function getPropAsDouble(const obj: TJsonValue; const name: string; const def: Double): Double;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonNumber then
Result := TJsonNumber(P.JsonValue).AsDouble
else
if P.JsonValue is TJsonString then
begin
var FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := StrToFloat(TJsonString(P.JsonValue).Value, FS);
end;
end;
function getPropAsBigInt(const obj: TJsonValue; const name: string): BigInteger;
begin
Result := getPropAsBigInt(obj, name, BigInteger.Zero);
end;
function getPropAsBigInt(const obj: TJsonValue; const name: string; const def: BigInteger): BigInteger;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonNumber then
Result := TJsonNumber(P.JsonValue).AsInt64
else
if P.JsonValue is TJsonString then
Result := BigInteger.Create(TJsonString(P.JsonValue).Value)
else
Result := def;
end;
function getPropAsObj(const obj: TJsonValue; const name: string): TJsonObject;
begin
Result := nil;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonObject then
Result := TJsonObject(P.JsonValue);
end;
function getPropAsArr(const obj: TJsonValue; const name: string): TJsonArray;
begin
Result := nil;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonArray then
Result := TJsonArray(P.JsonValue);
end;
function getPropAsBool(const obj: TJsonValue; const name: string; const def: Boolean): Boolean;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
const P = TJsonObject(obj).Get(name);
if Assigned(P) then
if Assigned(P.JsonValue) then
if P.JsonValue is TJsonTrue then
Result := True
else
if P.JsonValue is TJsonFalse then
Result := False
else
if P.JsonValue is TJsonString then
Result := SameText(TJsonString(P.JsonValue).Value, '1')
or SameText(TJsonString(P.JsonValue).Value, 'yes')
or SameText(TJsonString(P.JsonValue).Value, 'true');
end;
function quoteString(const S: string; const Quote: Char): string;
begin
Result := S;
if Length(Result) > 0 then
begin
var I: Integer;
// add extra backslash is there is a backslash, for example: c:\ --> c:\\
I := System.Low(Result);
while I <= Length(Result) do
if Result[I] = '\' then
begin
Result := Copy(Result, 1, I) + '\' + Copy(Result, I + 1, Length(Result));
I := I + 2;
end
else
Inc(I);
// add backslash if there is a double quote, for example: "a" --> \"a\"
I := System.Low(Result);
while I <= Length(Result) do
if Result[I] = Quote then
begin
Result := Copy(Result, 1, I - 1) + '\' + Copy(Result, I, Length(Result));
I := I + 2;
end
else
Inc(I);
end;
Result := Quote + Result + Quote;
end;
end.