-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfpc_seralizeadapter.pas
More file actions
505 lines (416 loc) · 9.92 KB
/
fpc_seralizeadapter.pas
File metadata and controls
505 lines (416 loc) · 9.92 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
unit fpc_seralizeadapter;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, variants,
fpjson, dbugintf,Laz_XMLRead, Laz2_DOM, Laz_XMLWrite,
intf_seralizeadapter;
type
{ TFPCXmlNode }
TFPCXmlNode=class(TInterfacedObject,IDataNode)
function GetAttributes(Name: string): string;
function GetChildItem(Index:integer): IDataNode;
function GetNodeName: string;
function GetValue: variant;
procedure SetAttributes(Name: string; AValue: string);
procedure SetNodeName(AValue: string);
procedure SetValue(AValue: variant);
function AddChild():IDataNode;
function ChildCount:integer;
procedure SetData(AValue: string);
function AddPropObj(const Name: string): IDataNode;
function PropObjByName(const Name:string): IDataNode;
function GetData: string;
function GetDumpText:string;
protected
fDoc:TDOMDocument;
fNode:TDOMNode;
function BuilDataNode(const Node:TDOMNode):IDataNode;
end;
{TFPCJsonNode=class(TInterfacedObject,IDataNode)
end;}
{ TFPCXmlAdapter }
TFPCXmlAdapter=class(TInterfacedObject,IDataAdapter)
function NewDoc:IDataNode;
function GetRootNode:IDataNode;
procedure LoadFromFile(const Filename:string);
procedure SaveToFile(const FileName:string);
function GetSeralzieString: string;
private
fDoc:TXMLDocument;
public
destructor Destroy;override;
end;
TFPCJsonNode=class(TInterfacedObject,IDataNode)
function GetAttributes(Name: string): string;
function GetChildItem(Index:integer): IDataNode;
function GetNodeName: string;
function GetValue: variant;
procedure SetAttributes(Name: string; AValue: string);
procedure SetNodeName(AValue: string);
procedure SetValue(AValue: variant);
function AddChild():IDataNode;
function ChildCount:integer;
procedure SetData(AValue: string);
function GetData: string;
function AddPropObj(const Name: string): IDataNode;
function PropObjByName(const Name:string): IDataNode;
function GetDumpText:string;
protected
fDoc:TJsonObject;
fNode:TJSOnData;
FParent:TJsonObject;
function BuilDataNode(const Node:TJsonData):IDataNode;
end;
{ TFPCJsonAdapter }
TFPCJsonAdapter=class(TInterfacedObject,IDataAdapter)
function NewDoc:IDataNode;
function GetRootNode:IDataNode;
procedure LoadFromFile(const Filename:string);
procedure SaveToFile(const FileName:string);
function GetSeralzieString: string;
private
fDoc:TJsonObject;
public
destructor Destroy;override;
end;
implementation
{ TFPCJsonNode }
function TFPCJsonNode.GetAttributes(Name: string): string;
begin
if FNode is TJSonData then
begin
result :=(FNode as TJsonObject).Get(LowerCase(Name));
end else
begin
assert(false,'fnode is not a object');
end;
end;
function TFPCJsonNode.GetChildItem(Index: integer): IDataNode;
var
Item:TJsonObject;
Count:integer;
JObj:TJsonObject;
Arr:TJSONArray;
begin
JObj :=FNode as TJsonObject;
Arr :=JObj.Arrays['ITEMS'];
Item :=Arr.Objects[Index];
result :=BuilDataNode(Item);
end;
function TFPCJsonNode.GetNodeName: string;
var
Index:integer;
begin
Index :=FParent.IndexOf(FNode);
result :=FParent.Names[Index];
end;
function TFPCJsonNode.GetValue: variant;
begin
result :=FNode.Value;
end;
procedure TFPCJsonNode.SetAttributes(Name: string; AValue: string);
var
JD:TJSOnData;
Jobj:TJsonObject;
begin
if FNode is TJsonObject then
begin
JObj :=FNode as TJsonObject;
JD :=Jobj.Find(LowerCase(Name));
if not Assigned(JD)then
begin
(FNode as TJsonObject).Add(LowerCase(Name),AValue);
end else
begin
jd.AsString:=AValue;
end;
end;
end;
procedure TFPCJsonNode.SetNodeName(AValue: string);
begin
(FNode as TJsonObject).Find('name').Value :=AVAlue;
end;
procedure TFPCJsonNode.SetValue(AValue: variant);
begin
FNode.Value:=AValue;
end;
function TFPCJsonNode.AddChild(): IDataNode;
var
JObj:TJsonObject;
Child:TJsonObject;
JNode:TFPCJsonNode;
JArr:TJSONArray;
begin
JObj :=FNode as TJsonObject;
if not JObj.Find('ITEMS',JArr) then
begin
JArr :=TJSONArray.Create;
JObj.Add('ITEMS',JArr);
end;
Child :=TJsonObject.Create();
JArr.Add(Child);
result :=BuilDataNode(Child);
end;
function TFPCJsonNode.ChildCount: integer;
var
JObj:TJsonObject;
Arr:TJSONArray;
begin
JObj :=fNode as TJsonObject;
Arr :=Jobj.Arrays['ITEMS'];
result :=Arr.Count;
end;
procedure TFPCJsonNode.SetData(AValue: string);
begin
(FNode as TJSonObject).Add('cdata',AValue);
end;
function TFPCJsonNode.GetData: string;
begin
result :=(FNode as TJsonObject).Get('cdata');
end;
function TFPCJsonNode.AddPropObj(const Name: string): IDataNode;
var
JObj,Child:TJsonObject;
begin
JObj :=FNode as TJsonObject;
Child :=TJsonObject.Create;
Jobj.Add(Name,Child);
Result :=self.BuilDataNode(child);
end;
function TFPCJsonNode.PropObjByName(const Name: string): IDataNode;
var
JObj,PropObj :TJsonObject;
begin
JObj :=FNode as TJsonObject;
if JObj.Find(LowerCase(Name),PropObj) then
begin
result :=BuilDataNode(PropObj);
end else
begin
result :=nil;
end;
end;
function TFPCJsonNode.GetDumpText: string;
begin
result :=FNode.AsJSON;
end;
function TFPCJsonNode.BuilDataNode(const Node: TJsonData): IDataNode;
var
JNode:TFPCJsonNode;
begin
JNode :=TFPCJsonNode.Create;
JNode.fDoc :=self.fDoc;
JNode.fNode :=Node;
JNode.FParent :=FNode as TJsonObject;
result :=JNode;
end;
{ TFPCJsonAdapter }
function TFPCJsonAdapter.NewDoc: IDataNode;
begin
Fdoc :=GetJson('{JSONOBJECT:{}}') as TJsonObject;
end;
function TFPCJsonAdapter.GetRootNode: IDataNode;
var
Node:TFPCJsonNode;
JObj:TJsonObject;
begin
JObj :=FDoc.Find('JSONOBJECT') as TJsonObject;
Node :=TFPCJsonNode.Create;
Node.fNode :=JObj;
Node.FParent :=FDoc;
Node.fDoc :=fDOc;
result :=Node;
end;
procedure TFPCJsonAdapter.LoadFromFile(const Filename: string);
var
FS:TFileStream;
begin
FS :=TFileStream.Create(FileName,fmopenRead);
try
FS.Position:=0;
if not Assigned(Fdoc) then FreeAndNil(FDoc);
FDoc :=GetJson(FS) as TJsonObject;
finally
FreeAndNil(FS);
end;
end;
procedure TFPCJsonAdapter.SaveToFile(const FileName: string);
var
FS:TFileStream;
begin
FS :=TFileStream.Create(FileName,fmcreate);
try
//FDoc.FormatJSON([foUseTabchar,foSkipWhiteSpace]);
FDoc.FormatJSON(DefaultFormat);
Fdoc.DumpJSON(FS);
finally
FreeAndNil(FS);
end;
end;
function TFPCJsonAdapter.GetSeralzieString: string;
begin
result :=fdoc.AsString;
end;
destructor TFPCJsonAdapter.Destroy;
begin
if Assigned(FDoc) then
FreeAndNil(FDoc);
inherited ;
end;
{ TFPCXmlNode }
function TFPCXmlNode.BuilDataNode(const Node: TDOMNode): IDataNode;
var
xmlNode:TFPCXmlNode;
begin
xmlNode :=TFpcXmlNode.Create;
xmlNode.fDoc :=self.fDoc;
xmlnode.fNode :=Node;
result :=xmlNode;
end;
function TFPCXmlNode.GetAttributes(Name: string): string;
begin
result :=fNode.Attributes.GetNamedItem(Name).NodeValue;
end;
function TFPCXmlNode.GetChildItem(Index: integer): IDataNode;
var
Node:TDOMNode;
begin
//result :=fNode.ChildNodes[Index];
Node :=fNode.ChildNodes[Index];
result :=self.BuilDataNode(Node);
end;
function TFPCXmlNode.GetNodeName: string;
begin
result :=fNode.NodeName;
end;
function TFPCXmlNode.GetValue: variant;
begin
result :=fNode.NodeValue;
end;
procedure TFPCXmlNode.SetAttributes(Name: string; AValue: string);
var
attr:TDOMNode;
begin
attr :=FNode.Attributes.GetNamedItem(Name);
if Assigned(Attr) then
begin
attr.NodeValue:=AValue;
end else
begin
if fNode is TDOMElement then
begin
(fNode as TDOMElement).SetAttribute(Name,AValue);
end else
begin
assert(false,'this nod is not a element node');
end;
end;
end;
procedure TFPCXmlNode.SetNodeName(AValue: string);
begin
//
end;
procedure TFPCXmlNode.SetValue(AValue: variant);
begin
fNode.NodeValue :=VarTostr(AValue);
end;
function TFPCXmlNode.AddChild(): IDataNode;
var
Ele:TDomElement;
begin
Ele :=FDoc.CreateElement('ITEM');
fNode.AppendChild(Ele);
result :=self.BuilDataNode(Ele);
end;
function TFPCXmlNode.ChildCount: integer;
begin
result :=FNode.ChildNodes.Count;
end;
procedure TFPCXmlNode.SetData(AValue: string);
var
xmlNode :TFPCXmlNode;
CData:TDOMCDataSection;
begin
CData :=fDoc.CreateCDATASection(AVAlue);
fNode.AppendChild(CData);
end;
function TFPCXmlNode.AddPropObj(const Name: string): IDataNode;
var
Ele:TDOMElement;
begin
Ele :=FDoc.CreateElement(Name);
FNode.AppendChild(Ele);
Result :=self.BuilDataNode(Ele);
end;
function TFPCXmlNode.PropObjByName(const Name: string): IDataNode;
var
Node:TDOMNode;
begin
{JObj :=FJSon.O[Name];
if Assigned(JObj) then
begin
result :=BuildDataNode(JObj)
end else
begin
result :=nil;
end; }
Node :=FDoc.FindNode(Name);
if Assigned(Node) then
begin
result :=self.BuilDataNode(Node);
end else
begin
result :=nil;
end;
end;
function TFPCXmlNode.GetData: string;
begin
result :=FNode.FindNode('#cdata-section').NodeValue;
end;
function TFPCXmlNode.GetDumpText: string;
begin
result :=self.fNode.TextContent;
end;
{ TFPCXmlAdapter }
destructor TFPCXmlAdapter.Destroy;
begin
if Assigned(fDoc) then
FreeAndNil(fDoc);
inherited ;
end;
function TFPCXmlAdapter.NewDoc: IDataNode;
var
Element:TDomElement;
begin
if Assigned(fDoc) then
begin
FreeAndNil(fDoc);
end;
fDoc :=TXMLDocument.Create;
Element :=fdoc.CreateElement('XMLObject');
Fdoc.AppendChild(Element);
end;
function TFPCXmlAdapter.GetRootNode: IDataNode;
var
xmlNode:TFPCXmlNode;
begin
xmlNode :=TFPCXmlNode.Create;
xmlNode.fNode :=fDoc.FirstChild;
xmlNode.fDoc :=fDoc;
result :=xmlNode;
end;
procedure TFPCXmlAdapter.LoadFromFile(const Filename: string);
begin
//
ReadXMLFile(fDoc,FileName);
end;
procedure TFPCXmlAdapter.SaveToFile(const FileName: string);
begin
WriteXML(fDoc,FileName);
end;
function TFPCXmlAdapter.GetSeralzieString: string;
begin
WriteXML(FDoc,result);
end;
end.