move ifstream example

Christopher Dunn 2015-02-22 00:43:42 -06:00
parent ccb86d20f9
commit 740effe392

45
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 <fstream>
#include <json/value.h>
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 <fstream>
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.