* Refactor so LoadFromStream can be overridden

git-svn-id: trunk@34196 -
This commit is contained in:
michael 2016-07-23 20:04:38 +00:00
parent c715aa5188
commit e1751df5c3

View File

@ -68,6 +68,8 @@ type
protected protected
FJSON: TJSONObject; FJSON: TJSONObject;
FModified: Boolean; FModified: Boolean;
Procedure LoadFromFile(Const AFileName : String);
Procedure LoadFromStream(S : TStream); virtual;
procedure Loaded; override; procedure Loaded; override;
function FindPath(Const APath: UnicodeString; AllowCreate : Boolean) : TJSONObject; function FindPath(Const APath: UnicodeString; AllowCreate : Boolean) : TJSONObject;
function FindObject(Const APath: UnicodeString; AllowCreate : Boolean) : TJSONObject; function FindObject(Const APath: UnicodeString; AllowCreate : Boolean) : TJSONObject;
@ -673,11 +675,6 @@ end;
procedure TJSONConfig.DoSetFilename(const AFilename: String; ForceReload: Boolean); procedure TJSONConfig.DoSetFilename(const AFilename: String; ForceReload: Boolean);
Var
P : TJSONParser;
J : TJSONData;
F : TFileStream;
begin begin
if (not ForceReload) and (FFilename = AFilename) then if (not ForceReload) and (FFilename = AFilename) then
exit; exit;
@ -685,32 +682,11 @@ begin
if csLoading in ComponentState then if csLoading in ComponentState then
exit; exit;
Flush; Flush;
If Not FileExists(AFileName) then If Not FileExists(AFileName) then
Clear Clear
else else
begin LoadFromFile(AFileName);
F:=TFileStream.Create(AFileName,fmopenRead);
try
P:=TJSONParser.Create(F,FJSONOptions);
try
J:=P.Parse;
If (J is TJSONObject) then
begin
FreeAndNil(FJSON);
FJSON:=J as TJSONObject;
FKey:=FJSON;
end
else
Raise EJSONConfigError.CreateFmt(SErrInvalidJSONFile,[AFileName]);
finally
P.Free;
end;
finally
F.Free;
end;
end;
end; end;
procedure TJSONConfig.SetFilename(const AFilename: String); procedure TJSONConfig.SetFilename(const AFilename: String);
@ -741,6 +717,46 @@ begin
Result:=P; Result:=P;
end; end;
procedure TJSONConfig.LoadFromFile(const AFileName: String);
Var
F : TFileStream;
begin
F:=TFileStream.Create(AFileName,fmopenRead);
try
LoadFromStream(F);
finally
F.Free;
end;
end;
procedure TJSONConfig.LoadFromStream(S: TStream);
Var
P : TJSONParser;
J : TJSONData;
begin
P:=TJSONParser.Create(S,FJSONOptions);
try
J:=P.Parse;
If (J is TJSONObject) then
begin
FreeAndNil(FJSON);
FJSON:=J as TJSONObject;
FKey:=FJSON;
end
else
begin
FreeAndNil(J);
Raise EJSONConfigError.CreateFmt(SErrInvalidJSONFile,[FileName]);
end;
finally
P.Free;
end;
end;
procedure TJSONConfig.CloseKey; procedure TJSONConfig.CloseKey;
begin begin