dynamic Settings = JsonSettings.Load<SettingsBag>("config.json").EnableAutosave().AsDynamic(); Settings["LastUpdate"] = DateTime.Now; //fine too Settings.LastUpdate = DateTime.Now; //even better.You might ask, "Where does the modularity part comes in?" and I'll answer with this:
SettingsBag Settings = JsonSettings.Load<SettingsBag>("config.json", s => s.WithEncryption("password"));The settings file is now encrypted with a memory-sealed password (using `SecureString`) and will be saved as raw bytes.
What if I want to make it more "stringy copyable"?
SettingsBag Settings = JsonSettings.Load<SettingsBag>("config.json", s => s.WithEncryption("password").WithBase64());Just throw that `WithBase64` at the end of the configuration lambda function and watch the file as `AZaz09/-=` characters so you can paste that config file to anywhere and still keeping it encrypted.
Abstraction
Aside from `SettingsBag`, the library features strong-typed settings writing specifically for those who prefer it strong.
Heres a quick self explanatory:
//Step 1: create a class and inherit JsonSettings class MySettings : JsonSettings { //Step 2: override a default FileName or keep it empty. Just make sure to specify it when calling Load! //This is used for default saving and loading so you won't have to specify the filename/path every time. //Putting just a filename without folder will put it inside the executing file's directory. public override string FileName { get; set; } = "TheDefaultFilename.extension"; //for loading and saving. #region Settings public string SomeProperty { get; set; } public int SomeNumberWithDefaultValue { get; set; } = 1; public CustomClass Classic { get; set; } #endregion //Step 3: Override parent's constructors public MySettings() { } public MySettings(string fileName) : base(fileName) { } } //Step 4: Load public MySettings Settings = JsonSettings.Load<MySettings>("config.json"); //relative path to executing file. //Step 5: Pwn. Settings.SomeProperty = "ok"; Settings.Save();
Is there a way to verify the config.json file before it attempts to load it?
ReplyDelete