Quick.Options.Serializer.Json.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. { ***************************************************************************
  2. Copyright (c) 2015-2020 Kike Pérez
  3. Unit : Quick.Options.Serializer.Json
  4. Description : Configuration groups Json Serializer
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 18/10/2019
  8. Modified : 07/02/2020
  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.Options.Serializer.Json;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. System.IOUtils,
  27. System.Generics.Collections,
  28. System.Json,
  29. Quick.Commons,
  30. Quick.JSON.Utils,
  31. Quick.Json.Serializer,
  32. Quick.Options;
  33. type
  34. TJsonOptionsSerializer = class(TOptionsSerializer)
  35. private
  36. fSerializer : TRTTIJson;
  37. function ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
  38. public
  39. constructor Create;
  40. destructor Destroy; override;
  41. function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
  42. procedure Save(const aFilename : string; aSections : TSectionList); override;
  43. function GetFileSectionNames(const aFilename : string; out oSections : TArray<string>) : Boolean; override;
  44. end;
  45. implementation
  46. { TJsonOptionsSerializer }
  47. constructor TJsonOptionsSerializer.Create;
  48. begin
  49. fSerializer := TRTTIJson.Create(TSerializeLevel.slPublishedProperty,True);
  50. end;
  51. destructor TJsonOptionsSerializer.Destroy;
  52. begin
  53. fSerializer.Free;
  54. inherited;
  55. end;
  56. function TJsonOptionsSerializer.GetFileSectionNames(const aFilename: string; out oSections: TArray<string>): Boolean;
  57. var
  58. json : TJsonObject;
  59. jpair : TJSONPair;
  60. i : Integer;
  61. begin
  62. Result := False;
  63. json := nil;
  64. if ParseFile(aFilename,json) then
  65. begin
  66. try
  67. for i := 0 to json.Count - 1 do
  68. begin
  69. oSections := oSections + [json.Pairs[i].JsonString.Value];
  70. end;
  71. Result := True;
  72. finally
  73. json.Free;
  74. end;
  75. end;
  76. end;
  77. function TJsonOptionsSerializer.ParseFile(const aFilename : string; out aJsonObj : TJsonObject) : Boolean;
  78. var
  79. fileoptions : string;
  80. begin
  81. aJsonObj := nil;
  82. if FileExists(aFilename) then
  83. begin
  84. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  85. aJsonObj := TJsonObject.ParseJSONValue(fileoptions) as TJsonObject;
  86. end;
  87. Result := aJsonObj <> nil;
  88. end;
  89. function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
  90. var
  91. option : TOptions;
  92. fileoptions : string;
  93. json : TJsonObject;
  94. jpair : TJSONPair;
  95. begin
  96. Result := False;
  97. if ParseFile(aFilename,json) then
  98. begin
  99. try
  100. for option in aSections do
  101. begin
  102. jpair := fSerializer.GetJsonPairByName(json,option.Name);
  103. if jpair = nil then
  104. begin
  105. if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
  106. else Continue;
  107. end;
  108. if jpair.JsonValue <> nil then
  109. begin
  110. //deserialize option
  111. fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
  112. //validate loaded configuration
  113. option.ValidateOptions;
  114. end;
  115. end;
  116. Result := True;
  117. finally
  118. json.Free;
  119. end;
  120. end;
  121. end;
  122. procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  123. var
  124. option : TOptions;
  125. fileoptions : string;
  126. json : TJSONObject;
  127. jpair : TJSONPair;
  128. begin
  129. json := TJSONObject.Create;
  130. try
  131. for option in aSections do
  132. begin
  133. if not option.HideOptions then
  134. begin
  135. //validate configuration before save
  136. option.ValidateOptions;
  137. //serialize option
  138. jpair := fSerializer.Serialize(option.Name,option);
  139. json.AddPair(jpair);
  140. end;
  141. end;
  142. fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
  143. if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename,fileoptions);
  144. finally
  145. json.Free;
  146. end;
  147. end;
  148. end.