Skip to content

Commit 33cc7b8

Browse files
committed
Update README.md
1 parent 8ca8bdb commit 33cc7b8

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

README.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,74 @@ Of course special consideration is needed when displaying these strings if they
1313
So, for example,
1414

1515
```json
16-
{"test1" : "\uD840\uDC8A"}
16+
{"test1" : "\uD840\uDC8A"}
1717
```
1818

1919
will correctly parse and the object's "test1" member will have the byte sequence: `0xF0 0xA0 0x82 0x8A`
2020

2121
Here is a simple example of the usage of this library:
2222

2323
```c++
24-
#include "cpp-json/json.h"
25-
#include <fstream>
26-
#include <iostream>
27-
28-
int main() {
29-
// open a file
30-
std::ifstream file("test.json");
31-
32-
// json::parse can take two iterators or a std::istream
33-
json::value json = json::parse(file);
34-
35-
// you can access objects like associative array's easily
36-
// the result is a json::value
37-
// ... though in real code you may want to check the type first ;-)
38-
auto servlets = json["web-app"]["servlet"];
39-
40-
// when dealing with arrays, you can just use iterators,
41-
// or feel free to use C++11 ranged-for
42-
const json::array &a = as_array(servlets);
43-
for(auto it = a.begin(); it != a.end(); ++it) {
44-
const json::value &v = *it;
45-
// all basic types (numbers, strings, booleans) can be converted
46-
// to a string
47-
std::cout << to_string(v["servlet-name"]) << std::endl;
48-
}
24+
#include "cpp-json/json.h"
25+
#include <fstream>
26+
#include <iostream>
27+
28+
int main() {
29+
// open a file
30+
std::ifstream file("test.json");
31+
32+
// json::parse can take two iterators or a std::istream
33+
json::value json = json::parse(file);
34+
35+
// you can access objects like associative array's easily
36+
// the result is a json::value
37+
// ... though in real code you may want to check the type first ;-)
38+
auto servlets = json["web-app"]["servlet"];
39+
40+
// when dealing with arrays, you can just use iterators,
41+
// or feel free to use C++11 ranged-for
42+
const json::array &a = as_array(servlets);
43+
for(auto it = a.begin(); it != a.end(); ++it) {
44+
const json::value &v = *it;
45+
// all basic types (numbers, strings, booleans) can be converted
46+
// to a string
47+
std::cout << to_string(v["servlet-name"]) << std::endl;
4948
}
49+
}
5050
```
5151

5252
You can also programmatically create `json::value` objects like this:
5353

5454
```c++
55-
int main(int argc, char *argv[]) {
56-
json::array arr = {
57-
1,
58-
2,
59-
3,
60-
4,
61-
"Testing 1 2 3", json::object{
62-
{ "hello", 1234 },
63-
{ "world", 5678 }
64-
}
65-
};
66-
67-
std::cout << stringify(arr) << std::endl;
68-
}
69-
```
70-
71-
Which of course results in a object representing the following JSON:
72-
73-
```json
74-
[
55+
int main(int argc, char *argv[]) {
56+
json::array arr = {
7557
1,
7658
2,
7759
3,
7860
4,
79-
"Testing 1 2 3", {
80-
"hello" : 1234,
81-
"world" : 5678
61+
"Testing 1 2 3", json::object{
62+
{ "hello", 1234 },
63+
{ "world", 5678 }
8264
}
83-
]
65+
};
66+
67+
std::cout << stringify(arr) << std::endl;
68+
}
69+
```
70+
71+
Which of course results in a object representing the following JSON:
72+
73+
```json
74+
[
75+
1,
76+
2,
77+
3,
78+
4,
79+
"Testing 1 2 3", {
80+
"hello" : 1234,
81+
"world" : 5678
82+
}
83+
]
8484
```
8585

8686
Finally, this library is very fast, when processing a 190 MB JSON file I randomly selected, parsing took no more than 18 seconds on my machine. For a Qt4 JSON parsing library, you can also checkout my other project: [QJson4](https://github.com/eteran/qjson4)

0 commit comments

Comments
 (0)