add SettingsValueOption
This commit is contained in:
6
Pilz.Configuration/ISettingsValueOptionValueAccessor.cs
Normal file
6
Pilz.Configuration/ISettingsValueOptionValueAccessor.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Pilz.Configuration;
|
||||||
|
|
||||||
|
internal interface ISettingsValueOptionValueAccessor
|
||||||
|
{
|
||||||
|
public object ValueRaw { get; set; }
|
||||||
|
}
|
||||||
49
Pilz.Configuration/SettingsValueOption.cs
Normal file
49
Pilz.Configuration/SettingsValueOption.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Pilz.Configuration;
|
||||||
|
|
||||||
|
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
[JsonConverter(typeof(SettingsValueOptionJsonConverter))]
|
||||||
|
public class SettingsValueOption<T> : ISettingsValueOptionValueAccessor where T : struct
|
||||||
|
{
|
||||||
|
protected T? value;
|
||||||
|
|
||||||
|
public T DefaultValue { get; }
|
||||||
|
public virtual bool IsDefault => value == null;
|
||||||
|
|
||||||
|
public SettingsValueOption()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public SettingsValueOption(T value)
|
||||||
|
{
|
||||||
|
this.value = DefaultValue = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual T Value
|
||||||
|
{
|
||||||
|
get => value ?? DefaultValue;
|
||||||
|
set => this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator T(SettingsValueOption<T> option)
|
||||||
|
{
|
||||||
|
return option.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
object ISettingsValueOptionValueAccessor.ValueRaw
|
||||||
|
{
|
||||||
|
get => value;
|
||||||
|
set => this.value = (T?)value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Reset()
|
||||||
|
{
|
||||||
|
value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
27
Pilz.Configuration/SettingsValueOptionJsonConverter.cs
Normal file
27
Pilz.Configuration/SettingsValueOptionJsonConverter.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Pilz.Configuration;
|
||||||
|
|
||||||
|
public class SettingsValueOptionJsonConverter : JsonConverter
|
||||||
|
{
|
||||||
|
public override bool CanConvert(Type objectType)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (existingValue is ISettingsValueOptionValueAccessor option)
|
||||||
|
option.ValueRaw = reader.Value;
|
||||||
|
return existingValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (value is ISettingsValueOptionValueAccessor option)
|
||||||
|
writer.WriteValue(option.ValueRaw);
|
||||||
|
else
|
||||||
|
writer.WriteNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user