c++ - rapidjson: How can I split a Document object to a smaller Document object? -
i working on c++ project , use rapidjson
json parsing. have json:
{ "a": "vala", "b": { "ba": "valba", "bb": "valbb", "bc": "valbc" }, "c": "valc" }
i parse whole json , document
object containing values. want somehow process document
object , extract b
part. if parsing json:
{ "b": { "ba": "valba", "bb": "valbb", "bc": "valbc" } }
i thought of parsing document
object myself wondering if there easier/faster way of doing that. ideas?
"b" element can extracted , put new document way:
#include <iostream> #include <rapidjson/document.h> #include <rapidjson/stringbuffer.h> #include <rapidjson/writer.h> using namespace rapidjson; int main(void) { const char* json = "{\"a\": \"vala\",\"b\": {\"ba\": \"valba\",\"bb\": \"valbb\",\"bc\": \"valbc\"},\"c\": \"valc\"}"; document d; d.parse<0>(json); value& data = d["b"]; document d2; d2.setobject(); d2.addmember("b", data, d2.getallocator()); rapidjson::stringbuffer buffer; rapidjson::writer<rapidjson::stringbuffer> writer(buffer); d2.accept(writer); std::cout << buffer.getstring() << std::endl; return 0; }
output:
{"b":{"ba":"valba","bb":"valbb","bc":"valbc"}}
Comments
Post a Comment