With JsonCpp I want to serialize big objects with limited stack resources on an embedded device. All the examples I found are using stack objects which will be copied into each other (I guess). I want to reduce the copying the Json::Value objects all the time, but still using clustered code -- so that each object just needs to know how to serialize itself. I prepared a minimal example, which orientates to the described memory management from this answer https://stackoverflow.com/a/42829726
But in my example (in the end) there is still an not needed/wanted copy:
(*p)["a"] = *a.toJson(); // value will be copied into new instance
Can this be avoided somehow with JsonCpp?
struct itoJson
{
std::shared_ptr<Json::Value> toJson();
};
struct A: itoJson
{
int i;
std::shared_ptr<Json::Value> toJson()
{
std::shared_ptr<Json::Value> p = std::shared_ptr<Json::Value>(new Json::Value());
(*p)["i"] = i;
return p;
}
};
struct B: itoJson
{
int x;
A a;
std::shared_ptr<Json::Value> toJson()
{
std::shared_ptr<Json::Value> p = std::shared_ptr<Json::Value>(new Json::Value());
(*p)["x"] = x;
(*p)["a"] = *a.toJson(); // value will be copied into new instance
return p;
}
};