Quick.Options.Serializer.Json.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. { ***************************************************************************
  2. Copyright (c) 2015-2019 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 : 28/11/2019
  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. public
  38. constructor Create;
  39. destructor Destroy; override;
  40. function Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
  41. procedure Save(const aFilename : string; aSections : TSectionList); override;
  42. end;
  43. implementation
  44. { TJsonOptionsSerializer }
  45. constructor TJsonOptionsSerializer.Create;
  46. begin
  47. fSerializer := TRTTIJson.Create(TSerializeLevel.slPublishedProperty,True);
  48. end;
  49. destructor TJsonOptionsSerializer.Destroy;
  50. begin
  51. fSerializer.Free;
  52. inherited;
  53. end;
  54. function TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
  55. var
  56. option : TOptions;
  57. fileoptions : string;
  58. json : TJsonObject;
  59. jpair : TJSONPair;
  60. begin
  61. Result := False;
  62. if FileExists(aFilename) then
  63. begin
  64. //read option file
  65. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  66. json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject;
  67. for option in aSections do
  68. begin
  69. jpair := fSerializer.GetJsonPairByName(json,option.Name);
  70. if jpair = nil then
  71. begin
  72. if aFailOnSectionNotExists then raise Exception.CreateFmt('Config section "%s" not found',[option.Name])
  73. else Continue;
  74. end;
  75. if jpair.JsonValue <> nil then
  76. begin
  77. //deserialize option
  78. fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
  79. //validate loaded configuration
  80. option.ValidateOptions;
  81. end;
  82. end;
  83. end;
  84. end;
  85. procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  86. var
  87. option : TOptions;
  88. fileoptions : string;
  89. json : TJSONObject;
  90. jpair : TJSONPair;
  91. begin
  92. json := TJSONObject.Create;
  93. try
  94. for option in aSections do
  95. begin
  96. //validate configuration before save
  97. option.ValidateOptions;
  98. //serialize option
  99. jpair := fSerializer.Serialize(option.Name,option);
  100. json.AddPair(jpair);
  101. end;
  102. fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
  103. TFile.WriteAllText(aFilename,fileoptions);
  104. finally
  105. json.Free;
  106. end;
  107. end;
  108. end.