-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.cpp
56 lines (44 loc) · 1.54 KB
/
example2.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
#include <string>
#include "ThorSerialize/Traits.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/JsonThor.h"
/* A class that you want to serialize. */
class MyClass
{
int data1;
float data2;
std::string data3;
public:
MyClass(int data1, float data2, std::string const& data3)
: data1(data1)
, data2(data2)
, data3(data3)
{}
// This is only required if the members are private.
friend struct ThorsAnvil::Serialize::Traits<MyClass>;
};
/*
* Though there is no code involved, you do need to set up
* this structure to tell the library what fields need to be serialized.
* To do this use the macro: ThorsAnvil_MakeTrait()
* Specifying your class, and a list of members to serialize.
*/
ThorsAnvil_MakeTrait(MyClass, data1, data2, data3);
int main()
{
using ThorsAnvil::Serialize::jsonExporter;
using ThorsAnvil::Serialize::PrinterInterface;
MyClass data {56, 23.456, "Hi there"};
// This generates a simple Json Object (wordy)
std::cout << "Version 1\n";
std::cout << jsonExporter(data) << "\n\n\n";
// This generates a compact Json
std::cout << "Version 2 (Stream)\n";
std::cout << jsonExporter(data, PrinterInterface::OutputType::Stream) << "\n\n\n";
// Standard containers work automatically.
// As long as the type held by the container has had an appropriate
// Traits declaration.
std::vector<MyClass> vec(4, data);
std::cout << "Vector\n";
std::cout << jsonExporter(vec) << "\n";
}