TDictionary:SaveToFileメソッド/LoadFromFileメソッドDelphiの辞書の保存/読み込み。 TDictionaryは直列化できませんか?
どのようなエレガントなソリューション! まず、すべてが期待どおりに実行されます。
コンテンツは、正しく見えるJSON形式のファイルに保存されます。 しかし、ファイルを再ロードした後、問題があります:
Type
TEnumCategTypes = (e_SQL1, e_VBA, e_Text);
TCategParams = class
fontStyles : TFontStyles;
rgbColor : COLORREF;
end;
TdictCategory = class (TDictionary<TEnumCategTypes, TCategParams>)
public
public class function LoadFromFile(const AFileName: string): TdictCategory;
public class procedure SaveToFile(const AFileName: string; dict: TdictCategory);
end;
implementation
class procedure TdictCategory.SaveToFile(const AFileName: string; dict: TdictCategory);
var
stream : TStringStream;
begin
try
stream := TStringStream.Create(TJson.ObjectToJsonString(dict)) ;
stream.SaveToFile(AFileName)
finally
stream.Free;
end;
end;
//---
class function TdictCategory.LoadFromFile(const AFileName: string): TdictCategory;
var
stream: TStringStream;
begin
stream := TStringStream.Create;
try
stream.LoadFromFile(AFileName);
result := TJson.JsonToObject<TdictCategory>(stream.DataString);
finally
stream.Free;
end;
end;
テストは、以下の通りです。すべての栄光が終わります。ここ はコメントを含め、コードです:
..
var
cc: Colorref;
begin
.. // fill values
cc := DictCategory.Items[ e_SQL1 ].rgbColor; // Okay, it works
TdictCategory.SaveToFile('category.json', DictCategory); // Even the contents of the file, looks good
DictCategory.Clear;
DictCategory.Free;
DictCategory := nil;
DictCategory := TdictCategory.LoadFromFile('category.json'); // DictCategory is no longer NIL, and it looks optically well..
cc := DictCategory.Items[ e2_sql_aggregate ].rgbColor; // C R A S H !!! with AV
Delphi(ベルリン10.1)、辞書をシリアル化することができないようです!それが本当なら、それは本当に私を傷つける。私は他にもたくさんあると信じています。または、添付されたコードにエラーがありますか?
あなたの質問は非常に冗長です。これは 'TJson.JsonToObject(..)'(そしておそらく 'TJson.ObjectToJsonObject (..)')も 'TList 'や 'TDictionary 'のようなジェネリッククラスではうまく動作しません。 –