-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.JsonDataObjects.pas
268 lines (216 loc) · 8.45 KB
/
JPL.JsonDataObjects.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
unit JPL.JsonDataObjects;
{
Jacek Pazera
https://www.pazera-software.com
https://github.com/jackdp
License: public domain.
Helper class and routines for JsonDataObjects unit from https://github.com/ahausladen/JsonDataObjects
2022.06
}
interface
uses
Classes, SysUtils,
JsonDataObjects
;
type
TJPJsonDataObjectHelper = class helper for TJsonObject
private
public
function NameTypeExists(const Name: string; const JsonDataType: TJsonDataType): Boolean;
function TryGetObject(const Name: string; var JsonObject: TJsonObject): Boolean;
function TryGetArray(const Name: string; var JsonArray: TJsonArray): Boolean;
procedure AddObject(const ObjName: string; Obj: TJsonObject; AddCopyOfObj: Boolean = True);
procedure AddArray(const ArrName: string; Arr: TJsonArray; AddCopyOfArr: Boolean = True);
function GetJsonObject(const Name: string; CreateIfNotExists: Boolean = True): TJsonObject;
function GetJsonArray(const Name: string; CreateIfNotExists: Boolean = True): TJsonArray;
function ReadBool(const Name: string; const Default: Boolean): Boolean;
procedure WriteBool(const Name: string; const Value: Boolean);
function ReadString(const Name, Default: string): string;
procedure WriteString(const Name, Value: string);
function TryGetAsString(const Name: string; var Value: string): Boolean;
function ReadInteger(const Name: string; const Default: integer): integer;
procedure WriteInteger(const Name: string; const Value: integer);
function ReadInt64(const Name: string; const Default: Int64): Int64;
procedure WriteInt64(const Name: string; const Value: Int64);
function ReadUInt64(const Name: string; const Default: UInt64): UInt64;
procedure WriteUInt64(const Name: string; const Value: UInt64);
function ReadFloat(const Name: string; const Default: Double): Double;
procedure WriteFloat(const Name: string; const Value: Double);
function ReadDateTime(const Name: string; const Default: TDateTime): TDateTime;
procedure WriteDateTime(const Name: string; const Value: TDateTime);
function TryGetAsDateTime(const Name: string; var Value: TDateTime): Boolean;
end;
function GetJSONObjectFromStr(const JSONSource: string; Silent: Boolean = True): TJSONObject; overload;
function GetJSONObjectFromStr(const JSONSource: string; var ErrStr: string): TJSONObject; overload;
function IsNumberDataType(const AJsonDataType: TJsonDataType): Boolean;
implementation
// TJsonDataType = (jdtNone, jdtString, jdtInt, jdtLong, jdtULong, jdtFloat, jdtDateTime, jdtBool, jdtArray, jdtObject);
function GetJSONObjectFromStr(const JSONSource: string; Silent: Boolean = True): TJSONObject; overload;
begin
Result := TJsonObject.Create;
try
Result.FromJSON(JSONSource);
except
on E: Exception do
begin
if Assigned(Result) then Result.Free;
if Silent then Exit
else
raise Exception.Create(E.Message);
end;
end;
end;
function GetJSONObjectFromStr(const JSONSource: string; var ErrStr: string): TJSONObject;
begin
Result := TJsonObject.Create;
try
Result.FromJSON(JSONSource);
except
on E: Exception do
begin
if Assigned(Result) then Result.Free;
ErrStr := E.Message;
end;
end;
end;
function IsNumberDataType(const AJsonDataType: TJsonDataType): Boolean;
begin
Result :=
(AJsonDataType = jdtInt) or (AJsonDataType = jdtLong) or (AJsonDataType = jdtULong) or
(AJsonDataType = jdtFloat);
end;
{$region ' TJPJsonDataObjectHelper ' }
function TJPJsonDataObjectHelper.NameTypeExists(const Name: string; const JsonDataType: TJsonDataType): Boolean;
begin
if not Contains(Name) then Exit(False);
if Types[Name] <> JsonDataType then Exit(False);
Result := True;
end;
function TJPJsonDataObjectHelper.TryGetObject(const Name: string; var JsonObject: TJsonObject): Boolean;
begin
if NameTypeExists(Name, jdtObject) then
begin
JsonObject := O[Name];
Result := True;
end
else Result := False;
end;
function TJPJsonDataObjectHelper.TryGetArray(const Name: string; var JsonArray: TJsonArray): Boolean;
begin
if NameTypeExists(Name, jdtArray) then
begin
JsonArray := A[Name];
Result := True;
end
else Result := False;
end;
//----------------------------------------------------------------------------
procedure TJPJsonDataObjectHelper.AddArray(const ArrName: string; Arr: TJsonArray; AddCopyOfArr: Boolean = True);
begin
Self.A[ArrName].Assign(Arr);
if not AddCopyOfArr then Arr.Free;
end;
procedure TJPJsonDataObjectHelper.AddObject(const ObjName: string; Obj: TJsonObject; AddCopyOfObj: Boolean = True);
begin
Self.O[ObjName].Assign(Obj);
if not AddCopyOfObj then Obj.Free;
end;
function TJPJsonDataObjectHelper.GetJsonArray(const Name: string; CreateIfNotExists: Boolean): TJsonArray;
begin
if NameTypeExists(Name, jdtArray) then Result := A[Name]
else
if CreateIfNotExists then Result := A[Name]
else Result := nil;
end;
function TJPJsonDataObjectHelper.GetJsonObject(const Name: string; CreateIfNotExists: Boolean = True): TJsonObject;
begin
if NameTypeExists(Name, jdtObject) then Result := O[Name]
else
if CreateIfNotExists then Result := O[Name]
else Result := nil;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadBool(const Name: string; const Default: Boolean): Boolean;
begin
if NameTypeExists(Name, jdtBool) then Result := B[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteBool(const Name: string; const Value: Boolean);
begin
B[Name] := Value;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadString(const Name, Default: string): string;
begin
if NameTypeExists(Name, jdtString) then Result := S[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteString(const Name, Value: string);
begin
S[Name] := Value;
end;
function TJPJsonDataObjectHelper.TryGetAsString(const Name: string; var Value: string): Boolean;
begin
if NameTypeExists(Name, jdtString) then
begin
Value := S[Name];
Result := True;
end
else Result := False;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadInteger(const Name: string; const Default: integer): integer;
begin
if NameTypeExists(Name, jdtInt) then Result := I[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteInteger(const Name: string; const Value: integer);
begin
I[Name] := Value;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadInt64(const Name: string; const Default: Int64): Int64;
begin
if NameTypeExists(Name, jdtLong) then Result := L[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteInt64(const Name: string; const Value: Int64);
begin
L[Name] := Value;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadUInt64(const Name: string; const Default: UInt64): UInt64;
begin
if NameTypeExists(Name, jdtULong) then Result := U[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteUInt64(const Name: string; const Value: UInt64);
begin
U[Name] := Value;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadFloat(const Name: string; const Default: Double): Double;
begin
if NameTypeExists(Name, jdtFloat) then Result := F[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteFloat(const Name: string; const Value: Double);
begin
F[Name] := Value;
end;
//----------------------------------------------------------------------------
function TJPJsonDataObjectHelper.ReadDateTime(const Name: string; const Default: TDateTime): TDateTime;
begin
//if NameTypeExists(Name, jdtDateTime) then Result := D[Name] else Result := Default;
if NameTypeExists(Name, jdtString) then Result := D[Name] else Result := Default;
end;
procedure TJPJsonDataObjectHelper.WriteDateTime(const Name: string; const Value: TDateTime);
begin
D[Name] := Value;
end;
function TJPJsonDataObjectHelper.TryGetAsDateTime(const Name: string; var Value: TDateTime): Boolean;
begin
//if NameTypeExists(Name, jdtDateTime) then
if NameTypeExists(Name, jdtString) then
begin
Value := D[Name];
Result := True;
end
else Result := False;
end;
{$endregion TJPJsonDataObjectHelper}
end.