mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-12 22:14:25 +02:00
* Refactor so LoadFromStream can be overridden
git-svn-id: trunk@34196 -
This commit is contained in:
parent
c715aa5188
commit
e1751df5c3
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user