-
Notifications
You must be signed in to change notification settings - Fork 42
/
variant.cpp
32 lines (27 loc) · 1.05 KB
/
variant.cpp
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
#include <alpaca/alpaca.h>
int main() {
struct MyStruct {
std::map<std::string, std::variant<uint16_t, std::string, bool,
std::vector<std::string>>>
value;
};
MyStruct s{{{"keepalive", true},
{"port", uint16_t{8080}},
{"ip_address", std::string{"192.168.8.1"}},
{"subscriptions",
std::vector<std::string>{"motor_state", "battery_state"}}}};
// serialize
std::vector<uint8_t> bytes;
auto bytes_written = alpaca::serialize(s, bytes); // 87 bytes
// deserialize
std::error_code ec;
auto recovered = alpaca::deserialize<MyStruct>(bytes, ec);
assert((bool)ec == false);
assert((recovered.value == std::map < std::string,
std::variant<uint16_t, std::string, bool, std::vector<std::string>>{
{"keepalive", true},
{"port", uint16_t{8080}},
{"ip_address", std::string{"192.168.8.1"}},
{"subscriptions",
std::vector<std::string>{"motor_state", "battery_state"}}}));
}