-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
72 lines (53 loc) · 1.83 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "gjson/JsonObject.h"
int main(){
using namespace gjson;
JsonObject obj;
// parse
obj.Parse(R"( { "primes":[2,5,7,11,13] } )");
// empty aggregates
obj["empty_array"] = Array;
obj["empty_map"] = Map;
// assign an array
obj["nice_numbers"] = Array("two", 3, 4.4);
// assign a map
obj["next"] = Map(1,"one")
("two",2);
// we can use an integer as a key as well
obj[23] = "twentry three";
// iterator over the intergers
for( auto p : obj["primes"] ){
std::cout << p.AsInteger() << "\n";
}
// or use as an zero index array
auto const& primes = obj["primes"];
for(size_t i = 0; i!= primes.size(); ++i){
std::cout << primes[0] << "\n";
}
for( auto p : obj["nice_numbers"] ){
switch(p.GetType()){
case Type_Integer:
std::cout << p.AsInteger() << "\n";
break;
case Type_String:
std::cout << p.AsString() << "\n";
break;
case Type_Float:
std::cout << p.AsFloat() << "\n";
break;
default:
break;
}
}
// iteratate over a map
using MI = JsonObject::const_iterator;
for(MI iter( obj["next"].begin() ), end( obj["next"].end());iter!=end;++iter){
std::cout << iter.key() << " => " << iter.value() << "\n";
}
// display pretty
obj.Display();
// print to a single line
std::string s = obj.ToString();
JsonObject other;
// reparse
other.Parse(s);
}