use JsonConverterAttribute for Json serialization

This commit is contained in:
ig 2023-03-06 09:52:27 +01:00
parent 8dd591d752
commit 5fb3f23799
14 changed files with 15 additions and 15 deletions

View File

@ -1,26 +1,14 @@
using System.Text.Json;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static InnovEnergy.Lib.Units.Units;
namespace InnovEnergy.Lib.StatusApi; namespace InnovEnergy.Lib.StatusApi;
public abstract record DeviceStatus 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() public String DeviceType => GetType()
.Generate(t => t.BaseType!) .Unfold(t => t.BaseType)
.First(t => t.IsAbstract) .First(t => t.IsAbstract)
.Name .Name
.Replace("Status", ""); .Replace("Status", "");
public String ToJson() => JsonSerializer.Serialize(this, GetType(), JsonSerializerOptions);
} }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Angle; using T = Angle;
[JsonConverter(typeof(AngleConverter))]
public readonly partial struct Angle public readonly partial struct Angle
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = ApparentPower; using T = ApparentPower;
[JsonConverter(typeof(ApparentPowerConverter))]
public readonly partial struct ApparentPower public readonly partial struct ApparentPower
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Current; using T = Current;
[JsonConverter(typeof(CurrentConverter))]
public readonly partial struct Current public readonly partial struct Current
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Frequency; using T = Frequency;
[JsonConverter(typeof(FrequencyConverter))]
public readonly partial struct Frequency public readonly partial struct Frequency
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Template; using T = Template;
[JsonConverter(typeof(TemplateConverter))]
public readonly partial struct Template public readonly partial struct Template
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Number; using T = Number;
[JsonConverter(typeof(NumberConverter))]
public readonly partial struct Number public readonly partial struct Number
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Power; using T = Power;
[JsonConverter(typeof(PowerConverter))]
public readonly partial struct Power public readonly partial struct Power
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = ReactivePower; using T = ReactivePower;
[JsonConverter(typeof(ReactivePowerConverter))]
public readonly partial struct ReactivePower public readonly partial struct ReactivePower
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Resistance; using T = Resistance;
[JsonConverter(typeof(ResistanceConverter))]
public readonly partial struct Resistance public readonly partial struct Resistance
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Temperature; using T = Temperature;
[JsonConverter(typeof(TemperatureConverter))]
public readonly partial struct Temperature public readonly partial struct Temperature
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -16,4 +16,5 @@ public readonly partial struct Voltage
// P=UI // P=UI
public static Power operator *(Voltage voltage, Current current) => new Power(current.Value * voltage.Value); public static Power operator *(Voltage voltage, Current current) => new Power(current.Value * voltage.Value);
} }

View File

@ -10,6 +10,7 @@ namespace InnovEnergy.Lib.Units;
using T = Voltage; using T = Voltage;
[JsonConverter(typeof(VoltageConverter))]
public readonly partial struct Voltage public readonly partial struct Voltage
{ {
public Decimal Value { get; } public Decimal Value { get; }

View File

@ -243,7 +243,7 @@ public static class EnumerableUtils
return ts.ElementAtOrDefault(index) ?? defaultValue; 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; var value = seed;
while (value is not null) while (value is not null)