gpt4 book ai didi

c++ - 如何使用rapidjson(实现序列化)将对象序列化为std::string?

转载 作者:行者123 更新时间:2023-12-03 00:26:11 24 4
gpt4 key购买 nike

如何使用rapidjson将对象序列化为std::string?我已经实现了

class Person{
public:
std::string name;
uint64 id; // uint64 is typedef

template <typename Writer>
void Serialize(Writer& writer) const {
writer.StartObject();
writer.String("name");
writer.String(name);
writer.String(("id"));
writer.Uint64(id);
writer.EndObject();
}
std::string serialize(){
FileStream s(stdout);
PrettyWriter<FileStream> writer(s);
Serialize(writer);
return ? /// There is a problem

}
}

问题是序列化函数返回什么?

最佳答案

问题已经很老了,如果您仍在寻找答案,那么这是 @Lightness Races in Orbit 建议的答案

class Person{
public:
std::string name;
uint64 id; // uint64 is typedef

template <typename Writer>
void Serialize(Writer& writer) const {
writer.StartObject();
writer.String("name");
writer.String(name);
writer.String(("id"));
writer.Uint64(id);
writer.EndObject();
}
std::string serialize(){
StringBuffer s;
Writer<StringBuffer> writer(s);
Serialize(writer);
return s.GetString();
}
}

您还可以在此处查看示例代码:simplewriter.cpp

关于c++ - 如何使用rapidjson(实现序列化)将对象序列化为std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com