Skip to content

API Reference

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

API Reference

Namespace: MarcusMedina.IO.JsonFile

JsonFile<T>

Generic class. T must have a public parameterless constructor (where T : new()). Implements IDisposable.

Constructor

JsonFile(string filename)

Creates the wrapper for filename (without a suffix) and immediately calls Load().

Properties

Property Type Description
Data T? The loaded/working object. Populated by the constructor and by Load().
Filename string The filename passed to the constructor (read-only, no suffix).
Suffix string File extension, without the dot. Default is "json".
Format JsonSerializerOptions? Serialization options used by Load()/Save(). Defaults to indented output, nulls omitted on write, and reference cycles ignored.

Load

T? Load()

Reads {Filename}.{Suffix} and deserializes it into Data. If the file doesn't exist, or reading/deserializing fails, Data falls back to new T() instead of throwing. Called automatically by the constructor.

Save

void Save()

Serializes Data (using Format) and writes it to {Filename}.{Suffix}. Before writing, any existing file is renamed to {Filename}.{Suffix}.bak (replacing a previous backup), so the last saved version is always recoverable.

Dispose

void Dispose()

Calls Save(), then clears Data. Lets you use JsonFile<T> in a using block to save automatically when it goes out of scope.

Implicit conversion to T

public static implicit operator T(JsonFile<T> file)

Lets a JsonFile<T> be used anywhere a plain T is expected. If Data is null, it's initialised to new T() first.

Clone this wiki locally