-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
…nd improved error reporting (#984) * Allow any nullable double fields to be strings or doubles and improved error reporting * small fix
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace NetDaemon.HassModel.CodeGenerator.Model; | ||
|
||
class StringAsDoubleConverter : JsonConverter<double?> | ||
{ | ||
public override double? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// Some fields (step) can have a string or a numeric value. If it is a string we will try to parse it to a decimal | ||
return reader.TokenType switch | ||
{ | ||
JsonTokenType.Number => reader.GetDouble(), | ||
JsonTokenType.String => double.TryParse(reader.GetString(), out var d) ? d : null, | ||
_ => Skip(ref reader) | ||
Check warning on line 14 in src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs Codecov / codecov/patchsrc/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs#L14
|
||
}; | ||
} | ||
|
||
double? Skip(ref Utf8JsonReader reader) | ||
Check warning on line 18 in src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs GitHub Actions / 📦 publish nuget packages
|
||
{ | ||
reader.Skip(); | ||
return null; | ||
Check warning on line 21 in src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs Codecov / codecov/patchsrc/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs#L20-L21
|
||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, double? value, JsonSerializerOptions options) => throw new NotSupportedException(); | ||
Check warning on line 24 in src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs Codecov / codecov/patchsrc/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/StringAsDoubleConverter.cs#L24
|
||
} |