-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserialize_custom_string.cpp
More file actions
93 lines (82 loc) · 2.63 KB
/
serialize_custom_string.cpp
File metadata and controls
93 lines (82 loc) · 2.63 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include "bitserializer/bit_serializer.h"
#include "bitserializer/rapidjson_archive.h"
#include "bitserializer/types/std/vector.h"
#include "bitserializer/types/std/map.h"
using namespace BitSerializer;
using JsonArchive = BitSerializer::Json::RapidJson::JsonArchive;
// Some custom string type
class CMyString
{
public:
CMyString() = default;
CMyString(const char* str) : mString(str) { }
bool operator<(const CMyString& rhs) const { return this->mString < rhs.mString; }
const char* data() const noexcept { return mString.data(); }
size_t size() const noexcept { return mString.size(); }
// Required methods for conversion from/to std::string (can be implemented as external functions)
std::string ToString() const { return mString; }
void FromString(std::string_view str) { mString = str; }
private:
std::string mString;
};
// Serializes CMyString with key
template <class TArchive, typename TKey>
bool Serialize(TArchive& archive, TKey&& key, CMyString& value)
{
if constexpr (TArchive::IsLoading())
{
std::string_view stringView;
if (Detail::SerializeString(archive, std::forward<TKey>(key), stringView))
{
value.FromString(stringView);
return true;
}
}
else
{
std::string_view stringView(value.data(), value.size());
return Detail::SerializeString(archive, std::forward<TKey>(key), stringView);
}
return false;
}
// Serializes CMyString without key
template <class TArchive>
bool Serialize(TArchive& archive, CMyString& value)
{
if constexpr (TArchive::IsLoading())
{
std::string_view stringView;
if (Detail::SerializeString(archive, stringView))
{
value.FromString(stringView);
return true;
}
}
else
{
std::string_view stringView(value.data(), value.size());
return Detail::SerializeString(archive, stringView);
}
return false;
}
int main()
{
// Save list of custom strings to JSON
std::vector<CMyString> srcStrList = { "Red", "Green", "Blue" };
std::string jsonResult;
SerializationOptions serializationOptions;
serializationOptions.formatOptions.enableFormat = true;
BitSerializer::SaveObject<JsonArchive>(srcStrList, jsonResult, serializationOptions);
std::cout << "Saved JSON: " << jsonResult << std::endl;
// Load JSON-object to std::map based on custom strings
std::map<CMyString, CMyString> mapResult;
const std::string srcJson = R"({ "Background": "Blue", "PenColor": "White", "PenSize": "3", "PenOpacity": "50" })";
BitSerializer::LoadObject<JsonArchive>(mapResult, srcJson);
std::cout << std::endl << "Loaded map: " << std::endl;
for (const auto& val : mapResult)
{
std::cout << "\t" << val.first.ToString() << ": " << val.second.ToString() << std::endl;
}
return 0;
}