From 740effe392d65aa7a5d497cccbf83de68d84d613 Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sun, 22 Feb 2015 00:43:42 -0600 Subject: [PATCH] move ifstream example --- Home.md | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/Home.md b/Home.md index b8d2bf3..1cb0697 100644 --- a/Home.md +++ b/Home.md @@ -1,8 +1,21 @@ Introduction ------------ JSON (JavaScript Object Notation) is a lightweight data-interchange format. It can represent integer, real number, string, an ordered sequence of value, and a collection of name/value pairs. + Here is an example of JSON data: -```json +```js +{ + "encoding" : "UTF-8", + "plug-ins" : [ + "python", + "c++", + "ruby" + ], + "indent" : { "length" : 3, "use_space": true } +} +``` +And here it is with ***comments***: +```js // Configuration options { // Default encoding for text @@ -19,27 +32,19 @@ Here is an example of JSON data: "indent" : { "length" : 3, "use_space": true } } ``` -Features +Features of *jsoncpp* -------- -- read and write JSON document -- attach C and C++ style comments to element during parsing -- rewrite JSON document preserving original comments - Notes: Comments used to be supported in JSON but where removed for portability (C like comments are not supported in Python). Since comments are useful in configuration/input file, this feature was preserved. +- Read and write JSON document. +- Attach C and C++ style **comments** to element during parsing. +- Rewrite JSON document preserving original comments. Code example ------------ -You must open the file in an `std::ifstream` before you can use it: - ```cpp -#include +#include -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; // starts as "null"; will contain the root value after parsing -config_doc >> root; +std::cin >> root; // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no // such member. @@ -65,6 +70,13 @@ root["indent"]["use_space"] = getCurrentIndentUseSpace(); std::cout << root << "\n"; ``` +You can also read from a file, e.g.: +```cpp +#include + +std::ifstream config_doc("config_doc.json", std::ifstream::binary); +``` + If you need some unusual features, use *Builders*: ```cpp Json::Value root; @@ -87,6 +99,9 @@ Json::StreamWriterBuilder wbuilder; std::string outputConfig = Json::writeString(wbuilder, root); ``` +### A note on "comments" +Comments used to be supported in JSON but where removed for portability (C-like comments are not supported in Python). Since comments are useful in configuration/input file, this feature is preserved in `jsoncpp`. + 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.