123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- { ***************************************************************************
- Copyright (c) 2015-2018 Kike Pérez
- Unit : Quick.Config.Provider.Json
- Description : Save config to JSON file
- Author : Kike Pérez
- Version : 1.2
- Created : 21/10/2017
- Modified : 07/04/2018
- This file is part of QuickLib: https://github.com/exilon/QuickLib
- ***************************************************************************
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- *************************************************************************** }
- unit Quick.Config.Provider.Json;
- {$i QuickLib.inc}
- interface
- uses
- Classes,
- SysUtils,
- {$IFDEF DELPHIXE_UP}
- IOUtils,
- {$ELSE}
- Quick.Files,
- {$ENDIF}
- Rtti,
- {$IFDEF DELPHIRX102_UP}
- System.JSON.Types,
- System.JSON.Serializers,
- {$ELSE}
- Quick.Json.Serializer,
- {$IFDEF FPC}
- fpjson,
- fpjsonrtti,
- {$ELSE}
- Rest.Json.Types,
- System.JSON,
- {$ENDIF}
- {$ENDIF}
- Quick.Config;
- type
- TAppConfigJsonProvider<T: class> = class(TAppConfigProviderBase<T>)
- private
- fFilename : string;
- public
- constructor Create(var cConfig : T); override;
- property Filename : string read fFilename write fFilename;
- procedure Load(var cConfig : T); override;
- procedure Save(var cConfig : T); override;
- end;
- implementation
- constructor TAppConfigJsonProvider<T>.Create(var cConfig : T);
- begin
- inherited Create(cConfig);
- fFilename := TPath.ChangeExtension(ParamStr(0),'json');
- end;
- procedure TAppConfigJsonProvider<T>.Load(var cConfig : T);
- var
- json : TStrings;
- Serializer : TJsonSerializer;
- NewObj : T;
- begin
- //create object with rtti if nil
- //if not Assigned(Config) then Config := InitObject;
- if (not FileExists(fFilename)) and (CreateIfNotExists) then
- begin
- TAppConfig(cConfig).DefaultValues;
- Self.Save(cConfig);
- end;
- try
- json := TStringList.Create;
- try
- json.LoadFromFile(fFilename);
- {$IFDEF DELPHIRX102_UP}
- Serializer := TJsonSerializer.Create;
- try
- if TAppConfig(cConfig).DateTimeZone = TDateTimeZone.tzLocal then
- begin
- Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Local;
- Serializer.DateFormatHandling := TJsonDateFormatHandling.FormatSettings;
- end
- else Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Utc;
- NewObj := Serializer.Deserialize<T>(json.Text);
- finally
- Serializer.Free;
- end;
- {$ELSE}
- serializer := TJsonSerializer.Create(slPublishedProperty);
- try
- //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
- //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
- serializer.JsonToObject(cConfig,json.Text);
- Exit;
- finally
- serializer.Free;
- end;
- {$ENDIF}
- if Assigned(cConfig) then cConfig.Free;
- cConfig := NewObj;
- finally
- json.Free;
- end;
- except
- on e : Exception do raise e;
- end;
- end;
- procedure TAppConfigJsonProvider<T>.Save(var cConfig : T);
- var
- json : TStrings;
- Serializer : TJsonSerializer;
- ctx : TRttiContext;
- rprop : TRttiProperty;
- begin
- //create object with rtti if nil
- if not Assigned(cConfig) then cConfig := InitObject;
- try
- json := TStringList.Create;
- try
- {$IFDEF DELPHIRX102_UP}
- Serializer := TJsonSerializer.Create;
- try
- if TAppConfig(cConfig).JsonIndent then Serializer.Formatting := TJsonFormatting.Indented;
- if TAppConfig(cConfig).DateTimeZone = TDateTimeZone.tzLocal then
- begin
- Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Local;
- Serializer.DateFormatHandling := TJsonDateFormatHandling.FormatSettings;
- end
- else Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Utc;
- json.Text := Serializer.Serialize<T>(cConfig);
- finally
- Serializer.Free;
- end;
- {$ELSE}
- serializer := TJsonSerializer.Create(TSerializeLevel.slPublishedProperty);
- try
- //Streamer.Options := Streamer.Options + [jsoDateTimeAsString ,jsoUseFormatString];
- //Streamer.DateTimeFormat := 'yyyy-mm-dd"T"hh:mm:ss.zz';
- json.Text := serializer.ObjectToJson(cConfig);
- finally
- serializer.Free;
- end;
- {$ENDIF}
- json.SaveToFile(fFilename);
- {$IFDEF FPC}
- //TAppConfig(cConfig).LastSaved := Now;
- {$ELSE}
- ctx := TRttiContext.Create;
- try
- rprop := ctx.GetType(TypeInfo(T)).GetProperty('LastSaved');
- rprop.SetValue(TObject(cConfig),TValue.FromVariant(Now()));
- finally
- ctx.Free;
- end;
- {$ENDIF}
- finally
- json.Free;
- end;
- except
- on e : Exception do raise e;
- end;
- end;
- end.
|