Builders

Christopher Dunn 2015-02-14 15:55:19 -06:00
parent 3f556b0371
commit ae7e906e1d

46
Home.md

@ -37,19 +37,9 @@ 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 contain the root value after parsing Json::Value root; // starts as "null"; will contain the root value after parsing
Json::CharReaderBuilder rbuilder; config_doc >> root;
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"
<< errs;
return;
}
// Get the value of the member of root named 'encoding', return 'UTF-8' if there is no // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
// such member. // such member.
@ -71,14 +61,32 @@ root["encoding"] = getCurrentEncoding();
root["indent"]["length"] = getCurrentIndentLength(); root["indent"]["length"] = getCurrentIndentLength();
root["indent"]["use_space"] = getCurrentIndentUseSpace(); root["indent"]["use_space"] = getCurrentIndentUseSpace();
// Make a new JSON document for the configuration. Preserve original comments. // Make a new JSON document with the new configuration. Preserve original comments.
Json::StreamWriterBuilder wbuilder; std::cout << root << "\n";
std::string outputConfig = Json::writeString(wbuilder, root);
// 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"];
``` ```
If you need some unusual features, use *Builders*:
```cpp
Json::Value root;
Json::CharReaderBuilder rbuilder;
// Configure the Builder, then ...
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"
<< errs;
return;
}
// ...
Json::StreamWriterBuilder wbuilder;
// Configure the Builder, then ...
std::string outputConfig = Json::writeString(wbuilder, root);
```
Build instructions Build instructions
------------------ ------------------
The build instructions are located in the file [`README.md`](https://github.com/open-source-parsers/jsoncpp/blob/master/README.md) in the top-directory of the project. The build instructions are located in the file [`README.md`](https://github.com/open-source-parsers/jsoncpp/blob/master/README.md) in the top-directory of the project.