0

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;
        }
    };
Community
  • 1
  • 1
florgeng
  • 856
  • 1
  • 9
  • 17
  • Why are you using `shared_ptr`s? Return a `Json::Value` instead of a `std::shared_ptr` and your code should become simpler and much faster. – nwp Mar 28 '17 at 15:36
  • Assuming by "stack objects" you mean objects with automatic storage duration, that's false; there is loads of dynamic allocation here, some in your own code. – Lightness Races in Orbit Mar 28 '17 at 15:36
  • @nwp I don't get your comment, because my question is exactly about not to the `Json::Value` @BoundaryImposition usually I feel right when speaking about stack and heap objects, I think we mean the same, can you give me a hint were to look up the correct terms? – florgeng Mar 29 '17 at 06:32
  • I think you forgot a word there, "not to *copy* the `Json::Value`". While it looks like code of the form `Type value = create_value();` makes a copy compilers are generally smart enough to optimize it away, even without optimization flags enabled, and let `create_value` create the value directly in its final place. You are inhibiting that optimization with the `shared_ptr`. – nwp Mar 29 '17 at 08:10
  • @nwp ah ok, with the hint to relay on compiler optimization I understand, thank you guys. – florgeng Mar 29 '17 at 11:58

1 Answers1

1

JsonCpp does not support move semantics — this is issue #223.

Until it does, you cannot entirely avoid copies.

However, if you make your code simple by getting rid of the needless dynamic allocation and smart pointers, you may get lucky and see your compiler optimising away some of it (via mechanisms like return value optimisation). But not all of it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055