Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nogic1008/writableoptions.net
Provides Options<T> that can update values to source JSON file
https://github.com/nogic1008/writableoptions.net
csharp dotnet json options
Last synced: 5 days ago
JSON representation
Provides Options<T> that can update values to source JSON file
- Host: GitHub
- URL: https://github.com/nogic1008/writableoptions.net
- Owner: nogic1008
- License: mit
- Created: 2021-02-22T02:06:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T09:35:41.000Z (7 months ago)
- Last Synced: 2024-05-01T16:23:54.493Z (7 months ago)
- Topics: csharp, dotnet, json, options
- Language: C#
- Homepage:
- Size: 1.11 MB
- Stars: 17
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WritableOptions.Net
Provides `Options` that can update values to source JSON file
[![NuGet](https://img.shields.io/nuget/v/Nogic.WritableOptions?label=NuGet&logo=nuget&logoColor=blue)](https://www.nuget.org/packages/Nogic.WritableOptions/)
[![GitHub release](https://img.shields.io/github/v/release/nogic1008/WritableOptions.Net?include_prereleases&logo=github&sort=semver)](https://github.com/nogic1008/WritableOptions.Net/releases)
[![.NET CI](https://github.com/nogic1008/WritableOptions.Net/actions/workflows/dotnet.yml/badge.svg)](https://github.com/nogic1008/WritableOptions.Net/actions/workflows/dotnet.yml)
[![codecov](https://codecov.io/gh/nogic1008/WritableOptions.Net/branch/main/graph/badge.svg?token=SjTS03boND)](https://codecov.io/gh/nogic1008/WritableOptions.Net)
[![CodeFactor](https://www.codefactor.io/repository/github/nogic1008/WritableOptions.Net/badge)](https://www.codefactor.io/repository/github/nogic1008/WritableOptions.Net)
[![License](https://img.shields.io/github/license/nogic1008/WritableOptions.Net)](LICENSE)This is a fork of [Awesome.Net.WritableOptions](https://www.nuget.org/packages/Awesome.Net.WritableOptions), but:
- Supports [UTF-8+BOM](https://github.com/nogic1008/WritableOptions.Net/issues/55) files
- improves performance by using `Span` (see [benchmark](https://github.com/nogic1008/WritableOptions.Net/tree/main/sandbox/Benchmark))See also: [How to update values into appsetting.json?](https://stackoverflow.com/questions/40970944/how-to-update-values-into-appsetting-json)
## Usage
This library is intended for use with [.NET Generic Host](https://learn.microsoft.com/aspnet/core/fundamentals/host/generic-host) context.
### Setup
```csharp
using Nogic.WritableOptions;// Load config from { "MySection": {(here)} }
IConfigurationSection section = context.Configration.GetSection("MySection");// Save & Load from appsettings.json (from root folder)
services.ConfigureWritable(section);// Save & Load from (Special Folder)/appsettings.json.
// It is useful for Xamarin
services.ConfigureWritableWithExplicitPath(section, FileSystem.AppDataDirectory);
```### Consume
```csharp
public class AppBase
{
private readonly IWritableOptions _writableOptions;// Constructor DI
public AppBase(IWritableOptions writableOptions)
=> _writableOptions = writableOptions;public void Run()
{
// Read value via IOptions
var option = _writableOptions.Value;// Write value via IWritableOptions
var newOption = new MyOptions();
_writableOptions.Update(newOption, reload: true);
}
}
```### Sample
- [Console App](https://github.com/nogic1008/WritableOptions.Net/tree/main/sandbox/ConsoleAppExample/)
- [Xamarin App](https://github.com/nogic1008/WritableOptions.Net/tree/main/sandbox/XamarinExample/)
- [.NET MAUI App](https://github.com/nogic1008/WritableOptions.Net/tree/main/sandbox/MauiExample/)
- [Blazor Server App](https://github.com/nogic1008/WritableOptions.Net/tree/main/sandbox/BlazorExample/)