-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (66 loc) · 2.42 KB
/
main.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
// See the LICENSE file for license information.
//
// Created by Antti Stenvall <[email protected]>.
//
// This is test file for testing json2map class. This works out as a tutorial too!
//
#include <cstdlib>
#include "json2map.h"
int main(int argc, char **argv) {
if (argc < 2){
std::cout << "Call this as ./main json-inputfile" << std::endl;
return EXIT_SUCCESS;
}
// read JSON file name from command line parameter (test.json included in the repo)
std::string inputFile = std::string(argv[1]);
// init json2map structure
json::json2map *json = new json::json2map(inputFile);
// print all that is inside the json
json->prettyPrint();
std::cout << std::endl << std::endl;
// exaple of testing keys
if (!json->keyExists("notDefined")){
std::cout << "Given key does not exists" << std::endl;
}
if (json->keyExists("pi","data")){
// init double from json to a variable
double d = json->getValue<double>("pi","data");
std::cout << "Double: " << d << std::endl;
}
if (json->keyExists("firstNumbers","data")){
// retrieve vector of values
std::vector<int> temp = json->getVector<int>("firstNumbers","data");
std::cout << "Array" << std::endl;
for (unsigned int i=0;i<temp.size();i++){
std::cout << "Value " << i << ": " << temp.at(i) << std::endl;
}
}
// test value types
std::vector<std::string> empty;
std::cout << "Value type of root is: " << json->getValueType(empty) << std::endl;
if (json->keyExists("temp")){
std::cout << "Value type of temp is: " << json->getValueType("temp") << std::endl;
}
if (json->keyExists("name")){
std::cout << "Value type of name is: " << json->getValueType("name") << std::endl;
}
// init vector of json2maps from an existing value in json2map, note the valueType comparison
if (json->keyExists("characters") &&
json->getValueType("characters") == json_spirit::array_type){
std::vector<json::json2map*> jsonV = json->getVector("characters");
for (unsigned int i=0; i < jsonV.size(); i++){
// be loose with existence
std::cout << "Value " << i << ":" <<
" name: " << jsonV.at(i)->getValue<std::string>("name") <<
" born: " << jsonV.at(i)->getValue<int>("born") <<
std::endl;
}
// delete array
while (jsonV.size() > 0){
delete jsonV.at(jsonV.size()-1);
jsonV.pop_back();
}
}
delete json;
return EXIT_SUCCESS;
}