From 3f556b0371c2a95f2acab2c2a007dad4799e1b8b Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sat, 14 Feb 2015 13:28:50 -0600 Subject: [PATCH] Show use of builders. --- Home.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Home.md b/Home.md index 0c2d7ac..10c1183 100644 --- a/Home.md +++ b/Home.md @@ -39,14 +39,15 @@ std::ifstream config_doc("config_doc.json", std::ifstream::binary); Once that is done you can then use jsoncpp to parse the data: ```cpp -Json::Value root; // will contains the root value after parsing -Json::Reader reader; -bool parsingSuccessful = reader.parse( config_doc, root ); -if ( !parsingSuccessful ) +Json::Value root; // will contain the root value after parsing +Json::CharReaderBuilder rbuilder; +std::string errs; +bool parsingSuccessful = Json::parseFromStream(rbuilder, config_doc, &root, &errs); +if (!parsingSuccessful) { // report to the user the failure and their locations in the document. std::cout << "Failed to parse configuration\n" - << reader.getFormattedErrorMessages(); + << errs; return; } @@ -70,16 +71,13 @@ root["encoding"] = getCurrentEncoding(); root["indent"]["length"] = getCurrentIndentLength(); root["indent"]["use_space"] = getCurrentIndentUseSpace(); -Json::StyledWriter writer; // Make a new JSON document for the configuration. Preserve original comments. -std::string outputConfig = writer.write( root ); +Json::StreamWriterBuilder wbuilder; +std::string outputConfig = Json::writeString(wbuilder, root); -// You can also use streams. This will put the contents of any JSON -// stream at a particular sub-value, if you'd like. +// You can also use streams. This will write the contents of a particular JSON +// sub-value, using the default Json::StreamWriterBuilder. std::cin >> root["subtree"]; - -// And you can write to a stream, using the StyledWriter automatically. -std::cout << root; ``` Build instructions ------------------