Quick.Config.Provider.Json.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.Config.Provider.Json
  4. Description : Save config to JSON file
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 21/10/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.Provider.Json;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. {$IFDEF DELPHIXE_UP}
  28. IOUtils,
  29. {$ELSE}
  30. Quick.Files,
  31. {$ENDIF}
  32. Rtti,
  33. Quick.Json.Serializer,
  34. {$IFDEF FPC}
  35. fpjson,
  36. fpjsonrtti,
  37. {$ELSE}
  38. Rest.Json.Types,
  39. System.JSON,
  40. {$ENDIF}
  41. Quick.Config;
  42. type
  43. TAppConfigJsonProvider<T: class> = class(TAppConfigProviderBase<T>)
  44. private
  45. fFilename : string;
  46. public
  47. constructor Create(var cConfig : T); override;
  48. property Filename : string read fFilename write fFilename;
  49. procedure Load(var cConfig : T); override;
  50. procedure Save(var cConfig : T); override;
  51. end;
  52. implementation
  53. constructor TAppConfigJsonProvider<T>.Create(var cConfig : T);
  54. begin
  55. inherited Create(cConfig);
  56. fFilename := TPath.ChangeExtension(ParamStr(0),'json');
  57. end;
  58. procedure TAppConfigJsonProvider<T>.Load(var cConfig : T);
  59. var
  60. json : TStrings;
  61. Serializer : TJsonSerializer;
  62. NewObj : T;
  63. begin
  64. //create object with rtti if nil
  65. //if not Assigned(Config) then Config := InitObject;
  66. if (not FileExists(fFilename)) and (CreateIfNotExists) then
  67. begin
  68. TAppConfig(cConfig).DefaultValues;
  69. Self.Save(cConfig);
  70. end;
  71. try
  72. json := TStringList.Create;
  73. try
  74. json.LoadFromFile(fFilename);
  75. serializer := TJsonSerializer.Create(slPublishedProperty);
  76. try
  77. //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
  78. //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
  79. serializer.JsonToObject(cConfig,json.Text);
  80. Exit;
  81. finally
  82. serializer.Free;
  83. end;
  84. if Assigned(cConfig) then cConfig.Free;
  85. cConfig := NewObj;
  86. finally
  87. json.Free;
  88. end;
  89. except
  90. on e : Exception do raise e;
  91. end;
  92. end;
  93. procedure TAppConfigJsonProvider<T>.Save(var cConfig : T);
  94. var
  95. json : TStrings;
  96. Serializer : TJsonSerializer;
  97. ctx : TRttiContext;
  98. rprop : TRttiProperty;
  99. begin
  100. //create object with rtti if nil
  101. if not Assigned(cConfig) then cConfig := InitObject;
  102. try
  103. json := TStringList.Create;
  104. try
  105. serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
  106. try
  107. //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
  108. //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
  109. json.Text := serializer.ObjectToJson(cConfig,True);
  110. finally
  111. serializer.Free;
  112. end;
  113. json.SaveToFile(fFilename);
  114. {$IFDEF FPC}
  115. //TAppConfig(cConfig).LastSaved := Now;
  116. {$ELSE}
  117. ctx := TRttiContext.Create;
  118. try
  119. rprop := ctx.GetType(TypeInfo(T)).GetProperty('LastSaved');
  120. rprop.SetValue(TObject(cConfig),TValue.FromVariant(Now()));
  121. finally
  122. ctx.Free;
  123. end;
  124. {$ENDIF}
  125. finally
  126. json.Free;
  127. end;
  128. except
  129. on e : Exception do raise e;
  130. end;
  131. end;
  132. end.