-
Notifications
You must be signed in to change notification settings - Fork 3
/
UJsonObject.pas
138 lines (118 loc) · 2.9 KB
/
UJsonObject.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
unit UJsonObject;
interface
uses
System.JSON.Serializers,System.JSON.Types,Generics.Collections;
type
TJsonObjectEnumerator<T> = class;
{$M+}
TJsonObject<T> = class(TObject)
private
fMyObjectList:TList<TJsonObject<T>>;
fNode:T;
procedure MyFreeAndNil(var Obj);
public
Constructor Create;
Destructor Free;
Function SaveToText:String;
Procedure LoadFromText(JsonText:string);
Function ForInStorage:TJsonObjectEnumerator<T>;
published
property Node:T read fNode write fNode;
property Storage:TList<TJsonObject<T>> read fMyObjectList write fMyObjectList;
end;
{$M-}
TJsonObjectEnumerator<T>=class
private
ForInIndex:integer;
fEnumItem:TJsonObject<T>;
function GetCurrentItem:TJsonObject<T>;
public
property Current:TJsonObject<T> read GetCurrentItem;
function MoveNext:boolean;
function GetEnumerator:TJsonObjectEnumerator<T>;{$IFDEF SUPPORTS_INLINE}inline;{$ENDIF}
Constructor Create(EnumItem:TJsonObject<T>);
Destructor Destroy;override;
end;
implementation
{ TJsonObject<T> }
constructor TJsonObject<T>.Create;
begin
Storage:=TList<TJsonObject<T>>.Create;
end;
function TJsonObject<T>.ForInStorage: TJsonObjectEnumerator<T>;
begin
result:= TJsonObjectEnumerator<t>.Create(self);
end;
destructor TJsonObject<T>.Free;
var
tmp:T;
tmpJsonObj: TJsonObject<T>;
begin
tmp:=Node;
MyFreeAndNil(tmp);
while Storage.Count>0 do
begin
tmpJsonObj:=storage[0];
storage.Remove(tmpJsonObj);
tmpJsonObj.Free;
end;
Storage.Free;
end;
procedure TJsonObject<T>.LoadFromText(JsonText: string);
var
LSerializer : TJsonSerializer;
begin
LSerializer := TJsonSerializer.Create;
LSerializer.Formatting := TJsonFormatting.Indented;
LSerializer.Populate(JsonText,self);
LSerializer.Free
end;
//Copy FreeAndNil from SysUtils
procedure TJsonObject<T>.MyFreeAndNil(var Obj);
var
Temp: TObject;
begin
Temp := TObject(Obj);
Pointer(Obj) := nil;
Temp.Free;
end;
function TJsonObject<T>.SaveToText: String;
var
LSerializer : TJsonSerializer;
begin
LSerializer := TJsonSerializer.Create;
LSerializer.Formatting := TJsonFormatting.Indented;
result:= LSerializer.Serialize(self);
LSerializer.Free;
end;
{ TJsonObjectEnumerator<T> }
constructor TJsonObjectEnumerator<T>.Create(EnumItem: TJsonObject<T>);
begin
fEnumItem:= EnumItem;
ForInIndex:=-1;
end;
destructor TJsonObjectEnumerator<T>.Destroy;
begin
inherited;
end;
function TJsonObjectEnumerator<T>.GetCurrentItem: TJsonObject<T>;
begin
result:=nil;
if ForInIndex<0 then exit;
if fEnumItem.storage.Count>0 then
if ForInIndex<fEnumItem.storage.Count then
result:= fEnumItem.Storage[ForInIndex];
end;
function TJsonObjectEnumerator<T>.GetEnumerator: TJsonObjectEnumerator<T>;
begin
result:=self;
end;
function TJsonObjectEnumerator<T>.MoveNext: boolean;
begin
result:=true;
if ForInIndex>=fEnumItem.storage.Count-1 then
result:=false;
if result then
ForInIndex:=ForInIndex+1;
end;
end.