Quick.Config.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. { ***************************************************************************
  2. Copyright (c) 2015-2017 Kike Pérez
  3. Unit : Quick.Config
  4. Description : Load/Save config from/to JSON file
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 26/01/2017
  8. Modified : 29/09/2017
  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;
  22. interface
  23. uses
  24. System.Classes,
  25. System.SysUtils,
  26. {$IF CompilerVersion >= 32.0}
  27. System.JSON.Types,
  28. System.JSON.Serializers;
  29. {$ELSE}
  30. Rest.Json.Types,
  31. Rest.Json;
  32. {$ENDIF}
  33. type
  34. [JsonSerialize(TJsonMemberSerialization.&Public)]
  35. TAppConfig = class
  36. private
  37. [JSONMarshalledAttribute(False)]
  38. fConfigFile : string;
  39. [JSONMarshalledAttribute(False)]
  40. fConfigEncrypted : Boolean;
  41. [JSONMarshalledAttribute(False)]
  42. fConfigPassword : string;
  43. public
  44. constructor Create(const ConfigFileName : string);
  45. [JsonIgnoreAttribute]
  46. property ConfigFile : string read fConfigFile;
  47. [JsonIgnoreAttribute]
  48. property ConfigEncrypted : Boolean read fConfigEncrypted write fConfigEncrypted;
  49. [JsonIgnoreAttribute]
  50. property ConfigPassword : string read fConfigPassword write fConfigPassword;
  51. function Load(CreateIfNotExists : Boolean = False) : Boolean;
  52. function Save : Boolean;
  53. function AsJsonString : string;
  54. end;
  55. {Usage: create a descend class from TAppConfig and add public properties to be loaded/saved
  56. TMyConfig = class(TAppConfig)
  57. private
  58. fName : string;
  59. fSurname : string;
  60. fStatus : Integer;
  61. public
  62. property Name : string read fName write fName;
  63. property SurName : string read fSurname write fSurname;
  64. property Status : Integer read fStatus write fStatus;
  65. end;
  66. }
  67. implementation
  68. { TAppConfig }
  69. constructor TAppConfig.Create(const ConfigFileName : string);
  70. begin
  71. fConfigFile := ConfigFileName;
  72. end;
  73. function TAppConfig.Load(CreateIfNotExists : Boolean = False) : Boolean;
  74. var
  75. json : TStrings;
  76. {$IF CompilerVersion >= 32.0}
  77. Serializer : TJsonSerializer;
  78. {$ENDIF}
  79. begin
  80. if (CreateIfNotExists) and (not FileExists(fConfigFile)) then
  81. begin
  82. Self.Save;
  83. Result := False;
  84. end;
  85. try
  86. json := TStringList.Create;
  87. try
  88. json.LoadFromFile(fConfigFile);
  89. if fConfigEncrypted then
  90. begin
  91. //decrypt json
  92. end;
  93. {$IF CompilerVersion >= 32.0}
  94. Serializer := TJsonSerializer.Create;
  95. try
  96. Self := Serializer.Deserialize<TAppConfig>(json.Text);
  97. finally
  98. Serializer.Free;
  99. end;
  100. {$ELSE}
  101. Self := TJson.JsonToObject<TAppConfig>(json.Text)
  102. {$ENDIF}
  103. finally
  104. json.Free;
  105. end;
  106. except
  107. on e : Exception do raise e;
  108. end;
  109. end;
  110. function TAppConfig.Save : Boolean;
  111. var
  112. json : TStrings;
  113. {$IF CompilerVersion >= 32.0}
  114. Serializer : TJsonSerializer;
  115. {$ENDIF}
  116. begin
  117. try
  118. json := TStringList.Create;
  119. try
  120. if fConfigEncrypted then
  121. begin
  122. //encrypt json
  123. end;
  124. {$IF CompilerVersion >= 32.0}
  125. Serializer := TJsonSerializer.Create;
  126. try
  127. Serializer.Formatting := TJsonFormatting.Indented;
  128. json.Text := Serializer.Serialize(Self);
  129. finally
  130. Serializer.Free;
  131. end;
  132. {$ELSE}
  133. json.Text := TJson.ObjectToJsonString(Self);
  134. {$ENDIF}
  135. json.SaveToFile(fConfigFile);
  136. finally
  137. json.Free;
  138. end;
  139. except
  140. on e : Exception do raise e;
  141. end;
  142. end;
  143. function TAppConfig.AsJsonString : string;
  144. {$IF CompilerVersion >= 32.0}
  145. var
  146. Serializer: TJsonSerializer;
  147. {$ENDIF}
  148. begin
  149. Result := '';
  150. {$IF CompilerVersion >= 32.0}
  151. Serializer := TJsonSerializer.Create;
  152. try
  153. Serializer.Formatting := TJsonFormatting.Indented;
  154. Result := Serializer.Serialize(Self);
  155. finally
  156. Serializer.Free;
  157. end;
  158. {$ELSE}
  159. Result := TJson.ObjectToJsonString(Self);
  160. {$ENDIF}
  161. end;
  162. end.