forked from KoenZomers/TadoApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceTypeConverter.cs
More file actions
55 lines (47 loc) · 1.43 KB
/
Copy pathDeviceTypeConverter.cs
File metadata and controls
55 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Text.Json;
using System.Text.Json.Serialization;
namespace KoenZomers.Tado.Api.Converters;
/// <summary>
/// Converts the Tado device type returned by the Tado API to the DeviceTypes enumerator in this project
/// </summary>
public class DeviceTypeConverter : JsonConverter<Enums.DeviceTypes?>
{
public override Enums.DeviceTypes? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
{
return null;
}
var enumString = reader.GetString();
if (string.IsNullOrEmpty(enumString))
{
return null;
}
return enumString switch
{
"HEATING" => Enums.DeviceTypes.Heating,
"HOT_WATER" => Enums.DeviceTypes.HotWater,
_ => null
};
}
public override void Write(Utf8JsonWriter writer, Enums.DeviceTypes? value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
switch (value)
{
case Enums.DeviceTypes.Heating:
writer.WriteStringValue("HEATING");
break;
case Enums.DeviceTypes.HotWater:
writer.WriteStringValue("HOT_WATER");
break;
default:
writer.WriteNullValue();
break;
}
}
}