-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
dotnet add package MarcusMedina.IO.JsonFileRequires .NET 10+
using MarcusMedina.IO.JsonFile;
var settings = new JsonFile<AppSettings>("settings");
settings.Data.Volume = 80;
settings.Save();
// Later, or in another run of the app:
var reloaded = new JsonFile<AppSettings>("settings");
Console.WriteLine(reloaded.Data.Volume); // 80Filename is passed without a suffix — JsonFile<T> appends .json automatically, so "settings" becomes settings.json on disk.
If the file doesn't exist yet, Data is populated with a fresh new T() instead of throwing.
JsonFile<T> converts implicitly to T, so you can pass it straight into code that expects the plain object:
void PrintVolume(AppSettings s) => Console.WriteLine(s.Volume);
var settings = new JsonFile<AppSettings>("settings");
PrintVolume(settings); // implicit conversion — no .Data neededJsonFile<T> implements IDisposable — Dispose() calls Save() for you, so wrapping it in a using block saves automatically when you're done:
using (var settings = new JsonFile<AppSettings>("settings"))
{
settings.Data.Volume = 42;
} // Save() is called here automaticallyvar data = new JsonFile<AppSettings>("settings") { Suffix = "cfg" };
data.Save(); // writes settings.cfgFormat is a plain System.Text.Json.JsonSerializerOptions — override it if you need different serialization behaviour:
var data = new JsonFile<AppSettings>("settings")
{
Format = new JsonSerializerOptions { WriteIndented = false }
};By default, JsonFile<T> writes indented JSON, ignores null properties when writing, and ignores reference cycles.
Save() keeps one backup automatically: before writing, any existing settings.json is renamed to settings.json.bak (replacing a previous .bak if one exists).