Kaynağa Gözat

Config Defaultvalues

Unknown 7 yıl önce
ebeveyn
işleme
026910df6d
2 değiştirilmiş dosya ile 41 ekleme ve 4 silme
  1. 3 2
      Quick.Config.Provider.Json.pas
  2. 38 2
      Quick.Config.pas

+ 3 - 2
Quick.Config.Provider.Json.pas

@@ -1,13 +1,13 @@
 { ***************************************************************************
 { ***************************************************************************
 
 
-  Copyright (c) 2015-2017 Kike Pérez
+  Copyright (c) 2015-2018 Kike Pérez
 
 
   Unit        : Quick.Config.Provider.Json
   Unit        : Quick.Config.Provider.Json
   Description : Save config to JSON file
   Description : Save config to JSON file
   Author      : Kike Pérez
   Author      : Kike Pérez
   Version     : 1.1
   Version     : 1.1
   Created     : 21/10/2017
   Created     : 21/10/2017
-  Modified    : 11/11/2017
+  Modified    : 12/02/2018
 
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
 
@@ -80,6 +80,7 @@ begin
 
 
   if (not FileExists(fFilename)) and (CreateIfNotExists) then
   if (not FileExists(fFilename)) and (CreateIfNotExists) then
   begin
   begin
+    TAppConfig(cConfig).DefaultValues;
     Self.Save(cConfig);
     Self.Save(cConfig);
   end;
   end;
 
 

+ 38 - 2
Quick.Config.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Author      : Kike Pérez
   Version     : 1.2
   Version     : 1.2
   Created     : 26/01/2017
   Created     : 26/01/2017
-  Modified    : 02/02/2018
+  Modified    : 12/02/2018
 
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
 
@@ -79,7 +79,6 @@ type
     fLastSaved : TDateTime;
     fLastSaved : TDateTime;
   public
   public
     constructor Create; virtual;
     constructor Create; virtual;
-    //constructor Create(ADefaultValues : Boolean); overload; virtual; abstract;
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
     property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
     property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
@@ -89,7 +88,9 @@ type
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
     {$IF CompilerVersion >= 32.0}[JsonIgnoreAttribute]{$ENDIF}
     property LastSaved : TDateTime read fLastSaved write fLastSaved;
     property LastSaved : TDateTime read fLastSaved write fLastSaved;
     procedure Apply;
     procedure Apply;
+    procedure DefaultValues; virtual;
     function ToJSON : string;
     function ToJSON : string;
+    procedure FromJSON(const json : string);
   end;
   end;
 
 
   {Usage: create a descend class from TAppConfig and add public properties to be loaded/saved
   {Usage: create a descend class from TAppConfig and add public properties to be loaded/saved
@@ -161,6 +162,11 @@ begin
   if Assigned(fOnApplyConfig) then fOnApplyConfig;
   if Assigned(fOnApplyConfig) then fOnApplyConfig;
 end;
 end;
 
 
+procedure TAppConfig.DefaultValues;
+begin
+  //inherit to set default values if no config exists before
+end;
+
 
 
 function TAppConfig.ToJSON : string;
 function TAppConfig.ToJSON : string;
 {$IF CompilerVersion >= 32.0}
 {$IF CompilerVersion >= 32.0}
@@ -193,4 +199,34 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TAppConfig.FromJSON(const json : string);
+{$IF CompilerVersion >= 32.0}
+  var
+    Serializer : TJsonSerializer;
+  {$ENDIF}
+begin
+  try
+    {$IF CompilerVersion >= 32.0}
+      Serializer := TJsonSerializer.Create;
+      try
+        Serializer.Formatting := TJsonFormatting.Indented;
+        if JsonIndent then Serializer.Formatting := TJsonFormatting.Indented;
+        if DateTimeZone = TDateTimeZone.tzLocal then
+        begin
+          Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Local;
+          Serializer.DateFormatHandling := TJsonDateFormatHandling.FormatSettings;
+        end
+        else Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Utc;
+        Self := Serializer.Deserialize<TAppConfig>(json);
+      finally
+        Serializer.Free;
+      end;
+    {$ELSE}
+      TJson.JsonToObject(Self,TJSONObject(TJSONObject.ParseJSONValue(json.Text)));
+    {$ENDIF}
+  except
+    on e : Exception do raise Exception.Create(e.Message);
+  end;
+end;
+
 end.
 end.