如何将此 class 序列化为 XML 或 JSON
How to serialize this class into XML or JSON
我有一个从名为 "Campus" 的 class 派生的对象列表,其中包含两个字符串,一个 int 和两个列表:一个用于 "Students",另一个用于 "Teachers",在关闭程序之前,我想保存校园对象,当然还有列表中包含的 "Student" 和 "Teachers" 对象,我想在 XML 中序列化这些数据或 JSON 格式或什至任何其他格式,然后将结果存储在文件中。
谁能告诉我在 XML 或 JSON 或其他解决方案中使用库(不像 boost 那样重)进行序列化的最快方法。遇到JSON或者XML序列化的时候,我不知道该怎么办了!
编辑:这对 RapidJSON 可行吗?
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
};
不幸的是,C++ 不支持反射,因此它无法自动计算出参数名称。但请查看这个看起来接近您想要的答案:
您可以使用这个 C++ 序列化库:Pakal persist
#include "XmlWriter.h"
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
public:
void persist(Archive* archive)
{
archive->value("city",city);
archive->value("region",region);
archive->value("capacity",capacity);
archive->value("Students","Student",students);
archive->value("Teachers","Teacher",teachers);
}
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
};
Campus c;
XmlWriter writer;
writer.write("campus.xml","Campus",c);
我有一个从名为 "Campus" 的 class 派生的对象列表,其中包含两个字符串,一个 int 和两个列表:一个用于 "Students",另一个用于 "Teachers",在关闭程序之前,我想保存校园对象,当然还有列表中包含的 "Student" 和 "Teachers" 对象,我想在 XML 中序列化这些数据或 JSON 格式或什至任何其他格式,然后将结果存储在文件中。
谁能告诉我在 XML 或 JSON 或其他解决方案中使用库(不像 boost 那样重)进行序列化的最快方法。遇到JSON或者XML序列化的时候,我不知道该怎么办了! 编辑:这对 RapidJSON 可行吗?
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
};
不幸的是,C++ 不支持反射,因此它无法自动计算出参数名称。但请查看这个看起来接近您想要的答案:
您可以使用这个 C++ 序列化库:Pakal persist
#include "XmlWriter.h"
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
public:
void persist(Archive* archive)
{
archive->value("city",city);
archive->value("region",region);
archive->value("capacity",capacity);
archive->value("Students","Student",students);
archive->value("Teachers","Teacher",teachers);
}
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
};
Campus c;
XmlWriter writer;
writer.write("campus.xml","Campus",c);