-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserialize_to_stream.cpp
More file actions
46 lines (37 loc) · 1.04 KB
/
serialize_to_stream.cpp
File metadata and controls
46 lines (37 loc) · 1.04 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
#include <iostream>
#include <sstream>
#include "bitserializer/bit_serializer.h"
#include "bitserializer/rapidjson_archive.h"
using namespace BitSerializer;
using JsonArchive = BitSerializer::Json::RapidJson::JsonArchive;
class CPoint
{
public:
CPoint() = default;
CPoint(int x, int y)
: x(x), y(y)
{ }
template <class TArchive>
void Serialize(TArchive& archive)
{
archive << KeyValue("x", x);
archive << KeyValue("y", y);
}
int x = 0, y = 0;
};
int main()
{
auto testObj = CPoint(100, 200);
SerializationOptions serializationOptions;
serializationOptions.streamOptions.encoding = Convert::UtfType::Utf8;
serializationOptions.streamOptions.writeBom = false;
// Save to string stream
std::stringstream outputStream;
BitSerializer::SaveObject<JsonArchive>(testObj, outputStream, serializationOptions);
std::cout << outputStream.str() << std::endl;
// Load from string stream
CPoint loadedObj;
BitSerializer::LoadObject<JsonArchive>(loadedObj, outputStream);
assert(loadedObj.x == testObj.x && loadedObj.y == testObj.y);
return 0;
}