Quick.Options.Serializer.Json.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 : 22/10/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. procedure Load(const aFilename : string; aSections : TSectionList); 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. procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
  55. var
  56. option : TOptions;
  57. fileoptions : string;
  58. json : TJsonObject;
  59. jpair : TJSONPair;
  60. begin
  61. if FileExists(aFilename) then
  62. begin
  63. //read option file
  64. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  65. json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject;
  66. for option in aSections do
  67. begin
  68. jpair := fSerializer.GetJsonPairByName(json,option.Name);
  69. if jpair = nil then raise Exception.CreateFmt('Config section "%s" not found',[option.Name]);
  70. if jpair.JsonValue <> nil then
  71. begin
  72. //deserialize option
  73. fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject);
  74. //validate loaded configuration
  75. option.ValidateOptions;
  76. end;
  77. end;
  78. end;
  79. end;
  80. procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  81. var
  82. option : TOptions;
  83. fileoptions : string;
  84. json : TJSONObject;
  85. jpair : TJSONPair;
  86. begin
  87. json := TJSONObject.Create;
  88. try
  89. for option in aSections do
  90. begin
  91. //validate configuration before save
  92. option.ValidateOptions;
  93. //serialize option
  94. jpair := fSerializer.Serialize(option.Name,option);
  95. json.AddPair(jpair);
  96. end;
  97. fileoptions := TJsonUtils.JsonFormat(json.ToJSON);
  98. TFile.WriteAllText(aFilename,fileoptions);
  99. finally
  100. json.Free;
  101. end;
  102. end;
  103. end.