Quick.Config.Base.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.Config
  4. Description : Load/Save config from/to JSON file
  5. Author : Kike Pérez
  6. Version : 1.5
  7. Created : 26/01/2017
  8. Modified : 10/12/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Config.Base;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. SysUtils,
  27. Rtti,
  28. {$IFDEF FPC}
  29. fpjson,
  30. jsonparser,
  31. fpjsonrtti,
  32. Quick.Json.Serializer;
  33. {$ELSE}
  34. Quick.Json.Serializer,
  35. DBXJSON,
  36. System.JSON,
  37. Rest.Json.Types,
  38. Rest.Json;
  39. {$ENDIF}
  40. type
  41. TDateTimeZone = (tzLocal, tzUTC);
  42. TAppConfig = class;
  43. IAppConfigProvider = interface
  44. ['{D55B1EBF-47F6-478B-8F70-9444575CB825}']
  45. procedure Load(cConfig : TAppConfig);
  46. procedure Save(cConfig : TAppConfig);
  47. end;
  48. TSerializeProperty = (spPublic, spPublished);
  49. TAppConfigProviderBase = class(TInterfacedObject,IAppConfigProvider)
  50. private
  51. fCreateIfNotExists : Boolean;
  52. fSerializeLevel : TSerializeProperty;
  53. protected
  54. procedure Load(cConfig : TAppConfig); virtual; abstract;
  55. procedure Save(cConfig : TAppConfig); virtual; abstract;
  56. public
  57. constructor Create; virtual;
  58. property CreateIfNotExists : Boolean read fCreateIfNotExists write fCreateIfNotExists;
  59. property SerializeLevel : TSerializeProperty read fSerializeLevel write fSerializeLevel;
  60. end;
  61. TApplyConfigEvent = procedure of object;
  62. {$IFDEF DELPHIXE2_UP}[JsonSerialize(TJsonMemberSerialization.&Public)]{$ENDIF}
  63. TAppConfig = class
  64. private
  65. {$IFDEF FPC}
  66. fOnApplyConfig : TApplyConfigEvent;
  67. fDateTimeZone: TDateTimeZone;
  68. fJsonIndent: Boolean;
  69. fLastSaved : TDateTime;
  70. {$ELSE}
  71. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  72. fOnApplyConfig : TApplyConfigEvent;
  73. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  74. fDateTimeZone: TDateTimeZone;
  75. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  76. fJsonIndent: Boolean;
  77. {$IF CompilerVersion < 32.0}[JSONMarshalledAttribute(False)]{$ENDIF}
  78. fLastSaved : TDateTime;
  79. {$ENDIF}
  80. protected
  81. function GetProvider : IAppConfigProvider; virtual; abstract;
  82. public
  83. constructor Create; virtual;
  84. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  85. property OnApplyConfig : TApplyConfigEvent read fOnApplyConfig write fOnApplyConfig;
  86. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  87. property DateTimeZone : TDateTimeZone read fDateTimeZone write fDateTimeZone;
  88. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  89. property JsonIndent : Boolean read fJsonIndent write fJsonIndent;
  90. {$IFDEF DELPHIRX102_UP}[JsonIgnoreAttribute]{$ENDIF}
  91. property LastSaved : TDateTime read fLastSaved write fLastSaved;
  92. procedure Apply;
  93. procedure DefaultValues; virtual;
  94. procedure Load;
  95. procedure Save;
  96. function ToJSON : string;
  97. procedure FromJSON(const json : string);
  98. end;
  99. {Usage: create a descend class from TAppConfig and add published properties to be loaded/saved
  100. TMyConfig = class(TAppConfig)
  101. private
  102. fName : string;
  103. fSurname : string;
  104. fStatus : Integer;
  105. published
  106. property Name : string read fName write fName;
  107. property SurName : string read fSurname write fSurname;
  108. property Status : Integer read fStatus write fStatus;
  109. end;
  110. AppConfigProvider := TAppConfigJsonProvider<TMyConfig>.Create(MyConfig);
  111. MyConfig.Name := 'John';
  112. }
  113. implementation
  114. { TAppConfigProviderBase }
  115. constructor TAppConfigProviderBase.Create;
  116. begin
  117. fCreateIfNotExists := True;
  118. fSerializeLevel := spPublished;
  119. end;
  120. { TAppConfig }
  121. constructor TAppConfig.Create;
  122. begin
  123. fDateTimeZone := TDateTimeZone.tzLocal;
  124. fJsonIndent := True;
  125. fLastSaved := 0;
  126. end;
  127. procedure TAppConfig.Apply;
  128. begin
  129. if Assigned(fOnApplyConfig) then fOnApplyConfig;
  130. end;
  131. procedure TAppConfig.DefaultValues;
  132. begin
  133. //inherit to set default values if no config exists before
  134. end;
  135. function TAppConfig.ToJSON : string;
  136. var
  137. Serializer : TJsonSerializer;
  138. begin
  139. Result := '';
  140. try
  141. serializer := TJsonSerializer.Create(slPublishedProperty);
  142. try
  143. Result := serializer.ObjectToJSON(Self,True);
  144. finally
  145. serializer.Free;
  146. end;
  147. except
  148. on e : Exception do raise Exception.Create(e.Message);
  149. end;
  150. end;
  151. procedure TAppConfig.FromJSON(const json : string);
  152. var
  153. Serializer : TJsonSerializer;
  154. begin
  155. try
  156. serializer := TJsonSerializer.Create(slPublishedProperty);
  157. try
  158. {$IF NOT DEFINED(FPC) AND DEFINED(ANDROID)}
  159. serializer.JsonToObject(Self,json);
  160. {$ELSE}
  161. Self := TAppConfig(serializer.JsonToObject(Self,json));
  162. {$ENDIF}
  163. finally
  164. serializer.Free;
  165. end;
  166. except
  167. on e : Exception do raise Exception.Create(e.Message);
  168. end;
  169. end;
  170. procedure TAppConfig.Load;
  171. begin
  172. GetProvider.Load(Self);
  173. end;
  174. procedure TAppConfig.Save;
  175. begin
  176. GetProvider.Save(Self);
  177. end;
  178. end.