-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend.cpp
More file actions
111 lines (87 loc) · 2.9 KB
/
Copy pathextend.cpp
File metadata and controls
111 lines (87 loc) · 2.9 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "serialization.hpp"
struct StructCustom {
int a = 0;
bool b = false;
};
// extend PropertyBase
class PropertyObjectCustom : public PropertyBase
{
public:
PropertyObjectCustom(std::string name, StructCustom defaultValue) :m_data(defaultValue), PropertyBase(name) {}
PropertyObjectCustom(std::string name, StructCustom defaultValue, std::vector<PropertyBase*>& list)
:m_data(defaultValue), PropertyBase(name)
{
list.push_back(this);
}
void Set(StructCustom value) { m_data = value; }
StructCustom Get() { return m_data; }
// 按保存的结构实现序列化接口
virtual std::string Serialize() override {
rapidjson::StringBuffer strBuffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuffer);
writer.StartObject();
writer.Key(m_name.c_str());
writer.StartObject();
writer.Key("a");
writer.Int(m_data.a);
writer.Key("b");
writer.Bool(m_data.b);
writer.EndObject();
writer.EndObject();
return strBuffer.GetString();
}
// 按保存的结构实现反序列化接口
virtual bool DeSerialize(std::string& data) override {
rapidjson::Document doc;
doc.Parse(data.c_str());
const char* name = m_name.c_str();
if (!doc.HasParseError())
if (doc.HasMember(name) && doc[name].IsObject())
{
if (doc[name].HasMember("a") && doc[name]["a"].IsInt())
m_data.a = doc[name]["a"].GetInt();
else
return false;
if (doc[name].HasMember("b") && doc[name]["b"].IsBool())
m_data.a = doc[name]["b"].GetBool();
else
return false;
return true;
}
return false;
}
private:
StructCustom m_data;
};
// 可以自己写个宏定义简化定义,也可以单独声明,如下例子所示
#define PropertyCustom(name, type, defaultValue, propertyDeliver) \
private: \
propertyDeliver* m_##name = new propertyDeliver(#name, defaultValue, m_objs); \
public: \
type Get##name(){return m_##name->Get();} \
void Set##name(type val){m_##name->Set(val);}
class CarInfo: public SerializeBase
{
public:
GroupName("CarInfo");
Property(Speed, int, 0);
PropertyArray(ArrayInt, int);
// custom struct
PropertyCustom(MyStruct, StructCustom, StructCustom(), PropertyObjectCustom);
// or you can also define like this
PropertyObjectCustom* myObjectStructCustom = new PropertyObjectCustom("MyStruct2", { 20,true }, m_objs);
};
int main()
{
CarInfo carInfo;
std::string sGroupName = carInfo.GetGroupName();
carInfo.SetSpeed(28);
carInfo.SetMyStruct({ 222,true });
std::string strJson = carInfo.ToSerializeString();
CarInfo structCustom2;
bool bSuccess = structCustom2.FromSerializeString(strJson);
int speed = structCustom2.GetSpeed();
// 这样就获取到了自定义的结构数据,同理数组也可以自定义,参考PropertyArrayBase
StructCustom mystruct = structCustom2.GetMyStruct();
return 0;
}