-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2map.h
105 lines (99 loc) · 4.06 KB
/
json2map.h
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// See the LICENSE file for license information.
//
// Created by Antti Stenvall <[email protected]>.
//
#ifndef _JSON2MAP_H_
#define _JSON2MAP_H_
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include "json_spirit_reader_template.h"
#include "json_spirit_writer_template.h"
namespace json {
class json2map {
private:
// recursive map
std::map<std::string, json::json2map*> json_;
bool hasOnlyValue_; // if recursive map isn't there, then hasOnlyValue_ = true
json_spirit::Value value_; // and this is the value (value is stored anyway, this is not memory optimized, but redundant data is saved)
void init(json_spirit::Value value);
void prettyPrint(unsigned int indent);
public:
json2map(std::string fileName);
json2map(json_spirit::Value value);
// see templated methods below
json_spirit::Value getValue();
json_spirit::Value getValue(std::string val1);
json_spirit::Value getValue(std::string val2, std::string val1);
json_spirit::Value getValue(std::vector<std::string> stack);
json_spirit::Value_type getValueType();
json_spirit::Value_type getValueType(std::string val1);
json_spirit::Value_type getValueType(std::string val2, std::string val1);
json_spirit::Value_type getValueType(std::vector<std::string> stack);
std::vector<json::json2map*> getVector();
std::vector<json::json2map*> getVector(std::string val1);
std::vector<json::json2map*> getVector(std::string val2, std::string val1);
std::vector<json::json2map*> getVector(std::vector<std::string> stack);
bool keyExists(std::string val1);
bool keyExists(std::string val1,std::string val2);
bool keyExists(std::vector<std::string> stack);
void prettyPrint();
virtual ~json2map();
// methods with templates (which need to be in header file)
// shortcuts for value getters
template<typename Type>
Type getValue(std::string val1){
std::vector<std::string> stack;
stack.push_back(val1);
return getValue<Type>(stack);
}
template<typename Type>
Type getValue(std::string val2,std::string val1){
std::vector<std::string> stack;
stack.push_back(val2);
stack.push_back(val1);
return getValue<Type>(stack);
}
template<typename Type>
std::vector<Type> getVector(std::string val1){
std::vector<std::string> stack;
stack.push_back(val1);
return getVector<Type>(stack);
}
template<typename Type>
std::vector<Type> getVector(std::string val2,std::string val1){
std::vector<std::string> stack;
stack.push_back(val2);
stack.push_back(val1);
return getVector<Type>(stack);
}
// these are the actual value getter
template<typename Type>
Type getValue(std::vector<std::string> stack) {
if (stack.size() == 0) {
return value_.get_value<Type>();
} else {
std::string value = stack.at(stack.size() - 1);
stack.pop_back();
return json_[value]->getValue<Type>(stack);
}
}
template<typename Type>
std::vector<Type> getVector(std::vector<std::string> stack) {
if (stack.size() == 0) {
// if this is not array, then it is the fault of user
json_spirit::Array arr = value_.get_array();
std::vector<Type> val;
for (unsigned int j = 0; j < arr.size(); ++j) {
val.push_back(arr[j].get_value<Type>());
}
return val;
} else {
std::string value = stack.at(stack.size() - 1);
stack.pop_back();
return json_[value]->getVector<Type>(stack);
}
}
};
}
#endif