Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Saving an object in JSON and loading it from it
// ... ... ...
uses fpjson, jsonscanner, fpjsonrtti
;
// ... ... ...
procedure LoadFromJSON(AObject: TObject; const AFileName: String);
var
ADeStreamer: TJSONDeStreamer;
AJSON: TStringList;
begin
ADeStreamer:=TJSONDeStreamer.Create(nil);
AJSON:=TStringList.Create;
try
AJSON.LoadFromFile(AFileName);
try
ADeStreamer.JSONToObject(AJSON.Text, AObject);
except
end;
finally
AJSON.Free;
ADeStreamer.Free;
end;
end;
procedure SaveToJSON(AObject: TObject; const AFileName: String);
var
AStreamer: TJSONStreamer;
AJSON: TStringList;
begin
AStreamer:=TJSONStreamer.Create(nil);
AJSON:=TStringList.Create;
try
try
AStreamer.Options:=[jsoUseFormatString]+AStreamer.Options;
AJSON.Text:=AStreamer.ObjectToJSONString(AObject);
AJSON.SaveToFile(AFileName);
except
end;
finally
AJSON.Free;
AStreamer.Free;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment