2

JsonCpp is slow. And the code is pretty messy.

Is there any alternative that is faster, cleaner and supports stuff like:

Json::Value val, copy;
val["newMember"] = 100;
val["newMember2"] = "hello";
copy = val;
val["newMember2"] = "bye";
assert(val["newMember"] == copy["newMember"]);
assert(val["newMember2"] != copy["newMember2"]);

JsonCpp supports code like the one above.

I've tried rapidjson, which is very fast, but unfortunately it does not support copying Json values.

Any alternative? Bonus point for benchmarks.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • Do you want to store JSON or do is OK to translate JSON text into C++ structures. – Martin York Jul 06 '13 at 05:48
  • Would this work for you: http://pastebin.com/LwH92T3w – Martin York Jul 06 '13 at 06:01
  • @LokiAstari I want to store JSON objects like jsoncpp does – Vittorio Romeo Jul 06 '13 at 11:02
  • 1
    You might look at the C++ API in [JPJson](https://github.com/couchdeveloper/JPJson), which is the core of its Objective-C API. It can generate a representation with standard containers and basically support the syntax you requested. It's quite fast as well - comparable to rapidjson. – CouchDeveloper Jul 10 '13 at 16:48

1 Answers1

4

After searching for some time the "documentation" I finally found a good way to copy JSON objects with rapidjson wich is very convenient:

rapidjson::Document doc; // This is the base document that you got from parsing etc
rapidjson::Value& v = doc["newMember"]; // newMember = 100

assert(v.GetInt() == 100);

rapidjson::Document copy;
doc.Accept(copy); // The accept meachnism is the same as used in parsing, but for copying

assert(copy["newMember"].GetInt() == doc["newMember"].GetInt())

The explicit copying has one advantage: It forces you to think clearly about when you are using references or potentially unnecessary copies.

grundprinzip
  • 2,471
  • 1
  • 20
  • 34
  • It raises a lot of `C2248` errors when compiling, of a lot of elements inside the `Accept` function saying all the same message: `cannot access private member declared in class 'rapidjson::GenericDocument'`. I'm using `doc.Accept(other.doc)` in the copy constructor. Do you know how to solve this? – SysDragon Mar 28 '14 at 07:31
  • Recently, a contribution added deep-copying API to rapidjson. https://github.com/miloyip/rapidjson/pull/20 – Milo Yip Jun 30 '14 at 07:27