add JsonConverters for Units

This commit is contained in:
ig 2023-02-23 13:53:39 +01:00
parent 2328dd8d13
commit 55f70f056d
5 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
using InnovEnergy.Units.Json;
// ReSharper disable once CheckNamespace
namespace InnovEnergy.Units;
public static partial class Units
{
public static IReadOnlyList<JsonConverter> JsonConverters = new JsonConverter[]
{
new CurrentConverter(),
new VoltageConverter(),
new PowerConverter(),
new ResistanceConverter()
};
}

View File

@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace InnovEnergy.Units.Json;
public class CurrentConverter : JsonConverter<Current>
{
public override Current Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Current(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, Current value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}

View File

@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace InnovEnergy.Units.Json;
public class PowerConverter : JsonConverter<Power>
{
public override Power Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Power(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, Power value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}

View File

@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace InnovEnergy.Units.Json;
public class ResistanceConverter : JsonConverter<Current>
{
public override Current Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Current(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, Current value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}

View File

@ -0,0 +1,17 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace InnovEnergy.Units.Json;
public class VoltageConverter : JsonConverter<Voltage>
{
public override Voltage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new Voltage(reader.GetDecimal());
}
public override void Write(Utf8JsonWriter writer, Voltage value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}