Show use of builders.

Christopher Dunn 2015-02-14 13:28:50 -06:00
parent a8cc5e9bbd
commit 3f556b0371

22
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: Once that is done you can then use jsoncpp to parse the data:
```cpp ```cpp
Json::Value root; // will contains the root value after parsing Json::Value root; // will contain the root value after parsing
Json::Reader reader; Json::CharReaderBuilder rbuilder;
bool parsingSuccessful = reader.parse( config_doc, root ); std::string errs;
if ( !parsingSuccessful ) bool parsingSuccessful = Json::parseFromStream(rbuilder, config_doc, &root, &errs);
if (!parsingSuccessful)
{ {
// report to the user the failure and their locations in the document. // report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration\n" std::cout << "Failed to parse configuration\n"
<< reader.getFormattedErrorMessages(); << errs;
return; return;
} }
@ -70,16 +71,13 @@ root["encoding"] = getCurrentEncoding();
root["indent"]["length"] = getCurrentIndentLength(); root["indent"]["length"] = getCurrentIndentLength();
root["indent"]["use_space"] = getCurrentIndentUseSpace(); root["indent"]["use_space"] = getCurrentIndentUseSpace();
Json::StyledWriter writer;
// Make a new JSON document for the configuration. Preserve original comments. // 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 // You can also use streams. This will write the contents of a particular JSON
// stream at a particular sub-value, if you'd like. // sub-value, using the default Json::StreamWriterBuilder.
std::cin >> root["subtree"]; std::cin >> root["subtree"];
// And you can write to a stream, using the StyledWriter automatically.
std::cout << root;
``` ```
Build instructions Build instructions
------------------ ------------------