This repository was archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathjson_example.cpp
More file actions
78 lines (65 loc) · 1.87 KB
/
json_example.cpp
File metadata and controls
78 lines (65 loc) · 1.87 KB
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
72
73
74
75
76
77
78
#include "../json.hpp"
#include <iostream>
using json::JSON;
using namespace std;
int main()
{
// Example of creating each type
// You can also do JSON::Make( JSON::Class )
JSON null;
JSON Bool( true );
JSON Str( "RawString" );
JSON Str2( string( "C++String" ) );
JSON Int( 1 );
JSON Float( 1.2 );
JSON Arr = json::Array();
JSON Obj = json::Object();
// Types can be overwritten by assigning
// to the object again.
Bool = false;
Bool = "rtew";
Bool = 1;
Bool = 1.1;
Bool = string( "asd" );
// Append to Arrays, appending to a non-array
// will turn the object into an array with the
// first element being the value that's being
// appended.
Arr.append( 1 );
Arr.append( "test" );
Arr.append( false );
// Access Array elements with operator[]( unsigned ).
// Note that this does not do bounds checking, and
// returns a reference to a JSON object.
JSON& val = Arr[0];
// Arrays can be intialized with any elements and
// they are turned into JSON objects. Variadic
// Templates are pretty cool.
JSON Arr2 = json::Array( 2, "Test", true );
// Objects are accessed using operator[]( string ).
// Will create new pairs on the fly, just as std::map
// would.
Obj["Key1"] = 1.0;
Obj["Key2"] = "Value";
JSON Obj2 = json::Object();
Obj2["Key3"] = 1;
Obj2["Key4"] = Arr;
Obj2["Key5"] = Arr2;
// Nested Object
Obj["Key6"] = Obj2;
// Dump Obj to a string.
cout << Obj << endl;
// We can also use a more JSON-like syntax to create
// JSON Objects.
JSON Obj3 = {
"Key1", "Value",
"Key2", true,
"Key3", {
"Key4", json::Array( "This", "Is", "An", "Array" ),
"Key5", {
"BooleanValue", true
}
}
};
cout << Obj3 << endl;
}