Faster JsonCpp alternative that allows copying/mutability of Json objects?

独自空忆成欢 提交于 2019-12-01 08:54:57

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17498812/faster-jsoncpp-alternative-that-allows-copying-mutability-of-json-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!