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:
```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
------------------