-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
78 lines (68 loc) · 2.25 KB
/
example.cpp
File metadata and controls
78 lines (68 loc) · 2.25 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 <iostream>
#include <typeinfo>
#include "json_obj.hpp"
using namespace std;
int main()
{
int i;
// have to give key to represent json itself
json *a = new json();
json s;
/*principle
1. x["key"] will always get object
2. x("key") will always get array
3. any key duplicating with different json type will lead to
overwrite previous element.
4. try to get non-existing object or array will automatically create new one
*/
//add number
s["number"] = 1.2;
//add string
s["string"] = "Yeah~!";
//add boolean
s["boolean"] = true;
//add null
s["null"] = nullptr;
//add object
s["object"]["something"] = "string_in_something";
//add array (with some operator)
s("array")(2.2, "string_in_array");
(*a)["number_in_a"] = 9487;
//add another json
s["key_a"] = *a;
//add another json in array
s("array_a")(*a);
// a_json modification will be seen in s_json, and vice versa
(*a)["string_in_a"] = "this string is in a";
//push object in array
//push_object_back(json) is also provided
s("array").push_object_back();
//insert json in array
//insert_object(index) is also provided
s("array").insert_object(3, *a);
//psuh array in array
s("array").push_array_back()(3.3, "another_string");
//insert array in array
s("array").insert_array(0)(4.4, nullptr);
delete a;
// deleting a_json will not influence s_jon
//push number in array
//push_string_back, push_boolean_back also provided
s("array").push_number_back(5566);
//insert number in array
//insert_string, insert_boolean also provided
s("array").insert_number(1, 426);
// get array or object from array (it will insert a new element if not exist)
s("array").get_array(0).push_string_back("string in array of array");
s("array").get_object(4)["object_in_array"] = true;
//delete any element from object by key
s -= "array_a";
s -= "a_json";
//delete element from array by index (single or range) or just pop back
s("array").pop_any_back();
s("array").erase_any_single(4);
s("array").erase_any_range(1,4); // not include 4
//dump json to valid json format
s.print_json();
return 1;
}