-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.cpp
76 lines (61 loc) · 1 KB
/
example.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* CPP Seializer - Fast and simple data serialization for C++.
*
* https://swapped.ch/cpp-serializer
*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2019 Alex Pankratov. All rights reserved.
*/
#include "serialize.h"
#include "serialize_obj.h"
#include <assert.h>
/*
* 1. Define your structs
*/
struct foo
{
bool val_b;
std::string val_s;
uint16_t val_u;
double val_d;
};
struct bar
{
std::map<uint32_t, foo> baz;
std::vector<std::string> qux;
};
/*
* 2. Specify which fields to serialize
*/
OBJECT_SPEC( foo )
__f( val_b ),
__f( val_s ),
__f( val_u ),
__f( val_d )
END_OF_SPEC
OBJECT_SPEC( bar )
__f( baz ),
__f( qux )
END_OF_SPEC
/*
* 3. ... and done! Serialize at will.
*/
int main(int argc, char ** argv)
{
/*
* store
*/
buffer buf;
bar in;
store(buf, in);
/*
* parse
*/
parser par;
bar out;
par.init(buf);
parse(par, out);
assert( par.eof() );
// "out" is now the same as "in"
return 0;
}