1
|
using System.Text.Json;
|
2
|
using System.Text.Json.Serialization;
|
3
|
using Newtonsoft.Json;
|
4
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
5
|
|
6
|
namespace LDClient.network.data {
|
7
|
|
8
|
/// <summary>
|
9
|
/// This class represents a single payload that is sent to the server.
|
10
|
/// </summary>
|
11
|
[JsonObject(MemberSerialization.OptIn)]
|
12
|
public class Payload {
|
13
|
|
14
|
/// <summary>
|
15
|
/// Username of the currently logged user.
|
16
|
/// </summary>
|
17
|
[JsonPropertyName("username")]
|
18
|
public string? UserName { get; set; }
|
19
|
|
20
|
/// <summary>
|
21
|
/// Hostname of the pc.
|
22
|
/// </summary>
|
23
|
[JsonPropertyName("hostname")]
|
24
|
public string? HostName { get; set; }
|
25
|
|
26
|
/// <summary>
|
27
|
/// Timestamp (when a debugger was plugged/unplugged).
|
28
|
/// </summary>
|
29
|
[JsonPropertyName("timestamp")]
|
30
|
public string? TimeStamp { get; set; }
|
31
|
|
32
|
/// <summary>
|
33
|
/// Information about the head of the debugger.
|
34
|
/// </summary>
|
35
|
[JsonPropertyName("head_device")]
|
36
|
public DebuggerInfo? HeadDevice { get; set; }
|
37
|
|
38
|
/// <summary>
|
39
|
/// Information about the body of the debugger.
|
40
|
/// </summary>
|
41
|
[JsonPropertyName("body_device")]
|
42
|
public DebuggerInfo? BodyDevice { get; set; }
|
43
|
|
44
|
/// <summary>
|
45
|
/// Status of the debugger (connected/disconnected).
|
46
|
/// </summary>
|
47
|
[JsonPropertyName("status")]
|
48
|
public ConnectionStatus Status { get; set; }
|
49
|
|
50
|
/// <summary>
|
51
|
/// Returns a string representation of the payload.
|
52
|
/// </summary>
|
53
|
/// <returns></returns>
|
54
|
public override string ToString() {
|
55
|
return ParseToJson(this);
|
56
|
}
|
57
|
|
58
|
/// <summary>
|
59
|
/// Parses (converts) the payload into JSON format.
|
60
|
/// </summary>
|
61
|
/// <returns></returns>
|
62
|
public string ParseToJson() {
|
63
|
return Payload.ParseToJson(this);
|
64
|
}
|
65
|
|
66
|
/// <summary>
|
67
|
/// Serializes a given payload into JSON format.
|
68
|
/// </summary>
|
69
|
/// <param name="payload">payload to be serialized into JSON</param>
|
70
|
/// <returns></returns>
|
71
|
public static string ParseToJson(Payload payload) {
|
72
|
// Create options for serialization.
|
73
|
var options = new JsonSerializerOptions {
|
74
|
Converters = {
|
75
|
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
|
76
|
}
|
77
|
};
|
78
|
// Serialize the payload and return it.
|
79
|
return JsonSerializer.Serialize(payload, options);
|
80
|
}
|
81
|
}
|
82
|
}
|