Skip to content

Commit a6dbee0

Browse files
committed
new TOptions and IOptions<TOptions>
1 parent fa9ec4a commit a6dbee0

File tree

3 files changed

+799
-0
lines changed

3 files changed

+799
-0
lines changed

Diff for: Quick.Options.Serializer.Json.pas

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{ ***************************************************************************
2+
3+
Copyright (c) 2015-2019 Kike Pérez
4+
5+
Unit : Quick.Options.Serializer.Json
6+
Description : Configuration groups Json Serializer
7+
Author : Kike Pérez
8+
Version : 1.0
9+
Created : 18/10/2019
10+
Modified : 22/10/2019
11+
12+
This file is part of QuickLib: https://github.com/exilon/QuickLib
13+
14+
***************************************************************************
15+
16+
Licensed under the Apache License, Version 2.0 (the "License");
17+
you may not use this file except in compliance with the License.
18+
You may obtain a copy of the License at
19+
20+
http://www.apache.org/licenses/LICENSE-2.0
21+
22+
Unless required by applicable law or agreed to in writing, software
23+
distributed under the License is distributed on an "AS IS" BASIS,
24+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
See the License for the specific language governing permissions and
26+
limitations under the License.
27+
28+
*************************************************************************** }
29+
30+
unit Quick.Options.Serializer.Json;
31+
32+
{$i QuickLib.inc}
33+
34+
interface
35+
36+
uses
37+
System.SysUtils,
38+
System.IOUtils,
39+
System.Generics.Collections,
40+
System.Json,
41+
Quick.Commons,
42+
Quick.JSON.Utils,
43+
Quick.Json.Serializer,
44+
Quick.Options;
45+
46+
type
47+
48+
TJsonOptionsSerializer = class(TOptionsSerializer)
49+
private
50+
fSerializer : TRTTIJson;
51+
public
52+
constructor Create;
53+
destructor Destroy; override;
54+
procedure Load(const aFilename : string; aSections : TSectionList); override;
55+
procedure Save(const aFilename : string; aSections : TSectionList); override;
56+
end;
57+
58+
implementation
59+
60+
{ TJsonOptionsSerializer }
61+
62+
constructor TJsonOptionsSerializer.Create;
63+
begin
64+
fSerializer := TRTTIJson.Create(TSerializeLevel.slPublishedProperty,True);
65+
end;
66+
67+
destructor TJsonOptionsSerializer.Destroy;
68+
begin
69+
fSerializer.Free;
70+
inherited;
71+
end;
72+
73+
procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
74+
var
75+
option : TOptions;
76+
fileoptions : string;
77+
json : TJsonObject;
78+
jpair : TJSONPair;
79+
begin
80+
if FileExists(aFilename) then
81+
begin
82+
//read option file
83+
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
84+
json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject;
85+
for option in aSections do
86+
begin
87+
jpair := fSerializer.GetJsonPairByName(json,option.Name);
88+
if jpair = nil then raise Exception.CreateFmt('Config section "%s" not found',[option.Name]);
89+
if jpair.JsonValue <> nil then
90+
begin
91+
//deserialize option
92+
fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
93+
//validate loaded configuration
94+
option.ValidateOptions;
95+
end;
96+
end;
97+
end;
98+
end;
99+
100+
procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
101+
var
102+
option : TOptions;
103+
fileoptions : string;
104+
json : TJSONObject;
105+
jpair : TJSONPair;
106+
begin
107+
json := TJSONObject.Create;
108+
try
109+
for option in aSections do
110+
begin
111+
//validate configuration before save
112+
option.ValidateOptions;
113+
//serialize option
114+
jpair := fSerializer.Serialize(option.Name,option);
115+
json.AddPair(jpair);
116+
end;
117+
fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
118+
TFile.WriteAllText(aFilename,fileoptions);
119+
finally
120+
json.Free;
121+
end;
122+
end;
123+
124+
end.

Diff for: Quick.Options.Serializer.Yaml.pas

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{ ***************************************************************************
2+
3+
Copyright (c) 2015-2019 Kike Pérez
4+
5+
Unit : Quick.Options.Serializer.Yaml
6+
Description : Configuration groups Yaml Serializer
7+
Author : Kike Pérez
8+
Version : 1.0
9+
Created : 18/10/2019
10+
Modified : 22/10/2019
11+
12+
This file is part of QuickLib: https://github.com/exilon/QuickLib
13+
14+
***************************************************************************
15+
16+
Licensed under the Apache License, Version 2.0 (the "License");
17+
you may not use this file except in compliance with the License.
18+
You may obtain a copy of the License at
19+
20+
http://www.apache.org/licenses/LICENSE-2.0
21+
22+
Unless required by applicable law or agreed to in writing, software
23+
distributed under the License is distributed on an "AS IS" BASIS,
24+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
See the License for the specific language governing permissions and
26+
limitations under the License.
27+
28+
*************************************************************************** }
29+
30+
unit Quick.Options.Serializer.Yaml;
31+
32+
{$i QuickLib.inc}
33+
34+
interface
35+
36+
uses
37+
System.SysUtils,
38+
System.IOUtils,
39+
System.Generics.Collections,
40+
Quick.YAML,
41+
Quick.Commons,
42+
Quick.YAML.Serializer,
43+
Quick.Options;
44+
45+
type
46+
47+
TYamlOptionsSerializer = class(TOptionsSerializer)
48+
private
49+
fSerializer : TRTTIYaml;
50+
public
51+
constructor Create;
52+
destructor Destroy; override;
53+
procedure Load(const aFilename : string; aSections : TSectionList); override;
54+
procedure Save(const aFilename : string; aSections : TSectionList); override;
55+
end;
56+
57+
implementation
58+
59+
{ TYamlOptionsSerializer }
60+
61+
constructor TYamlOptionsSerializer.Create;
62+
begin
63+
fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
64+
end;
65+
66+
destructor TYamlOptionsSerializer.Destroy;
67+
begin
68+
fSerializer.Free;
69+
inherited;
70+
end;
71+
72+
procedure TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
73+
var
74+
option : TOptions;
75+
fileoptions : string;
76+
json : TYamlObject;
77+
jpair : TYamlPair;
78+
begin
79+
if FileExists(aFilename) then
80+
begin
81+
//read option file
82+
fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
83+
json := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
84+
for option in aSections do
85+
begin
86+
jpair := fSerializer.GetYamlPairByName(json,option.Name);
87+
if jpair.Value <> nil then
88+
begin
89+
//deserialize option
90+
fSerializer.DeserializeObject(option,jpair.Value as TYamlObject);
91+
//validate loaded configuration
92+
option.ValidateOptions;
93+
end;
94+
end;
95+
end;
96+
end;
97+
98+
procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
99+
var
100+
option : TOptions;
101+
fileoptions : string;
102+
yaml : TYamlObject;
103+
jpair : TYamlPair;
104+
begin
105+
yaml := TYamlObject.Create;
106+
try
107+
for option in aSections do
108+
begin
109+
//validate configuration before save
110+
option.ValidateOptions;
111+
//serialize option
112+
jpair := fSerializer.Serialize(option.Name,option);
113+
yaml.AddPair(jpair);
114+
end;
115+
fileoptions := yaml.ToYaml;
116+
TFile.WriteAllText(aFilename,fileoptions);
117+
finally
118+
yaml.Free;
119+
end;
120+
end;
121+
122+
end.
123+

0 commit comments

Comments
 (0)