Einzelnen Beitrag anzeigen

Ghostwalker

Registriert seit: 16. Jun 2003
Ort: Schönwald
1.299 Beiträge
 
Delphi 10.3 Rio
 
#1

Superobject und Objekte

  Alt 2. Mai 2017, 05:02
moinmoin,

bin grad ein wenig am verzweifeln. Ich hab ein Objekt, das seine Daten in Form eines Records enthält

Delphi-Quellcode:
  TOptionsOpts = (ooOne,ooTwo,ooThree);
  TOptionsData = record
                   fval1 : widestring;
                   fval2 : integer;
                   fval3 : Double;
                   fval4 : TOptionsOpts;
  end;

  TOptions = class
    private
       fdata : TOptionsData;

    protected
    public
       procedure ClearData;
       Procedure ResetToDefault;
       Procedure SaveToJson(filename:WideString);
       Procedure LoadFromJson(filename:WideString);
    published
      Property Value1:WideString read fdata.fval1 write fdata.fval1;
      Property Value2:Integer read fdata.fval2 write fdata.fval2;
      Property Value3:Double read fdata.fval3 write fdata.fval3;
      Property value4:TOptionsOpts read fdata.fval4 write fdata.fval4;
  end;
Soweit so gut. Das Object soll seine Daten nun in eine Datei (JSON-Format) speichern und ggf. auch wieder laden.

Speichern:
Delphi-Quellcode:
procedure TOptions.SaveToJson(filename: WideString);
var
  ctx : TSuperRttiContext;
  obj : ISuperObject;
begin
  ctx := TSuperRttiContext.Create;
  obj := ctx.AsJson<TOptionsData>(fdata);
  obj.SaveTo(filename);
  ctx.free;
end;
Das funktioniert auch soweit ganz gut. Aber das laden:

Delphi-Quellcode:
procedure TOptions.LoadFromJson(filename: WideString);
var
  ctx : TSuperRttiContext;
  ws : Widestring;
  ss : TStringStream;
  fs : TFileStream;

begin
  fs := TFileStream.Create(filename,fmOpenRead);
  ss := TStringStream.Create;
  ss.LoadFromStream(fs);
  ws := ss.DataString;
  fs.Free;
  ss.Free;
  ctx := TSuperRttiContext.Create;
  fdata := ctx.AsType<TOptionsData>(SO(ws));
  ctx.Free;
end;
mag so garnicht. bei ctx.AsType<TOptionsdata> bekomm ich einen marshalling-error. Kann mir da jemand weiterhelfen ? Danke schonmal.
Uwe
e=mc² or energy = milk * coffee²
  Mit Zitat antworten Zitat