-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmultiformat_customization.cpp
More file actions
47 lines (40 loc) · 1.12 KB
/
multiformat_customization.cpp
File metadata and controls
47 lines (40 loc) · 1.12 KB
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
#include <iostream>
#include "bitserializer/bit_serializer.h"
#include "bitserializer/rapidjson_archive.h"
#include "bitserializer/pugixml_archive.h"
using namespace BitSerializer;
using XmlArchive = BitSerializer::Xml::PugiXml::XmlArchive;
using JsonArchive = BitSerializer::Json::RapidJson::JsonArchive;
class CPoint
{
public:
CPoint(int x, int y)
: x(x), y(y)
{ }
template <class TArchive>
void Serialize(TArchive& archive)
{
// Serialize as attributes when archive type is XML
if constexpr (TArchive::archive_type == ArchiveType::Xml)
{
archive << AttributeValue("x", x);
archive << AttributeValue("y", y);
}
else
{
archive << KeyValue("x", x);
archive << KeyValue("y", y);
}
}
int x, y;
};
int main()
{
auto testObj = CPoint(100, 200);
const auto jsonResult = BitSerializer::SaveObject<JsonArchive>(testObj);
std::cout << "JSON: " << jsonResult << std::endl;
// Used explicitly defined root node name "Point" (to avoid auto-generated name "root")
const auto xmlResult = BitSerializer::SaveObject<XmlArchive>(KeyValue("Point", testObj));
std::cout << "XML: " << xmlResult << std::endl;
return 0;
}