-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdelphi_seralizeadapter.pas
More file actions
195 lines (166 loc) · 3.99 KB
/
delphi_seralizeadapter.pas
File metadata and controls
195 lines (166 loc) · 3.99 KB
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
unit delphi_seralizeadapter;
interface
uses
Classes,SysUtils,JsonDataObjects,intf_seralizeadapter;
type
TDJsonNode = class(TInterfacedObject, IDataNode)
strict private
function AddChild: IDataNode;
function AddPropObj(const Name: string): IDataNode;
function PropObjByName(const Name:string): IDataNode;
function ChildCount: integer;
function GetAttributes(Name: string): string;
function GetChildItem(Index:integer): IDataNode;
function GetData: string;
function GetNodeName: string;
function GetValue: variant;
procedure SetAttributes(Name: string; AValue: string);
procedure SetData(AValue: string);
procedure SetNodeName(AValue: string);
procedure SetValue(AValue: variant);
function GetDumpText:string;
private
protected
fDoc: TJsonObject;
FJSon: TJsonObject;
FParent: TJsonObject;
function BuildDataNode(const Node: TJsonObject): IDataNode;
public
end;
TDJsonAdapter = class(TInterfacedObject, IDataAdapter)
strict private
function GetRootNode: IDataNode;
function GetSeralzieString: string;
procedure LoadFromFile(const FileName:string);
function NewDoc: IDataNode;
procedure SaveToFile(const FileName:string);
private
FJ: TJsonObject;
public
destructor Destroy; override;
end;
implementation
destructor TDJsonAdapter.Destroy;
begin
if Assigned(FJ) then
FreeAndNil(FJ);
inherited;
end;
function TDJsonAdapter.GetRootNode: IDataNode;
var
Node:TDJsonNode;
JObj:TJsonObject;
begin
JObj := FJ.O['JSONOBJECT'];
Node :=TDJsonNode.Create;
Node.FJSon :=JObj;
Node.FParent :=FJ;
Node.fDoc :=FJ;
result :=Node;
end;
function TDJsonAdapter.GetSeralzieString: string;
begin
result :=FJ.ToJSON();
end;
procedure TDJsonAdapter.LoadFromFile(const FileName:string);
begin
if Assigned(FJ) then FreeAndNil(FJ);
FJ :=TJsonObject.Create;
FJ.LoadFromFile(FileName);
end;
function TDJsonAdapter.NewDoc: IDataNode;
begin
if Assigned(FJ) then FreeAndNil(FJ);
FJ :=TJsonObject.Create;
end;
procedure TDJsonAdapter.SaveToFile(const FileName:string);
begin
FJ.SaveToFile(FileName,false);
end;
function TDJsonNode.AddChild: IDataNode;
var
JObj:TJsonObject;
Arr:TJsonArray;
begin
Arr :=FJSon.A['ITEMS'];
Jobj :=Arr.AddObject;
Result :=BuildDataNode(JObj);
end;
function TDJsonNode.AddPropObj(const Name: string): IDataNode;
var
JObj:TJsonObject;
begin
JObj :=FJSon.O[LowerCase(Name)];
Result :=BuildDataNode(JObj);
end;
function TDJsonNode.BuildDataNode(const Node: TJsonObject): IDataNode;
var
JNode:TDJsonNode;
begin
JNode :=TDJsonNode.Create;
JNode.fDoc :=self.fDoc;
JNode.FJSon :=Node;
JNode.FParent := FJSon;
result :=JNode;
end;
function TDJsonNode.PropObjByName(const Name:string): IDataNode;
var
JObj:TJsonObject;
begin
JObj :=FJSon.O[LowerCase(Name)];
if Assigned(JObj) then
begin
result :=BuildDataNode(JObj)
end else
begin
result :=nil;
end;
end;
function TDJsonNode.ChildCount: integer;
begin
Result :=FJSon.A['ITEMS'].Count;
end;
function TDJsonNode.GetAttributes(Name: string): string;
begin
result :=FJSon.S[LowerCase(Name)];
end;
function TDJsonNode.GetChildItem(Index: integer): IDataNode;
var
Obj:TJsonObject;
begin
Obj :=FJSon.A['ITEMS'].O[Index];
result :=BuildDataNode(Obj);
end;
function TDJsonNode.GetData: string;
begin
result :=FJSon.Values['cdata'].Value;
end;
function TDJsonNode.GetDumpText: string;
begin
result :=FJSon.ToJSON(true);
end;
function TDJsonNode.GetNodeName: string;
begin
result :=FJSon.S['name'];
end;
function TDJsonNode.GetValue: variant;
begin
result :=FJSon.Values['value'].VariantValue;
end;
procedure TDJsonNode.SetAttributes(Name, AValue: string);
begin
FJSon.S[LowerCase(Name)] :=AValue;
end;
procedure TDJsonNode.SetData(AValue: string);
begin
FJSon.Values['cdata'].Value :=AValue;
end;
procedure TDJsonNode.SetNodeName(AValue: string);
begin
FJSon.S['name'] :=AValue;
end;
procedure TDJsonNode.SetValue(AValue: variant);
begin
FJSon.Values['value'].VariantValue :=AValue;
end;
end.