I have a class that contains simple attribute and I want to convert it to json. The method to generate json is below:
std::string Message::GetIncomingMessage(){
rapidjson::Document d; // Null
d.SetObject();
rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
rapidjson::Value from;
from.SetString(rapidjson::StringRef(this->from.c_str(), this->from.length()));
rapidjson::Value id;
id.SetString(rapidjson::StringRef(this->id.c_str(), this->id.length()));
rapidjson::Value status;
status.SetInt(this->status);
rapidjson::Value type;
type.SetInt(this->type);
rapidjson::Value body;
body.SetString(rapidjson::StringRef(this->body.c_str(), this->body.length()));
d.AddMember("From", from, allocator);
d.AddMember("ID", id, allocator);
d.AddMember("Status", status, allocator);
d.AddMember("Type", type, allocator);
d.AddMember("Body", body, allocator);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
return buffer.GetString();
}
It works normal but I noticed for the log, sometime this function took around 1 sec to finish. After check the timing for each function call, the line that took 1 sec is the d.SetObject().
What happened here? Is there any explanation and how to fix this?
Aucun commentaire:
Enregistrer un commentaire