I am trying to make a simple "person" vector of objects which can be saved and then loaded from file to vector of objects. I made a function found in some tutorials which returns all the content from class, but I have no clue what to do now, to put it into a file. Should I use i/o stream operators or something? At now I have the following code:
#include <iostream> // person.h
#include <string>
#include <vector>
using namespace std;
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson;
class person {
public:
std::string name;
string surname;
int age;
person();
~person();
person(string, string, int);
template <typename Writer>
void Serialize(Writer& writer) const {
writer.StartObject();
writer.String("name");
writer.String(name.c_str());
writer.String("surname");
writer.String(surname.c_str());
writer.String(("id"));
writer.Uint(age);
writer.EndObject();
}
std::string serialize(){
StringBuffer s;
Writer<StringBuffer> writer(s);
Serialize(writer);
return s.GetString();
}
};
#include "person.h" // person.cpp
person::person() {
}
person::~person() {
}
person::person(string name, string surname, int age) : name(name), surname(surname), age(age) {
}
#include "person.h" // main.cpp
int main() {
vector<person> Save;
person P1("Tak", "Nie", 20);
person P2("Yes", "No", 10);
Save.push_back(P1);
Save.push_back(P2);
cout << P1.serialize();
cout << P2.serialize();
return 0;
}
There is no point to have vector of person
, as it will be very complex to serialize and store as JSON.
Instead, you can maintain serialized object strings in a vector
and store them as an json
array into a Document
.
You can store your person json document to local storage with FileWriteStream
:
#include "include/rapidjson/writer.h"
#include "include/rapidjson/stringbuffer.h"
#include "include/rapidjson/document.h"
#include "include/rapidjson/ostreamwrapper.h"
#include "include/rapidjson/filewritestream.h"
#include "include/rapidjson/filereadstream.h"
#include "fstream"
#include "iostream"
#include "sstream"
vector<string> Save; // replace vector<person> with vector<string>
//
person P1("Tak", "Nie", 20);
person P2("Yes", "No", 10);
// more persons
//
Save.push_back(P1.serialize());
Save.push_back(P2.serialize());
// push more persons
//
Document d; // rapidjson Document
d.SetArray(); // to store Array of objects
rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
// iterate over objects vector created above
// and create rapidjson Values for each object
// Then store the object in Document
for (auto it = Save.begin(); it!=Save.end(); it++){
Value n((*it).c_str(), allocator);
d.PushBack(n,allocator);
}
// save rapidjson Document to local storage file name "output.json"
FILE* fp = fopen("output.json", "wb");
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
Writer<FileWriteStream> writer(os);
d.Accept(writer);
fclose(fp);
Now all your objects were serialized, store as elements in array in the document.
Now to restore the objects into an array of classes of person
, say P[]
, we can do the reverse, first use FileReadStream
to restore stored rapidjson
array into a Document
, then iterate over your document an fetch objects one by one, use the data to create new person
classes:
// restore saved rapidjson Document from file "output.json"
FILE* fp = fopen("output.json", "rb"); // non-Windows use "r"
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);
fclose(fp);
//
assert(d.IsArray());
int size= d.Size();
person* P[size+1]; // Now create an array of class person to restore retrieved objects.
// person object paramets
std::string theName;
std::string theSur;
int theid;
// loop over the Array Document
for (int i = 0; i < d.Size(); i++)
{
Document P1_doc; // new rapidjson Document
P1_doc.Parse(d[i].GetString()); // parse each object from d into new Dcoument P1_doc
assert(P1_doc.IsObject());
// now restore each person object data
if(P1_doc.HasMember("name")){
const rapidjson::Value& name = P1_doc["name"];
theName = name.GetString();
}
if (P1_doc.HasMember("surname")){
const rapidjson::Value& surname = P1_doc["surname"];
theSur = surname.GetString();
}
if (P1_doc.HasMember("id")){
const rapidjson::Value& id = P1_doc["id"];
theid = id.GetInt();
}
// Now restore a person object from retrieved data
P[i] = new person(theName, theSur, theid);
}
return 0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.