Created
December 15, 2015 21:56
-
-
Save courtarro/fe881db0258b55d69d2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "rapidjson/document.h" | |
#include "rapidjson/pointer.h" | |
#include "rapidjson/stringbuffer.h" | |
#include "rapidjson/writer.h" | |
/* | |
Evaluates the deep copy abilities of rapidjson when operating on an entire document. | |
Output text should match this: | |
Raw input: {"key":"value","array":[5,4,3,2],"nested":{"child":true}} | |
Original: {"key":"value","array":[5,4,3,2],"nested":{"child":true}} | |
Original (again): {"key":"value","array":[5,4,3,2],"nested":{"child":true}} | |
Copy: {"key":"!!!ue","array":[5,4,42,2],"nested":{"child":false,"sibling":99.9}} | |
*/ | |
int main(int argc, char * argv[]) { | |
auto input = "{\"key\":\"value\",\"array\":[5,4,3,2],\"nested\":{\"child\":true}}"; | |
// Print the raw input | |
std::cout << "Raw input: " << input << std::endl; | |
rapidjson::Document inDoc; | |
inDoc.Parse(input); | |
// Print the document | |
{ | |
rapidjson::StringBuffer buffer; | |
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | |
inDoc.Accept(writer); | |
std::cout << "Original: " << buffer.GetString() << std::endl; | |
} | |
// Make the copy | |
rapidjson::Document outDoc; | |
outDoc.CopyFrom(inDoc, outDoc.GetAllocator()); | |
// Modify the copy | |
SetValueByPointer(outDoc, "/array/2", 42); | |
SetValueByPointer(outDoc, "/nested/child", false); | |
SetValueByPointer(outDoc, "/nested/sibling", 99.9); | |
// Manually modify the string in memory to confirm that the memory is not shared between documents | |
rapidjson::Pointer ptr("/key"); | |
rapidjson::Value* val = ptr.Get(outDoc); | |
memcpy(val, "!!!", 3); | |
// Print the original again | |
{ | |
rapidjson::StringBuffer buffer; | |
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | |
inDoc.Accept(writer); | |
std::cout << "Original (again): " << buffer.GetString() << std::endl; | |
} | |
// Print the copy | |
{ | |
rapidjson::StringBuffer buffer; | |
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | |
outDoc.Accept(writer); | |
std::cout << "Copy: " << buffer.GetString() << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment