-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacteristicConverter.cs
51 lines (47 loc) · 1.73 KB
/
CharacteristicConverter.cs
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
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using HomeKit.Characteristics;
namespace HomeKit
{
internal class CharacteristicConverter : JsonConverter<Characteristic>
{
public override Characteristic Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException("Deserialization is not implemented");
}
public override void Write(Utf8JsonWriter writer, Characteristic value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("iid", value.Iid);
writer.WriteString("type", value.Type);
writer.WriteString("format", value.Format);
writer.WriteStartArray("perms");
foreach (var perm in value.Perms)
{
writer.WriteStringValue(perm);
}
writer.WriteEndArray();
if (value is BoolCharacteristic boolCharacteristic)
{
if (value is not IdentifyCharacteristic)
{
writer.WriteBoolean("value", boolCharacteristic.Value);
}
}
else if (value is FloatCharacteristic floatCharacteristic)
{
writer.WriteNumber("value", floatCharacteristic.Value);
}
else if (value is StringCharacteristic stringCharacteristic)
{
writer.WriteString("value", stringCharacteristic.Value);
}
else
{
throw new NotImplementedException("ACharacteristicConverter: " + value.GetType().FullName);
}
writer.WriteEndObject();
}
}
}