-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialize_third_party_class.cpp
56 lines (48 loc) · 1.26 KB
/
serialize_third_party_class.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
#include <iostream>
#include "bitserializer/bit_serializer.h"
#include "bitserializer/rapidjson_archive.h"
using namespace BitSerializer;
using JsonArchive = BitSerializer::Json::RapidJson::JsonArchive;
namespace MyApp
{
class TestThirdPartyClass
{
public:
TestThirdPartyClass(int x, int y) noexcept
: x(x), y(y)
{ }
// Example of public property
int x;
// Example of property that is only accessible via a getter/setter
[[nodiscard]] int GetY() const noexcept { return y; }
void SetY(const int inY) noexcept { this->y = inY; }
private:
int y;
};
// Serializes TestThirdPartyClass.
template<typename TArchive>
void SerializeObject(TArchive& archive, TestThirdPartyClass& testThirdPartyClass)
{
// Serialize public property
archive << KeyValue("x", testThirdPartyClass.x);
// Serialize private property
if constexpr (TArchive::IsLoading())
{
int y = 0;
archive << KeyValue("y", y);
testThirdPartyClass.SetY(y);
}
else
{
const int y = testThirdPartyClass.GetY();
archive << KeyValue("y", y);
}
}
}
int main() // NOLINT(bugprone-exception-escape)
{
auto testObj = MyApp::TestThirdPartyClass(100, 200);
const auto result = BitSerializer::SaveObject<JsonArchive>(testObj);
std::cout << result << std::endl;
return 0;
}