You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Creates a lambda which captures `buf` by reference and appends data to it when invoked
22
-
auto out = sink(buf);
23
-
24
-
// The first argument can be any function object with signature void(const char* data, size_t len) (when C++20 is used, it satisfies the sink_type concept)
25
-
serialize(out, a);
26
-
27
-
// Creates a mutable lambda which captures `buf` by reference and reads data from it when invoked
28
-
auto in = source(buf);
29
-
30
-
// The first argument can be any function object with signature void(char* data, size_t len) (when C++20 is used, it satisfies the source_type concept)
31
-
deserialize(in, a);
32
-
```
33
-
34
-
#### `std::vector`
35
-
36
-
```
37
-
using namespace msgpackcpp;
38
-
39
-
std::vector<int> v1(10);
40
-
std::iota(begin(v1), end(v1), 0);
41
-
std::vector<int> v2;
42
-
43
-
// You can also serialize into an ostream object
44
-
std::ostringstream sout;
45
-
auto out = sink(sout);
46
-
serialize(out, v1);
47
-
48
-
// Deserialize from an istream object
49
-
std::istringstream sin(sout.str());
50
-
auto in = source(sin);
51
-
deserialize(in, v2);
52
-
```
53
-
54
-
#### `std::map`
12
+
```cpp
13
+
#include"msgpack.h"
14
+
#include"msgpack_sinks.h"
55
15
16
+
intmain()
17
+
{
18
+
using msgpackcpp::serialize;
19
+
using msgpackcpp::deserialize;
20
+
using msgpackcpp::sink;
21
+
using msgpackcpp::source;
22
+
23
+
// Some data
24
+
int a = 1, aa;
25
+
double b = 3.15, bb;
26
+
std::string c = "hello there", cc;
27
+
std::vector<char> d = {0,1,2,3,4,5,6,7,8,9}, dd;
28
+
std::map<std::string, int> e = {{"a", 1}, {"b", 2}}, ee;
29
+
30
+
31
+
// Serialize
32
+
std::vector<char> buf;
33
+
auto out = sink(buf);
34
+
serialize(out, a);
35
+
serialize(out, b);
36
+
serialize(out, c);
37
+
serialize(out, d);
38
+
serialize(out, e);
39
+
40
+
// Deserialize
41
+
auto in = source(buf);
42
+
deserialize(in, aa);
43
+
deserialize(in, bb);
44
+
deserialize(in, cc);
45
+
deserialize(in, dd);
46
+
deserialize(in, ee);
47
+
}
56
48
```
57
-
using namespace msgpackcpp;
58
49
59
-
std::map<std::string, int> a = {{"a", 1}, {"b", 2}};
60
-
std::map<std::string, int> b;
50
+
### Custom types
61
51
62
-
std::vector<char> buf;
63
-
auto out = sink(buf);
64
-
serialize(out, a);
52
+
Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct
65
53
66
-
auto in = source(buf);
67
-
deserialize(in, b);
68
-
```
54
+
```cpp
55
+
#include"msgpack.h"
56
+
#include"msgpack_sinks.h"
69
57
70
-
#### Custom object
71
-
72
-
Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct. This will get picked up by ADL.
0 commit comments