forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.json.pas
219 lines (197 loc) · 5.9 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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.json;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers;
function marshal (const obj: TJsonValue): string;
function unmarshal(const value: string) : TJsonValue;
function getPropAsStr(obj: TJsonValue; const name: string; const def: string = ''): string;
function getPropAsInt(obj: TJsonValue; const name: string; def: Integer = 0): Integer;
function getPropAsExt(obj: TJsonValue; const name: string; def: Extended = 0): Extended;
function getPropAsBig(obj: TJsonValue; const name: string; def: BigInteger): BigInteger;
function getPropAsObj(obj: TJsonValue; const name: string): TJsonObject;
function getPropAsArr(obj: TJsonValue; const name: string): TJsonArray;
function quoteString(const S: string; Quote: Char = '"'): string;
implementation
function marshal(const obj: TJsonValue): string;
var
B: TBytes;
I: Integer;
begin
Result := '';
if not Assigned(obj) then
EXIT;
I := obj.EstimatedByteSize;
if I <= 0 then
EXIT;
SetLength(B, I);
I := obj.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(obj: TJsonValue; const name: string; const def: string): string;
var
P: TJsonPair;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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(obj: TJsonValue; const name: string; def: Integer): Integer;
var
P: TJsonPair;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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 getPropAsExt(obj: TJsonValue; const name: string; def: Extended): Extended;
var
P : TJsonPair;
FS: TFormatSettings;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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
FS := TFormatSettings.Create;
FS.DecimalSeparator := '.';
Result := StrToFloat(TJsonString(P.JsonValue).Value, FS);
end;
end;
function getPropAsBig(obj: TJsonValue; const name: string; def: BigInteger): BigInteger;
var
P: TJsonPair;
begin
Result := def;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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(obj: TJsonValue; const name: string): TJsonObject;
var
P: TJsonPair;
begin
Result := nil;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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(obj: TJsonValue; const name: string): TJsonArray;
var
P: TJsonPair;
begin
Result := nil;
if not Assigned(obj) then
EXIT;
if not(obj is TJsonObject) then
EXIT;
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 quoteString(const S: string; Quote: Char): string;
var
I: Integer;
begin
Result := S;
if Length(Result) > 0 then
begin
// add extra backslash is there is a backslash, for example: c:\ --> c:\\
I := 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 := 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.