Quick.Options.Serializer.Yaml.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. { ***************************************************************************
  2. Copyright (c) 2015-2019 Kike Pérez
  3. Unit : Quick.Options.Serializer.Yaml
  4. Description : Configuration groups Yaml 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.Yaml;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. System.IOUtils,
  27. System.Generics.Collections,
  28. Quick.YAML,
  29. Quick.Commons,
  30. Quick.YAML.Serializer,
  31. Quick.Options;
  32. type
  33. TYamlOptionsSerializer = class(TOptionsSerializer)
  34. private
  35. fSerializer : TRTTIYaml;
  36. public
  37. constructor Create;
  38. destructor Destroy; override;
  39. procedure Load(const aFilename : string; aSections : TSectionList); override;
  40. procedure Save(const aFilename : string; aSections : TSectionList); override;
  41. end;
  42. implementation
  43. { TYamlOptionsSerializer }
  44. constructor TYamlOptionsSerializer.Create;
  45. begin
  46. fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
  47. end;
  48. destructor TYamlOptionsSerializer.Destroy;
  49. begin
  50. fSerializer.Free;
  51. inherited;
  52. end;
  53. procedure TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSectionList);
  54. var
  55. option : TOptions;
  56. fileoptions : string;
  57. json : TYamlObject;
  58. jpair : TYamlPair;
  59. begin
  60. if FileExists(aFilename) then
  61. begin
  62. //read option file
  63. fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8);
  64. json := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
  65. for option in aSections do
  66. begin
  67. jpair := fSerializer.GetYamlPairByName(json,option.Name);
  68. if jpair.Value <> nil then
  69. begin
  70. //deserialize option
  71. fSerializer.DeserializeObject(option,jpair.Value as TYamlObject);
  72. //validate loaded configuration
  73. option.ValidateOptions;
  74. end;
  75. end;
  76. end;
  77. end;
  78. procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSectionList);
  79. var
  80. option : TOptions;
  81. fileoptions : string;
  82. yaml : TYamlObject;
  83. jpair : TYamlPair;
  84. begin
  85. yaml := TYamlObject.Create;
  86. try
  87. for option in aSections do
  88. begin
  89. //validate configuration before save
  90. option.ValidateOptions;
  91. //serialize option
  92. jpair := fSerializer.Serialize(option.Name,option);
  93. yaml.AddPair(jpair);
  94. end;
  95. fileoptions := yaml.ToYaml;
  96. TFile.WriteAllText(aFilename,fileoptions);
  97. finally
  98. yaml.Free;
  99. end;
  100. end;
  101. end.