|
| 1 | +{ *************************************************************************** |
| 2 | +
|
| 3 | + Copyright (c) 2015-2019 Kike Pérez |
| 4 | +
|
| 5 | + Unit : Quick.Options.Serializer.Json |
| 6 | + Description : Configuration groups Json Serializer |
| 7 | + Author : Kike Pérez |
| 8 | + Version : 1.0 |
| 9 | + Created : 18/10/2019 |
| 10 | + Modified : 22/10/2019 |
| 11 | +
|
| 12 | + This file is part of QuickLib: https://github.com/exilon/QuickLib |
| 13 | +
|
| 14 | + *************************************************************************** |
| 15 | +
|
| 16 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | + you may not use this file except in compliance with the License. |
| 18 | + You may obtain a copy of the License at |
| 19 | +
|
| 20 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +
|
| 22 | + Unless required by applicable law or agreed to in writing, software |
| 23 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | + See the License for the specific language governing permissions and |
| 26 | + limitations under the License. |
| 27 | +
|
| 28 | + *************************************************************************** } |
| 29 | + |
| 30 | +unit Quick.Options.Serializer.Json; |
| 31 | + |
| 32 | +{$i QuickLib.inc} |
| 33 | + |
| 34 | +interface |
| 35 | + |
| 36 | +uses |
| 37 | + System.SysUtils, |
| 38 | + System.IOUtils, |
| 39 | + System.Generics.Collections, |
| 40 | + System.Json, |
| 41 | + Quick.Commons, |
| 42 | + Quick.JSON.Utils, |
| 43 | + Quick.Json.Serializer, |
| 44 | + Quick.Options; |
| 45 | + |
| 46 | +type |
| 47 | + |
| 48 | + TJsonOptionsSerializer = class(TOptionsSerializer) |
| 49 | + private |
| 50 | + fSerializer : TRTTIJson; |
| 51 | + public |
| 52 | + constructor Create; |
| 53 | + destructor Destroy; override; |
| 54 | + procedure Load(const aFilename : string; aSections : TSectionList); override; |
| 55 | + procedure Save(const aFilename : string; aSections : TSectionList); override; |
| 56 | + end; |
| 57 | + |
| 58 | +implementation |
| 59 | + |
| 60 | +{ TJsonOptionsSerializer } |
| 61 | + |
| 62 | +constructor TJsonOptionsSerializer.Create; |
| 63 | +begin |
| 64 | + fSerializer := TRTTIJson.Create(TSerializeLevel.slPublishedProperty,True); |
| 65 | +end; |
| 66 | + |
| 67 | +destructor TJsonOptionsSerializer.Destroy; |
| 68 | +begin |
| 69 | + fSerializer.Free; |
| 70 | + inherited; |
| 71 | +end; |
| 72 | + |
| 73 | +procedure TJsonOptionsSerializer.Load(const aFilename : string; aSections : TSectionList); |
| 74 | +var |
| 75 | + option : TOptions; |
| 76 | + fileoptions : string; |
| 77 | + json : TJsonObject; |
| 78 | + jpair : TJSONPair; |
| 79 | +begin |
| 80 | + if FileExists(aFilename) then |
| 81 | + begin |
| 82 | + //read option file |
| 83 | + fileoptions := TFile.ReadAllText(aFilename,TEncoding.UTF8); |
| 84 | + json := TJSONObject.ParseJSONValue(fileoptions) as TJSONObject; |
| 85 | + for option in aSections do |
| 86 | + begin |
| 87 | + jpair := fSerializer.GetJsonPairByName(json,option.Name); |
| 88 | + if jpair = nil then raise Exception.CreateFmt('Config section "%s" not found',[option.Name]); |
| 89 | + if jpair.JsonValue <> nil then |
| 90 | + begin |
| 91 | + //deserialize option |
| 92 | + fSerializer.DeserializeObject(option,jpair.JsonValue as TJSONObject); |
| 93 | + //validate loaded configuration |
| 94 | + option.ValidateOptions; |
| 95 | + end; |
| 96 | + end; |
| 97 | + end; |
| 98 | +end; |
| 99 | + |
| 100 | +procedure TJsonOptionsSerializer.Save(const aFilename : string; aSections : TSectionList); |
| 101 | +var |
| 102 | + option : TOptions; |
| 103 | + fileoptions : string; |
| 104 | + json : TJSONObject; |
| 105 | + jpair : TJSONPair; |
| 106 | +begin |
| 107 | + json := TJSONObject.Create; |
| 108 | + try |
| 109 | + for option in aSections do |
| 110 | + begin |
| 111 | + //validate configuration before save |
| 112 | + option.ValidateOptions; |
| 113 | + //serialize option |
| 114 | + jpair := fSerializer.Serialize(option.Name,option); |
| 115 | + json.AddPair(jpair); |
| 116 | + end; |
| 117 | + fileoptions := TJsonUtils.JsonFormat(json.ToJSON); |
| 118 | + TFile.WriteAllText(aFilename,fileoptions); |
| 119 | + finally |
| 120 | + json.Free; |
| 121 | + end; |
| 122 | +end; |
| 123 | + |
| 124 | +end. |
0 commit comments