![]() |
Superobject und Objekte
moinmoin,
bin grad ein wenig am verzweifeln. Ich hab ein Objekt, das seine Daten in Form eines Records enthält
Delphi-Quellcode:
Soweit so gut. Das Object soll seine Daten nun in eine Datei (JSON-Format) speichern und ggf. auch wieder laden.
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; Speichern:
Delphi-Quellcode:
Das funktioniert auch soweit ganz gut. Aber das laden:
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;
Delphi-Quellcode:
mag so garnicht. bei ctx.AsType<TOptionsdata> bekomm ich einen marshalling-error. Kann mir da jemand weiterhelfen ? Danke schonmal.
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; |
AW: Superobject und Objekte
Moin...:P
Warum steht ihr so auf Records. In Verbindung mit Klassen und Properties gibt es immer Probleme. Imho erst Recht mit Serialisieren. (Sichwort Beispiel: Der linken Seite kann nichts zugewiesen werden.) Versuch es mal hiermit:
Delphi-Quellcode:
type
TOptionsOpts = (ooOne, ooTwo, ooThree); TOptionsData = class private FVal1: WideString; FVal2: Integer; FVal3: Double; FVal4: TOptionsOpts; public property Val1: WideString read FVal1 write FVal1; property Val2: Integer read FVal2 write FVal2; property Val3: Double read FVal3 write FVal3; property Val4: TOptionsOpts read FVal4 write FVal4; end; TOptions = class private FData: TOptionsData; protected public procedure ClearData; procedure ResetToDefault; procedure SaveToJson(Filename: WideString); procedure LoadFromJson(Filename: WideString); published constructor Create; destructor Destroy; override; property Data: TOptionsData read FData write FData; end; implementation constructor TOptions.Create; begin FData := TOptionsData.Create; end; destructor TOptions.Destroy; begin FData.Free; inherited; end; ... |
AW: Superobject und Objekte
Zitat:
* Record ->Strukturtype zu reinen Speicherung von strukturierten Daten * Class -> Strukturtype für Objekte (Daten+Methoden zu deren Verarbeitung) Im Ursprungsversuch hatte ich die Eigenschaften ja eigentlich auch direkt in TOptions, was aber zum gleichen Problem führte. Zitat:
|
AW: Superobject und Objekte
Zitat:
Sind records nicht eher etwas für maschinennahe Spezialfälle, wenn man im Speicher ganz bestimmte Bytefolgen benötigt? |
AW: Superobject und Objekte
Nein, ist eine Frage der effizients. Eine Klasse hat nunmal wesentlich mehr Informationen zu speichern (auch von der internen Verwaltung her) als ein Record.
Schau dir einfach mal TObject an (davon wird jede Klasse abgeleitet). Dann wirst du sehen das z.B.
Delphi-Quellcode:
weniger Speicher verbrät als
Type
TPoint = record x,y : integer end;
Delphi-Quellcode:
Aber, zurück zum Thema, ändert das nichts am Problem.
Type
TPoint = class public x,y : integer; end; |
AW: Superobject und Objekte
Zitat:
Delphi-Quellcode:
und diversen Filler-Bytes.
packed
Records haben durchaus ihre Berechtigung in modernem Code. Sei es, weil man Instantiation-by-Declaration, Operator-Overloading oder Copy-On-Assign nutzen möchte. Mal eben den Inhalt eines Records lokal sichern und wieder herstellen geht in der Regel viel einfacher als bei Klassen. Dazu bieten sie (wie Klassen) auch Methoden und Eigenschaften. |
AW: Superobject und Objekte
Jap. Das unter anderem auch :)
Hat jetzt aber nix mit dem Problem zu tun. |
AW: Superobject und Objekte
ok...zurück zum Thema:
jemand noch eine Idee woran das liegen könnte ? |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:22 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz