Skip to content

Latest commit

 

History

History
173 lines (134 loc) · 4.04 KB

README.md

File metadata and controls

173 lines (134 loc) · 4.04 KB

Awesome.Net.WritableOptions

Use to update option values into json file

Usage

Install-Package

Install-Package Awesome.Net.WritableOptions

Configure writable options

Let's start with the simplest configuration

services.ConfigureWritableOptions<MyOptions>(configurationRoot, "MySectionName");

or use custom json file

services.ConfigureWritableOptions<MyOptions>(configurationRoot, "MySectionName", "Resources/appsettings.custom.json");

or use custom json serializer options

services.ConfigureWritableOptions<MyOptions>(
    configurationRoot, 
    "MySectionName", 
    defaultSerializerOptions: new JsonSerializerOptions
    {
        WriteIndented = true,
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        Converters = { new JsonStringEnumConverter() }
    });

Update option values into json file

private readonly IWritableOptions<MyOptions> _options;

public MyClass(IWritableOptions<MyOptions> options)
{
    _options = options;
}
_options.Update(opt => {
    opt.Field1 = "value1";
    opt.Field2 = "value2";
});

No Reload

_options.Update(opt => {
    opt.Field1 = "value1";
    opt.Field2 = "value2";
}, false);

If a certain configuration, specific json serialization options need to be used.

var customSerializerOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
    Converters = { new JsonStringEnumConverter() }
};
_options.Update(opt => {
    opt.Field1 = "value1";
    opt.Field2 = "value2";
}, serializerOptions: customSerializerOptions);

See more: How to update values into appsetting.json?

Others

JsonFileHelper

Methods

public class JsonFileHelper
{
    public static Func<JsonSerializerOptions> DefaultSerializerOptions;

    public static void AddOrUpdateSection<T>(string jsonFilePath, string sectionName, Action<T> updateAction = null, JsonSerializerOptions serializerOptions = null) where T : class, new();

    public static void AddOrUpdateSection<T>(string jsonFilePath, string sectionName, T value, JsonSerializerOptions serializerOptions = null);

    public static bool TryGet<T>(string jsonFilePath, string sectionName, out T value, JsonSerializerOptions serializerOptions = null);
}

AddOrUpdateSection

Use the default JSON serialization option update some configuration items

JsonFileHelper.AddOrUpdateSection(
    jsonFilePath: "appsettings.json", 
    sectionName: "MySectionName", 
    value: new MyOptions {
        opt.Field1 = "value1";
        opt.Field2 = "value2";
    });

Or use the default JSON serialization option update the configuration section

JsonFileHelper.AddOrUpdateSection<MyOptions>(
    jsonFilePath: "Resources/appsettings.custom.json", 
    sectionName: "MySectionName", 
    updateAction: opt => {
        opt.Field1 = "value1";
        opt.Field2 = "value2";
    });

Or use the custom JSON serialization option update the configuration section

JsonFileHelper.AddOrUpdateSection<MyOptions>(
    jsonFilePath: "Resources/appsettings.custom.json", 
    sectionName: "MySectionName", 
    updateAction: opt => {
        opt.Field1 = "value1";
        opt.Field2 = "value2";
    },
    serializerOptions: new JsonSerializerOptions
    {
        WriteIndented = true,
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        Converters = { new JsonStringEnumConverter() }
    });

TryGet

Use the default JSON serialization options

if(JsonFileHelper.TryGet(jsonFilePath, sectionName, out MyOptions value))
{
    ...
}

Or use the custom JSON serialization options

var customSerializerOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
    Converters = { new JsonStringEnumConverter() }
};
if(JsonFileHelper.TryGet(jsonFilePath, sectionName, out MyOptions value, customSerializerOptions))
{
    ...
}