Einzelnen Beitrag anzeigen

mytbo

Registriert seit: 8. Jan 2007
458 Beiträge
 
#9

AW: Json Array of String: Wie Array-Items per Schleife auslesen

  Alt 19. Sep 2023, 18:43
Um das Ganze abzuschließen, im konkreten Fall würde es reichen, die Eigenschaftsnamen für das Lesen und Schreiben der JSON-Datei zu ändern:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.data,
  mormot.core.text,
  mormot.core.json,
  mormot.core.rtti,
  mormot.core.unicode,
  mormot.core.variants,
  mormot.core.os;
  
type
  TDBSettingDataBase = class(TSynPersistent)
  private
    FDBServerName: String;
    FDBName: String;
    FDBUser: String;
    FDBUserPw: Integer;
    FDBProvider: Integer;
  published
    property DBServerName: String
      read FDBServerName write FDBServerName;
    property DBName: String
      read FDBName write FDBName;
    property DBUser: String
      read FDBUser write FDBUser;
    property DBUserPw: Integer
      read FDBUserPw write FDBUserPw;
    property DBProvider: Integer
      read FDBProvider write FDBProvider;
  end;
  
  TDBSettings = class(TSynAutoCreateFields)
  private
    FDataBase: TDBSettingDataBase;
    FShorthandSymbol: TStringDynArray;
  public
    function LoadFromFile(const pmcFileName: TFileName): Boolean;
    procedure SaveToFile(const pmcFileName: TFileName);
  published
    property DataBase: TDBSettingDataBase
      read FDataBase;
    property ShorthandSymbol: TStringDynArray
      read FShorthandSymbol;
  end;

function TDBSettings.LoadFromFile(const pmcFileName: TFileName): Boolean;
begin
  Result := ObjectLoadJson(Self, StringFromFile(pmcFileName));
end;

procedure TDBSettings.SaveToFile(const pmcFileName: TFileName);
begin
  FileFromString(ObjectToJson(Self), pmcFileName);
end;
  
initialization
  Rtti.ByClass[TDBSettingDataBase].Props.NameChanges(
    ['DBServerName', 'DBName', 'DBUser', 'DBUserPw', 'DBProvider'],
    ['DB-ServerName', 'DB-Name', 'DB-User', 'DB-User-Pw', 'DB-Provider']);
Als Ausführung mit Records wie folgt:
Delphi-Quellcode:
type
  TDBSettingDataBase = packed record
    DBServerName: String;
    DBName: String;
    DBUser: String;
    DBUserPw: Integer;
    DBProvider: Integer;
  end;

  TDBSettings = packed record
    DataBase: TDBSettingDataBase;
    ShorthandSymbol: TStringDynArray; // array of String
    function LoadFromFile(const pmcFileName: TFileName): Boolean;
    procedure SaveToFile(const pmcFileName: TFileName);
  end;

function TDBSettings.LoadFromFile(const pmcFileName: TFileName): Boolean;
begin
  Result := RecordLoadJson(Self, StringFromFile(pmcFileName), TypeInfo(TDBSettings));
end;

procedure TDBSettings.SaveToFile(const pmcFileName: TFileName);
begin
  FileFromString(RecordSaveJson(Self, TypeInfo(TDBSettings)), pmcFileName);
end;

initialization
  Rtti.ByTypeInfo[TypeInfo(TDBSettingDataBase)].Props.NameChanges(
    ['DBServerName', 'DBName', 'DBUser', 'DBUserPw', 'DBProvider'],
    ['DB-ServerName', 'DB-Name', 'DB-User', 'DB-User-Pw', 'DB-Provider']);
Anwendung so:
Delphi-Quellcode:
var
  rec: TDBSettings;
begin
  rec.LoadFromFile('settings.json');
  ShowMessage(rec.DataBase.DBServerName);

  rec.DataBase.DBServerName := 'TestServer';
  rec.SaveToFile('settings.json');
Bis bald...
Thomas

Geändert von mytbo (19. Sep 2023 um 18:53 Uhr) Grund: Tippfehler korrigiert
  Mit Zitat antworten Zitat