JsonSettings
Base class for saving, encrypting, and loading application settings in a Json file
Install / Use
/learn @adamfoneil/JsonSettingsREADME
This is sort of an update of my AoOptions project, moving it from XML to Json, and removing the WinForms dependency. It was also important that I support a DPAPI encryption on properties, since the intended use case for this is to store sensitive things like database connection strings and other credentials. More info about DPAPI is here.
Nuget package: JsonSettings.Library
Note, as of 1/21/19, version 1.0.8 replaces the package AoJsonSettings in order to target .NET Standard 2.0.
How to Use
-
Create a class for your app settings based on SettingsBase abstract class and implement the Filename property.
-
If you need to encrypt a string property, add the [JsonProtect] attribute to the property. Props to this SO answer from Brian Rogers on implementing the custom attribute that does this.
-
Instantiate your settings class with
JsonSettingsBase.Load<T>whereTis your settings type. -
Use the
Savemethod to persist any modified settings to disk.
Examples
var settings = SettingsBase.Load<MySettings>();
// when app closing or on some other event:
settings.Save();
Encrypted property example:
public class AppSettings : SettingsBase
{
public override string Filename => BuildPath(Environment.SpecialFolder.LocalApplicationData, "AppSettings.json");
public string Greeting { get; set; } = "hello";
[JsonProtect(DataProtectionScope.CurrentUser)]
public string SensitiveValue { get; set; }
}
A Simpler Use
If you don't need the built-in settings file handling logic, you can use the JsonFile static class with its Load, Save (and LoadAsync, SaveAsync) methods to make it easy to save any json data to a file. The [JsonProtect] attribute still works in this usage.
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
