浏览代码

* Refactor so LoadFromStream can be overridden

git-svn-id: trunk@34196 -
michael 9 年之前
父节点
当前提交
e1751df5c3
共有 1 个文件被更改,包括 43 次插入27 次删除
  1. 43 27
      packages/fcl-json/src/jsonconf.pp

+ 43 - 27
packages/fcl-json/src/jsonconf.pp

@@ -68,6 +68,8 @@ type
   protected
     FJSON: TJSONObject;
     FModified: Boolean;
+    Procedure LoadFromFile(Const AFileName : String);
+    Procedure LoadFromStream(S : TStream); virtual;
     procedure Loaded; override;
     function FindPath(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);
 
-Var
-  P : TJSONParser;
-  J : TJSONData;
-  F : TFileStream;
-  
 begin
   if (not ForceReload) and (FFilename = AFilename) then
     exit;
@@ -685,32 +682,11 @@ begin
 
   if csLoading in ComponentState then
     exit;
-
   Flush;
   If Not FileExists(AFileName) then
     Clear
   else
-    begin
-    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;
+    LoadFromFile(AFileName);
 end;
 
 procedure TJSONConfig.SetFilename(const AFilename: String);
@@ -741,6 +717,46 @@ begin
     Result:=P;
 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;
 begin