Einzelnen Beitrag anzeigen

mytbo

Registriert seit: 8. Jan 2007
472 Beiträge
 
#4

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

  Alt 19. Sep 2023, 00:12
Das Ganze scheint zu funktionieren, aber irgendwie fühlt sich das für mich noch nicht richtig an.
Lösung mit mORMot. Wenn die Eigenschaftsnamen in der JSON-Datei nach den Regeln für Pascal Feldnamen benannt sind, lässt es sich automatisch mappen:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.text,
  mormot.core.json,
  mormot.core.os;

type
  TDBSettings = packed record
    DataBase: record
      DBServerName: String; // statt "DB-ServerName"
      DBName: String;
      DBUser: String;
      DBUserPw: Integer;
      DBProvider: Integer;
    end;
    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;
Im Beispiel ist das nicht der Fall, es muss ein Custom-Serializer registriert werden:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.data,
  mormot.core.text,
  mormot.core.json,
  mormot.core.rtti,
  mormot.core.os;

type
  TDBSettingDataBase = class(TObject)
  private
    FDBServerName: String;
    FDBName: String;
    FDBUser: String;
    FDBUserPw: Integer;
    FDBProvider: Integer;
  protected
    class procedure ReadDataBaseJson(var pmvContext: TJsonParserContext; pmValue: TObject);
    class procedure WriteDataBaseJson(pmWriter: TJsonWriter; pmValue: TObject; pmOptions: TTextWriterWriteObjectOptions);
  public
    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;

class procedure TDBSettingDataBase.ReadDataBaseJson(var pmvContext: TJsonParserContext; pmValue: TObject);
var
  values: array[0..4] of TValuePUtf8Char;
begin
  if pmvContext.ParseObject(['DB-ServerName', 'DB-Name', 'DB-User', 'DB-User-Pw', 'DB-Provider'], @values) then
  begin
    with TDBSettingDataBase(pmValue) do
    begin
      DBServerName := values[0].ToString;
      DBName := values[1].ToString;
      DBUser := values[2].ToString;
      DBUserPw := values[3].ToInteger;
      DBProvider := values[4].ToInteger;
    end;
  end;
end;

class procedure TDBSettingDataBase.WriteDataBaseJson(pmWriter: TJsonWriter; pmValue: TObject; pmOptions: TTextWriterWriteObjectOptions);
begin
  with TDBSettingDataBase(pmValue) do
    pmWriter.AddJsonEscape(['DB-ServerName', DBServerName, 'DB-Name', DBName, 'DB-User', DBUser, 'DB-User-Pw', DBUserPw, 'DB-Provider', DBProvider]);
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
  TRttiJson.RegisterCustomSerializerClass(TDBSettingDataBase, TDBSettingDataBase.ReadDataBaseJson, TDBSettingDataBase.WriteDataBaseJson);
Und so anwenden:
Delphi-Quellcode:
with TDBSettings.Create do
try
  LoadFromFile('settings.json');
  ShowMessage(DataBase.DBServerName);

  DataBase.DBServerName := 'TestServer';
  SaveToFile('settings.json');
finally
  Free;
end;
Bis bald...
Thomas
  Mit Zitat antworten Zitat