Skip to content

Repository files navigation

WritableJsonConfiguration

JSON

GitHub last commit GitHub code size in bytes

GitHub search hit counter Nuget

What is it?

If you want to add a settings to your app and you like json, then this is what you need. This uses the standard interface for working with settings, which is provided by Microsoft.Extensions.Configuration. To do this, we added methods for changing values in settings via the standard interface, which will edit the json file themselves.

How to use?

Add Nuget Package in your project:

Install-Package WritableJsonConfiguration

Create configuration:

IConfigurationRoot configuration = WritableJsonConfigurationFabric.Create("Settings.json");

Use this configuration in the app as you need, usually people register it with the IoC, example(Splat):

Locator.CurrentMutable.RegisterConstant(configuration, typeof(IConfiguration));

Get value:

Themes theme = configuration.GetSection("Appearance:Theme").Get<Themes>();

or

Themes theme = configuration.Get<Themes>("Appearance:Theme");

Set value:

configuration.GetSection("Appearance:Theme").Set(theme);

or

configuration.Set("Appearance:Theme", theme);

Opt-in atomic writes on Windows

IConfigurationRoot configuration = WritableJsonConfigurationFabric.Create(source =>
{
    source.Path = "Settings.json";
    source.Optional = true;
    source.ReloadOnChange = false;
    source.UseAtomicWrites = true;
    source.ResolveFileProvider();
});

UseAtomicWrites defaults to false, preserving the previous API and platform behavior. When enabled, both Set overloads serialize writes to the same physical path within the process. A complete, parser-validated temporary file is flushed and replaces the main file; .bak contains the previous readable version. A first save creates only the main file. No-op saves do not rotate the backup. Memory is published after the file commit; ambiguous I/O errors reread disk and block further writes if reconciliation fails. Restart the application or recreate the configuration root before trying again in that case; calling Reload() on the same root does not unblock writes. Numeric path segments remain property names inside JSON objects. Inside arrays they are indexes; sparse indexes are rejected rather than redirected to another element.

Temporary files receive the source file's restricted Windows access permissions before any settings bytes are written. Existing main/backup permission differences that cannot safely be preserved cause an error, not a broader copy. The first file uses the containing directory's permissions. Other operating systems reject explicit opt-in with PlatformNotSupportedException; the default mode remains available.

This does not recover an already corrupt file, coordinate multiple processes, make a series of Set calls transactional, or guarantee survival of arbitrary hardware failure. Applications should validate/recover settings before loading configuration. Backups and leftover temporary files may contain secrets and must not be published.

Communication

Any suggestions and comments are welcome. If you want to contact me, use Telegram

About

Source of configurations in JSON format with the ability to edit values directly from the running application. Based on Microsoft.Extensions.Configuration.

Topics

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages