Table of Contents

Class SettingsBag

Namespace
Nucs.JsonSettings
Assembly
Nucs.JsonSettings.dll

A dynamic settings class, adds settings as you go.

public sealed class SettingsBag : JsonSettings, ISavable, IDisposable
Inheritance
SettingsBag
Implements
Inherited Members
Extension Methods

Remarks

The value store is thread-safe: it is backed by a ConcurrentDictionary<TKey, TValue> (through Nucs.JsonSettings.Collections.SafeDictionary<TKey, TValue>), so concurrent Get<T>(string, T), Set(string, object), Remove(string) and enumeration of Data neither corrupt state nor lose entries. Autosave-on-write (when Autosave is enabled) is a separate concern: the persistence path is not fully synchronized across threads, so drive saves from a single writer or coalesce a burst inside a SuspendAutosave scope.

Constructors

SettingsBag()

public SettingsBag()

SettingsBag(string)

public SettingsBag(string fileName)

Parameters

fileName string

Properties

Autosave

Will perform a safe after a change in any non-hardcoded public property.

[JsonIgnore]
public bool Autosave { get; set; }

Property Value

bool

Data

All the settings in this bag.

public IReadOnlyDictionary<string, object> Data { get; }

Property Value

IReadOnlyDictionary<string, object>

FileName

Serves as a reminder where to save or from where to load (if it is loaded on construction and doesnt change between constructions).
Can be relative to executing file's directory.

[JsonIgnore]
public override string FileName { get; set; }

Property Value

string

this[string]

public object? this[string key] { get; set; }

Parameters

key string

Property Value

object

Methods

AsDynamic()

Return a dynamic accessor that will accept any variable that can be serialized by Newtonsoft.Json. Index access ([]) or Property/Field is working.

public dynamic AsDynamic()

Returns

dynamic

EnableAutosave()

Enable autosave when a property is written.

public SettingsBag EnableAutosave()

Returns

SettingsBag

Get<T>(string, T)

Gets the value for key, converting it to T, or returns default when the key is missing or its value is null.

public T? Get<T>(string key, T @default = default)

Parameters

key string
default T

Returns

T

Type Parameters

T

Remarks

A value stored under one numeric width comes back under another after a round-trip -- Newtonsoft deserializes a JSON integer as long, so a value set as int is a boxed long once reloaded. A hard (T) unbox threw InvalidCastException on that, and a null value threw NullReferenceException. This coerces through ChangeType(object, Type) after the exact/assignable fast path, and treats a null the same as a missing key.

Remove(string)

public bool Remove(string key)

Parameters

key string

Returns

bool

RemoveWhere(Func<KeyValuePair<string, object>, bool>)

Removes all items that comprarer returns true to.

Remove where is similar to RemoveAll(Predicate<T>).

public int RemoveWhere(Func<KeyValuePair<string, object>, bool> comprarer)

Parameters

comprarer Func<KeyValuePair<string, object>, bool>

Returns

int

Set(string, object)

Sets or adds a value.

public void Set(string key, object value)

Parameters

key string
value object