forked from Memnarch/Delphinus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN.JSonFile.pas
257 lines (222 loc) · 7.34 KB
/
DN.JSonFile.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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.JSonFile;
interface
uses
DN.JSon;
type
TJSonFile = class
protected
procedure Load(const ARoot: TJSONObject); virtual;
procedure Save(const ARoot: TJSONObject); virtual;
procedure WriteString(AParent: TJSONObject; const AProperty, AContent: string);
procedure WritePath(AParent: TJSonObject; const AProperty: string; const AContent: string);
procedure WriteInteger(AParent: TJSONObject; const AProperty: string; AContent: Integer);
procedure WriteBoolean(AParent: TJSONObject; const AProperty: string; AValue: Boolean);
procedure WriteFloat(AParent: TJSonObject; const AProperty: string; AValue: Single);
function WriteArray(AParent: TJSONObject; const AArrayName: string): TJSONArray;
function WriteObject(AParent: TJSONObject; const AObjectName: string): TJSONObject;
function WriteArrayObject(AParent: TJSONArray): TJSONObject;
function ReadString(AParent: TJSONObject; const AProperty: string; const ADefault: string = ''): string;
function ReadInteger(AParent: TJSONObject; const AProperty: string; const ADefault: Integer = 0): Integer;
function ReadFloat(AParent: TJSonObject; const AProperty: string; const ADefault: Single = 0): Single;
function ReadBoolean(AParent: TJSonObject; const AProperty: string; ADefault: Boolean = False): Boolean;
function ReadObject(AParent: TJSONObject; const AProperty: string; var AObject: TJSonObject): Boolean;
function ReadArray(AParent: TJSonObject; const AProperty: string; var AArray: TJSONArray): Boolean;
function ReadJSOnValue(AParent: TJSOnObject; const AProperty: string; var AValue: TJSonValue): Boolean;
function EscapeQuotes(const AString: string): string;
function UnescapeQuotes(const AString: string): string;
public
function LoadFromFile(const AFileName: string): Boolean;
procedure SaveToFile(const AFileName: string);
function LoadFromString(const AText: string): Boolean;
function ToString: string; override;
end;
implementation
uses
Classes,
SysUtils;
var
GJSonFormatSettings: TFormatSettings;
{ TJSonFile }
function TJSonFile.EscapeQuotes(const AString: string): string;
begin
Result := StringReplace(AString, '"', '\"', [rfReplaceAll]);
end;
procedure TJSonFile.Load(const ARoot: TJSONObject);
begin
end;
function TJSonFile.LoadFromFile(const AFileName: string): Boolean;
var
LData: TStringStream;
begin
LData := TStringStream.Create();
try
LData.LoadFromFile(AFileName);
Result := LoadFromString(LData.DataString);
finally
LData.Free;
end;
end;
function TJSonFile.LoadFromString(const AText: string): Boolean;
var
LRoot: TJSONObject;
begin
LRoot := TJSONObject.ParseJSONValue(AText) as TJSonObject;
Result := Assigned(LRoot);
if Result then
begin
try
Load(LRoot);
finally
LRoot.Free;
end;
end;
end;
function TJSonFile.ReadArray(AParent: TJSonObject; const AProperty: string;
var AArray: TJSONArray): Boolean;
var
LValue: TJSONValue;
begin
Result := ReadJSOnValue(AParent, AProperty, LValue) and (LValue is TJSONArray);
AArray := TJSONArray(LValue);
end;
function TJSonFile.ReadBoolean(AParent: TJSonObject; const AProperty: string;
ADefault: Boolean): Boolean;
var
LValue: TJSONValue;
begin
Result := ADefault;
if ReadJSOnValue(AParent, AProperty, LValue) then
if LValue is TJSONTrue then
Result := True
else if LValue is TJSONFalse then
Result := False;
end;
function TJSonFile.ReadFloat(AParent: TJSonObject; const AProperty: string;
const ADefault: Single): Single;
begin
if not TryStrToFloat(ReadString(AParent, AProperty), Result, GJSonFormatSettings) then
Result := ADefault;
end;
function TJSonFile.ReadInteger(AParent: TJSONObject; const AProperty: string;
const ADefault: Integer): Integer;
begin
Result := StrToInt(ReadString(AParent, AProperty, IntToStr(ADefault)));
end;
function TJSonFile.ReadJSOnValue(AParent: TJSOnObject; const AProperty: string;
var AValue: TJSonValue): Boolean;
begin
AValue := AParent.GetValue(AProperty);
Result := Assigned(AValue);
end;
function TJSonFile.ReadObject(AParent: TJSONObject; const AProperty: string;
var AObject: TJSonObject): Boolean;
var
LValue: TJSONValue;
begin
Result := ReadJSOnValue(AParent, AProperty, LValue) and (LValue is TJSonObject);
AObject := TJSONObject(LValue);
end;
function TJSonFile.ReadString(AParent: TJSONObject; const AProperty,
ADefault: string): string;
var
LValue: TJSONValue;
begin
if ReadJSOnValue(AParent, AProperty, LValue) then
Result := LValue.Value
else
Result := ADefault;
{$if CompilerVersion < 23}
Result := UnescapeQuotes(Result);
{$IfEnd}
end;
procedure TJSonFile.Save(const ARoot: TJSONObject);
begin
end;
procedure TJSonFile.SaveToFile(const AFileName: string);
var
LData: TStringStream;
begin
LData := TStringStream.Create();
try
LData.WriteString(ToString);
LData.SaveToFile(AFileName);
finally
LData.Free;
end;
end;
function TJSonFile.ToString: string;
var
LRoot: TJSONObject;
begin
LRoot := TJSONObject.Create();
try
Save(LRoot);
Result := LRoot.ToString;
finally
LRoot.Free;
end;
end;
function TJSonFile.UnescapeQuotes(const AString: string): string;
begin
Result := StringReplace(AString, '\"', '"', [rfReplaceAll]);
end;
function TJSonFile.WriteArray(AParent: TJSONObject;
const AArrayName: string): TJSONArray;
begin
Result := TJSONArray.Create();
AParent.AddPair(AArrayName, Result);
end;
function TJSonFile.WriteArrayObject(AParent: TJSONArray): TJSONObject;
begin
Result := TJSONObject.Create();
AParent.AddElement(Result);
end;
procedure TJSonFile.WriteBoolean(AParent: TJSONObject; const AProperty: string;
AValue: Boolean);
begin
if AValue then
AParent.AddPair(AProperty, TJSONTrue.Create())
else
AParent.AddPair(AProperty, TJSONFalse.Create())
end;
procedure TJSonFile.WriteFloat(AParent: TJSonObject; const AProperty: string;
AValue: Single);
begin
WriteString(AParent, AProperty, FloatToStr(AValue));
end;
procedure TJSonFile.WriteInteger(AParent: TJSONObject; const AProperty: string;
AContent: Integer);
begin
WriteString(AParent, AProperty, IntToStr(AContent));
end;
function TJSonFile.WriteObject(AParent: TJSONObject;
const AObjectName: string): TJSONObject;
begin
Result := TJSONObject.Create();
AParent.AddPair(AObjectName, Result);
end;
procedure TJSonFile.WritePath(AParent: TJSonObject; const AProperty,
AContent: string);
begin
WriteString(AParent, AProperty, StringReplace(AContent, '\', '\\', [rfReplaceAll]));
end;
procedure TJSonFile.WriteString(AParent: TJSONObject; const AProperty,
AContent: string);
begin
{$if CompilerVersion < 23}
AParent.AddPair(AProperty, EscapeQuotes(AContent));
{$Else}
AParent.AddPair(AProperty, TJSONString.Create(AContent));
{$IfEnd}
end;
initialization
GJSonFormatSettings := TFormatSettings.Create();
GJSonFormatSettings.DecimalSeparator := '.';
end.