use JsonConverterAttribute for Json serialization
This commit is contained in:
parent
8dd591d752
commit
5fb3f23799
|
@ -1,26 +1,14 @@
|
|||
using System.Text.Json;
|
||||
using InnovEnergy.Lib.Utils;
|
||||
using static InnovEnergy.Lib.Units.Units;
|
||||
|
||||
namespace InnovEnergy.Lib.StatusApi;
|
||||
|
||||
public abstract record DeviceStatus
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonSerializerOptions;
|
||||
|
||||
static DeviceStatus()
|
||||
{
|
||||
JsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
|
||||
JsonConverters.ForEach(JsonSerializerOptions.Converters.Add); // how stupid is that?!!
|
||||
}
|
||||
|
||||
public String DeviceType => GetType()
|
||||
.Generate(t => t.BaseType!)
|
||||
.Unfold(t => t.BaseType)
|
||||
.First(t => t.IsAbstract)
|
||||
.Name
|
||||
.Replace("Status", "");
|
||||
|
||||
public String ToJson() => JsonSerializer.Serialize(this, GetType(), JsonSerializerOptions);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Angle;
|
||||
|
||||
[JsonConverter(typeof(AngleConverter))]
|
||||
public readonly partial struct Angle
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = ApparentPower;
|
||||
|
||||
[JsonConverter(typeof(ApparentPowerConverter))]
|
||||
public readonly partial struct ApparentPower
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Current;
|
||||
|
||||
[JsonConverter(typeof(CurrentConverter))]
|
||||
public readonly partial struct Current
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Frequency;
|
||||
|
||||
[JsonConverter(typeof(FrequencyConverter))]
|
||||
public readonly partial struct Frequency
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Template;
|
||||
|
||||
[JsonConverter(typeof(TemplateConverter))]
|
||||
public readonly partial struct Template
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Number;
|
||||
|
||||
[JsonConverter(typeof(NumberConverter))]
|
||||
public readonly partial struct Number
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Power;
|
||||
|
||||
[JsonConverter(typeof(PowerConverter))]
|
||||
public readonly partial struct Power
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = ReactivePower;
|
||||
|
||||
[JsonConverter(typeof(ReactivePowerConverter))]
|
||||
public readonly partial struct ReactivePower
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Resistance;
|
||||
|
||||
[JsonConverter(typeof(ResistanceConverter))]
|
||||
public readonly partial struct Resistance
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Temperature;
|
||||
|
||||
[JsonConverter(typeof(TemperatureConverter))]
|
||||
public readonly partial struct Temperature
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -16,4 +16,5 @@ public readonly partial struct Voltage
|
|||
|
||||
// P=UI
|
||||
public static Power operator *(Voltage voltage, Current current) => new Power(current.Value * voltage.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
|
|||
|
||||
using T = Voltage;
|
||||
|
||||
[JsonConverter(typeof(VoltageConverter))]
|
||||
public readonly partial struct Voltage
|
||||
{
|
||||
public Decimal Value { get; }
|
||||
|
|
|
@ -243,7 +243,7 @@ public static class EnumerableUtils
|
|||
return ts.ElementAtOrDefault(index) ?? defaultValue;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Generate<T>(this T seed, Func<T, T> next)
|
||||
public static IEnumerable<T> Unfold<T>(this T seed, Func<T, T?> next)
|
||||
{
|
||||
var value = seed;
|
||||
while (value is not null)
|
||||
|
|
Loading…
Reference in New Issue