-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.JSON.pas
456 lines (390 loc) · 12 KB
/
GS.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
unit GS.JSON;
{$I GSCore.inc}
interface
Uses
{$IFDEF FPC}
Classes,
SysUtils,
{$IFDEF USE_GENERIC}
Generics.Collections, //Starting 3.1.1 only.
{$ENDIF}
{$ELSE}
System.Classes,
System.SysUtils,
{$IFDEF USE_GENERIC}
System.Generics.Collections,
{$ENDIF}
{$ENDIF}
Jsons,
JsonsUtilsEx,
{$IFDEF BCPBASE64}
BcpBase64,
{$ENDIF}
GS.Common;
Type
TGSJsonArrayPropertyItem = Class
Public
PropertyName : string;
ItemArrayType :TClass;
End;
{$IFDEF USE_GENERIC}
TGSJsonListOfProperty = class(TDictionary<String,TGSJsonArrayPropertyItem>)
Procedure AddListProperty(anArrayPropertyName : String; aItemArrayType : TClass);
Function GetPropertyConfiguration(anArrayPropertyName : String) : TGSJsonArrayPropertyItem;
Procedure RegisterPropertyArrayTypes(Const PropertyName : String; ItemArrayType : TClass);
end;
{$ELSE}
TGSJsonListOfProperty = class(TKeysValues_UTF8StringToObject)
Function TryGetValue(aKey : String; var aResult : TGSJsonArrayPropertyItem) : Boolean; Reintroduce;
Procedure Add(aString : String; aGSJsonArrayPropertyItem: TGSJsonArrayPropertyItem); Reintroduce;
Procedure AddListProperty(anArrayPropertyName : String; aItemArrayType : TClass);
Function GetPropertyConfiguration(anArrayPropertyName : String) : TGSJsonArrayPropertyItem;
Procedure RegisterPropertyArrayTypes(Const PropertyName : String; ItemArrayType : TClass);
end;
{$ENDIF}
TGSJsonArray = TJsonArray;
TGSJsonObject = TJsonObject;
TGSJson = class(TJson)
private
function GetJson: TJson;
Protected
class var FListOfProperty : TGSJsonListOfProperty; { TODO : TO REMOVE PLZ ! }
class function CheckInstance : Boolean;
class Procedure Init;
class Procedure Release;
Public
{$IFDEF BCPBASE64}
class var base64 : TBase64;
class Function Base64StringToBytes(Const aBase64String : String) : TBytes;
class function BytesToBase64String(const aBytes : TBytes) : String;
{$ENDIF}
{$IFDEF DCC} //Currently, this compile on FPC work only for trunck. But Trunck is broken on ARM. :(
class Procedure JsonToObject(Const aJSONString : String; var aObject : TObject);
class Function ObjectToJson(aObject : TObject) : String;
{$ENDIF DCC} //Currently, this compile on FPC work only for trunck. But Trunck is broken on ARM. :(
class function JSONDateToString(aDate : TDateTime) : String;
class function JSONStringToDate(aDate : String) : TDateTime;
class Function Configuration : TGSJsonListOfProperty;
function Path(jsonPath : string) : TJsonValue;
end;
IGSJsonObject = interface;
IGSJsonObject = interface
function Put(const Name: String; const Value: Boolean): TJsonValue; overload;
function Put(const Name: String; const Value: Integer): TJsonValue; overload;
function Put(const Name: String; const Value: Extended): TJsonValue; overload;
function Put(const Name: String; const Value: String): TJsonValue; overload;
function Stringify : string;
procedure parser(aJsonString : String);
function get(const name : String) : String;
function getAsValue(const name : String) : TJsonValue;
function getAsArray(const name : String) : TJsonArray;
function exists(const name : string) : boolean;
end;
TGSJsonImpl = class(TInterfacedObject, IGSJsonObject)
private
FJson : TGSJSONObject;
public
constructor Create; virtual;
destructor Destroy; Override;
//IGSJsonObject
function Put(const Name: String; const Value: Boolean): TJsonValue; overload;
function Put(const Name: String; const Value: Integer): TJsonValue; overload;
function Put(const Name: String; const Value: Extended): TJsonValue; overload;
function Put(const Name: String; const Value: String): TJsonValue; overload;
function Stringify : string;
procedure parser(aJsonString : String);
function get(const name : String) : String;
function getAsValue(const name : String) : TJsonValue;
function getAsArray(const name : String) : TGSJsonArray;
function exists(const name : string) : boolean;
end;
TGSJsonTool = class
class function JSONStrToDateTime(aJsonStr : string) : TDateTime;
class function DataTimeToJSONStr(aDataTime : TDateTime) : String;
end;
implementation
{ TGSJson }
{$IFDEF BCPBASE64}
class function TGSJson.Base64StringToBytes(const aBase64String: String): TBytes;
begin
result := base64.Decode(aBase64String);
end;
class function TGSJson.BytesToBase64String(const aBytes: TBytes): String;
begin
result := base64.Encode(aBytes);
end;
{$ENDIF}
class function TGSJson.CheckInstance: Boolean;
begin
result :=true;
if Not Assigned(FListOfProperty) then
begin
FListOfProperty := TGSJsonListOfProperty.Create;
end;
end;
class function TGSJson.Configuration: TGSJsonListOfProperty;
begin
CheckInstance;
Result := FListOfProperty;
end;
function TGSJson.GetJson: TJson;
begin
result := TJson(Self);
end;
class procedure TGSJson.Init;
begin
FListOfProperty := Nil;
{$IFDEF BCPBASE64}
base64 := TBase64.Create;
{$ENDIF}
end;
class function TGSJson.JSONDateToString(aDate: TDateTime): String;
begin
result := jsonsUtilsEx.JSONDateToString(aDate);
end;
class function TGSJson.JSONStringToDate(aDate: String): TDateTime;
begin
result := jsonsUtilsEx.JSONStringToDate(aDate);
end;
{$IFDEF DCC} //Currently, this compile on FPC work only for trunk. But Trunk is broken on ARM. :(
class Procedure TGSJson.JsonToObject(const aJSONString: String;
var aObject: TObject);
begin
JsonsUtilsEx.__JsonToObject(aJSONString, aObject);
end;
class function TGSJson.ObjectToJson(aObject: TObject): String;
begin
Result := jsonsUtilsEx.__ObjectToJson(aObject);
end;
{$ENDIF DCC} //Currently, this compile on FPC work only for trunk. But Trunk is broken on ARM. :(
function TGSJson.Path(jsonPath: string): TJsonValue;
var l : TstringList;
ll : TJsonObject;
I: Integer;
begin
result := nil;
l := TStringList.Create;
try
l.Delimiter := '.';
l.DelimitedText := jsonPath;
if l.Count>1 then
begin
ll := get(l[0]).AsObject;
if l.Count=2 then
begin
result := ll.Values[l[1]];
end
else
begin
for i := 1 to l.Count-2 do
ll := ll.Values[l[i]].AsObject;
result := ll.Values[l[l.Count-1]];
end;
end
else
raise Exception.Create('Error Message');
finally
FreeAndNil(l);
end;
end;
class procedure TGSJson.Release;
{$IFDEF USE_GENERIC}
var aItem : TPair<String, TGSJsonArrayPropertyItem>;
ab : TArray<TPair<String, TGSJsonArrayPropertyItem>>;
{$ELSE}
var i : integer;
{$ENDIF}
begin
if Assigned(FListOfProperty) then
begin
{$IFDEF USE_GENERIC}
ab := FListOfProperty.ToArray;
for aItem in ab do
begin
aItem.Value.Free;
end;
FreeAndNil(FListOfProperty);
{$ELSE}
for I := 0 to FListOfProperty.Count-1 do
begin
FListOfProperty.FArray[i].Value.Free;
end;
{$ENDIF}
end;
{$IFDEF BCPBASE64}
FreeAndNil(base64);
{$ENDIF}
end;
{ TGSJsonListOfProperty }
procedure TGSJsonListOfProperty.RegisterPropertyArrayTypes(const PropertyName: String;
ItemArrayType: TClass);
begin
AddListProperty(PropertyName,ItemArrayType);
end;
procedure TGSJsonListOfProperty.Add(aString: String;
aGSJsonArrayPropertyItem: TGSJsonArrayPropertyItem);
begin
Assert(assigned(aGSJsonArrayPropertyItem));
Inherited Add(aString, aGSJsonArrayPropertyItem);
end;
function TGSJsonListOfProperty.TryGetValue(aKey: String;
var aResult: TGSJsonArrayPropertyItem): Boolean;
begin
Result := Inherited TryGetValue(aKey,TObject(aResult));
end;
procedure TGSJsonListOfProperty.AddListProperty(anArrayPropertyName: String;
aItemArrayType: TClass);
var aItem : TGSJsonArrayPropertyItem;
begin
Assert(length(anArrayPropertyName)>0);
if not TryGetValue(anArrayPropertyName,aItem) then
begin
aItem := TGSJsonArrayPropertyItem.Create;
Add(anArrayPropertyName,aItem);
aItem.ItemArrayType := aItemArrayType;
end;
end;
function TGSJsonListOfProperty.GetPropertyConfiguration(
anArrayPropertyName: String): TGSJsonArrayPropertyItem;
var aItem : TGSJsonArrayPropertyItem;
begin
Result := Nil;
if TryGetValue(anArrayPropertyName,aItem) then
result := aItem
else
begin
raise Exception.Create('Error Message');
end;
end;
{ TODO : Put a function to pretiffy, or dumb a json stqrting from the fellowing code (to refactor in a recursive manner)
var l : TStringStream;
ljson : TJson;
ljv : TJsonbase;
i : integer;
lpname : string;
procedure InternalJsonValueProcess(aValue : TJSonValue; const aName : String = '');
begin
case aValue.ValueType of
jvNone,jvNull : raise Exception.Create('todo');
jvString : FQuery.parameters.addStringValue(aValue.AsString,aName);
jvArray : raise Exception.Create('todo');
jvNumber : FQuery.parameters.addIntegerValue(Round(aValue.AsNumber),aName);
jvBoolean : FQuery.parameters.addBooleanValue(aValue.AsBoolean,aName);
jvObject : raise Exception.Create('todo');
end;
end;
begin
l := TStringStream.Create;
try
l.LoadFromStream(stream);
ljson := TJson.Create;
try
ljson.Parse(l.DataString);
case ljson.StructType of
jsNone: ;
jsArray: ;
jsObject:
begin
decodeF
end;
end;
for i := 0 to ljson.Count-1 do
begin
ljv := ljson.Get(i);
if ljv is TJsonValue then
begin
InternalJsonValueProcess(TJSonValue(ljv));
end
else
if ljv is TJsonPair then
begin
lpname := TJsonPair(ljv).Name;
InternalJsonValueProcess(TJSonPair(ljv).Value,lpName);
end
else
if ljv is TJsonArray then
begin
raise Exception.Create('Error Message');
end
else
if ljv is TJsonObject then
begin
raise Exception.Create('Error Message');
end;
end;
finally
FreeAndNil(ljson);
end;
finally
FreeAndNil(l);
end;
end;
}
{ TGSJsonImpl }
constructor TGSJsonImpl.Create;
begin
inherited;
FJson := TGSJSONObject.Create;
end;
destructor TGSJsonImpl.Destroy;
begin
FreeAndNil(FJson);
inherited;
end;
function TGSJsonImpl.exists(const name: string): boolean;
begin
result := FJson.Find(name)>-1;
end;
function TGSJsonImpl.get(const name: String): String;
begin
result := FJson.Values[name].AsString;
end;
function TGSJsonImpl.getAsArray(const name: String): TGSJsonArray;
begin
result := FJson.Values[name].AsArray;
end;
function TGSJsonImpl.getAsValue(const name: String): TJsonValue;
begin
result := FJson.Values[name];
end;
procedure TGSJsonImpl.parser(aJsonString: String);
begin
FJson.Parse(aJsonString);
end;
function TGSJsonImpl.Put(const Name: String; const Value: Boolean): TJsonValue;
begin
result := FJson.Put(Name,Value);
end;
function TGSJsonImpl.Put(const Name: String; const Value: Integer): TJsonValue;
begin
result := FJson.Put(Name,Value);
end;
function TGSJsonImpl.Put(const Name: String; const Value: Extended): TJsonValue;
begin
result := FJson.Put(Name,Value);
end;
function TGSJsonImpl.Put(const Name, Value: String): TJsonValue;
begin
result := FJson.Put(Name,Value);
end;
function TGSJsonImpl.Stringify: string;
begin
result := FJson.Stringify;
end;
{ TGSJSonTool }
class function TGSJSonTool.DataTimeToJSONStr(aDataTime: TDateTime): String;
begin
result := JSONDateToString(aDataTime);
end;
class function TGSJSonTool.JSONStrToDateTime(aJsonStr: string): TDateTime;
begin
if aJsonStr.Trim.Length=0 then
result := 0.0
else
result := JSONStringToDate(aJsonStr);
end;
Initialization
TGSJson.Init;
Finalization
TGSJson.Release;
end.