1
1
{ ***************************************************************************
2
2
3
- Copyright (c) 2015-2019 Kike Pérez
3
+ Copyright (c) 2015-2020 Kike Pérez
4
4
5
5
Unit : Quick.Options.Serializer.Json
6
6
Description : Configuration groups Json Serializer
7
7
Author : Kike Pérez
8
8
Version : 1.0
9
9
Created : 18/10/2019
10
- Modified : 28/11/2019
10
+ Modified : 07/02/2020
11
11
12
12
This file is part of QuickLib: https://github.com/exilon/QuickLib
13
13
@@ -48,11 +48,13 @@ interface
48
48
TJsonOptionsSerializer = class (TOptionsSerializer)
49
49
private
50
50
fSerializer : TRTTIJson;
51
+ function ParseFile (const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
51
52
public
52
53
constructor Create;
53
54
destructor Destroy; override;
54
55
function Load (const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
55
56
procedure Save (const aFilename : string; aSections : TSectionList); override;
57
+ function GetFileSectionNames (const aFilename : string; out oSections : TArray<string>) : Boolean; override;
56
58
end ;
57
59
58
60
implementation
@@ -70,6 +72,41 @@ destructor TJsonOptionsSerializer.Destroy;
70
72
inherited ;
71
73
end ;
72
74
75
+ function TJsonOptionsSerializer.GetFileSectionNames (const aFilename: string; out oSections: TArray<string>): Boolean;
76
+ var
77
+ json : TJsonObject;
78
+ jpair : TJSONPair;
79
+ i : Integer;
80
+ begin
81
+ Result := False;
82
+ json := nil ;
83
+ if ParseFile(aFilename,json) then
84
+ begin
85
+ try
86
+ for i := 0 to json.Count - 1 do
87
+ begin
88
+ oSections := oSections + [json.Pairs[i].JsonString.Value ];
89
+ end ;
90
+ Result := True;
91
+ finally
92
+ json.Free;
93
+ end ;
94
+ end ;
95
+ end ;
96
+
97
+ function TJsonOptionsSerializer.ParseFile (const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
98
+ var
99
+ fileoptions : string;
100
+ begin
101
+ aJsonObj := nil ;
102
+ if FileExists(aFilename) then
103
+ begin
104
+ fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
105
+ aJsonObj := TJsonObject.ParseJSONValue(fileoptions) as TJsonObject;
106
+ end ;
107
+ Result := aJsonObj <> nil ;
108
+ end ;
109
+
73
110
function TJsonOptionsSerializer.Load (const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
74
111
var
75
112
option : TOptions;
@@ -78,26 +115,28 @@ function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSect
78
115
jpair : TJSONPair;
79
116
begin
80
117
Result := False;
81
- if FileExists (aFilename) then
118
+ if ParseFile (aFilename,json ) then
82
119
begin
83
- // read option file
84
- fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
85
- json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject;
86
- for option in aSections do
87
- begin
88
- jpair := fSerializer.GetJsonPairByName(json,option.Name );
89
- if jpair = nil then
90
- begin
91
- if aFailOnSectionNotExists then raise Exception.CreateFmt(' Config section "%s" not found' ,[option.Name ])
92
- else Continue;
93
- end ;
94
- if jpair.JsonValue <> nil then
120
+ try
121
+ for option in aSections do
95
122
begin
96
- // deserialize option
97
- fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
98
- // validate loaded configuration
99
- option.ValidateOptions;
123
+ jpair := fSerializer.GetJsonPairByName(json,option.Name );
124
+ if jpair = nil then
125
+ begin
126
+ if aFailOnSectionNotExists then raise Exception.CreateFmt(' Config section "%s" not found' ,[option.Name ])
127
+ else Continue;
128
+ end ;
129
+ if jpair.JsonValue <> nil then
130
+ begin
131
+ // deserialize option
132
+ fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
133
+ // validate loaded configuration
134
+ option.ValidateOptions;
135
+ end ;
100
136
end ;
137
+ Result := True;
138
+ finally
139
+ json.Free;
101
140
end ;
102
141
end ;
103
142
end ;
@@ -113,14 +152,17 @@ procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSec
113
152
try
114
153
for option in aSections do
115
154
begin
116
- // validate configuration before save
117
- option.ValidateOptions;
118
- // serialize option
119
- jpair := fSerializer.Serialize(option.Name ,option);
120
- json.AddPair(jpair);
155
+ if not option.HideOptions then
156
+ begin
157
+ // validate configuration before save
158
+ option.ValidateOptions;
159
+ // serialize option
160
+ jpair := fSerializer.Serialize(option.Name ,option);
161
+ json.AddPair(jpair);
162
+ end ;
121
163
end ;
122
164
fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
123
- TFile.WriteAllText(aFilename,fileoptions);
165
+ if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
124
166
finally
125
167
json.Free;
126
168
end ;
0 commit comments