-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
Namespace: MarcusMedina.IO.JsonFile
Generic class. T must have a public parameterless constructor (where T : new()). Implements IDisposable.
JsonFile(string filename)Creates the wrapper for filename (without a suffix) and immediately calls Load().
| 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. |
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.
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.
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.
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.