Diesen Ansatz mit dem JSON-Class-Mapping kann man auch ohne externe Bibliothek nur mit Delphi-Bordmitteln realisieren:
Delphi-Quellcode:
interface
uses
System.Types,
REST.Json.Types;
// wegen des Attributs
type
TDBSettings =
class
type
TDataBase =
class
private
[JSONName('
DB-Name')]
FDBName:
String;
[JSONName('
DB-Provider')]
FDBProvider: Integer;
FDBServerName:
String;
[JSONName('
DB-User')]
FDBUser:
String;
[JSONName('
DB-User-Pw')]
FDBUserPw: Integer;
public
property DBName:
String read FDBName
write FDBName;
property DBProvider: Integer
read FDBProvider
write FDBProvider;
property DBServerName:
String read FDBServerName
write FDBServerName;
property DBUser:
String read FDBUser
write FDBUser;
property DBUserPw: Integer
read FDBUserPw
write FDBUserPw;
end;
private
FDatabase: TDataBase;
public
ShorthandSymbol: TStringDynArray;
constructor Create;
destructor Destroy;
override;
class function LoadFromFile(
const pmcFileName:
string): TDBSettings;
procedure SaveToFile(
const pmcFileName:
string);
property Database: TDataBase
read FDatabase;
end;
implementation
constructor TDBSettings.Create;
begin
inherited Create;
FDatabase := TDataBase.Create();
end;
destructor TDBSettings.Destroy;
begin
FDatabase.Free;
inherited Destroy;
end;
In der Anwendung sieht das dann in etwa so aus:
Delphi-Quellcode:
uses
System.IOUtils, System.SysUtils,
REST.Json;
const
cFileName = 'c:\temp\settings.json';
begin
var settings := TJson.JsonToObject<TDBSettings>(string.Join(sLineBreak, cArr));
try
TFile.WriteAllText(cFileName, TJson.ObjectToJsonString(settings));
finally
settings.Free;
end;
begin
var settings := TJson.JsonToObject<TDBSettings>(TFile.ReadAllText(cFileName));
try
...
finally
settings.Free;
end;
end;
Nebenbei: Das StringArray als Feld anstatt Property ist zumindest zu hinterfragen.