Skip to content

Getting Started

Marcus Ackre Medina edited this page Jul 20, 2026 · 1 revision

Getting Started

Install

dotnet add package MarcusMedina.IO.JsonFile

Requires .NET 10+

Basic save and load

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); // 80

Filename 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.

Implicit conversion to T

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 needed

Using a using block

JsonFile<T> implements IDisposableDispose() 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 automatically

Custom file suffix

var data = new JsonFile<AppSettings>("settings") { Suffix = "cfg" };
data.Save(); // writes settings.cfg

Custom serialization format

Format 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.

Backups

Save() keeps one backup automatically: before writing, any existing settings.json is renamed to settings.json.bak (replacing a previous .bak if one exists).

Clone this wiki locally