forked from hydrobyte/TestJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTJ.LibSystemJSON.pas
More file actions
113 lines (95 loc) · 2.25 KB
/
TJ.LibSystemJSON.pas
File metadata and controls
113 lines (95 loc) · 2.25 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
unit TJ.LibSystemJSON;
interface
uses
System.Classes, System.IOUtils,
TJ.Lib,
System.JSON;
type
TLibSystemJSON = class (TInterfacedObject, ILib)
public
procedure AfterConstruction; override;
destructor Destroy; override;
procedure Add(const aKey, aValue: string);
function Count: Integer;
procedure Clear;
procedure Save(const aFileName: string);
procedure Load(const aFileName: string);
function Find(const aKey, aValue: string): Boolean;
procedure Parse;
function Check(const aCode: string): Boolean;
function ToString: string; override;
protected
fName: string;
fJson, fJsonClone: TJSONObject;
function fGetName: string;
end;
implementation
procedure TLibSystemJSON.AfterConstruction;
begin
inherited;
fName := 'System.JSON';
fJson := TJSONObject.Create;
fJsonClone := TJSONObject.Create;
end;
destructor TLibSystemJSON.Destroy;
begin
fJson.Free;
fJsonClone.Free;
inherited Destroy;
end;
procedure TLibSystemJSON.Add(const aKey, aValue: string);
begin
fJson.AddPair(aKey, aValue);
end;
function TLibSystemJSON.Count: Integer;
begin
Result := fJson.Count;
end;
procedure TLibSystemJSON.Save(const aFileName: string);
begin
TFile.WriteAllText(aFileName, fJson.ToJSON);
end;
procedure TLibSystemJSON.Clear;
begin
fJson.Free;
fJsonClone.Free;
fJson := TJSONObject.Create;
fJsonClone := TJSONObject.Create;
end;
procedure TLibSystemJSON.Load(const aFileName: string);
begin
fJson.Free;
fJson := fJson.ParseJSONValue(TFile.ReadAllText(aFileName)) as TJSONObject;
end;
function TLibSystemJSON.Find(const aKey, aValue: string): Boolean;
begin
Result := (fJson.GetValue(aKey).Value = aValue);
end;
procedure TLibSystemJSON.Parse;
begin
fJsonClone.Free;
fJsonClone := fJson.ParseJSONValue(fJson.ToJSON) as TJSONObject;
end;
function TLibSystemJSON.Check(const aCode: string): Boolean;
var
jTmp: TJSONValue;
begin
Result := False;
try
jTmp := fJson.ParseJSONValue(aCode);
Result := Assigned(jTmp);
finally
jTmp.Free;
end;
end;
function TLibSystemJSON.ToString: string;
begin
Result := fJson.ToJSON;
end;
function TLibSystemJSON.fGetName: string;
begin
Result := fName;
end;
initialization
RegisterTJLib('System.JSON', TLibSystemJSON);
end.