-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJObject.h
More file actions
236 lines (198 loc) · 5.33 KB
/
Copy pathJObject.h
File metadata and controls
236 lines (198 loc) · 5.33 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef MYUTIL_JOBJECT_H
#define MYUTIL_JOBJECT_H
#include <iostream>
// #include <utility>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <unordered_map>
#include <map>
#include <variant>
#include <sstream>
namespace json
{
// c++17中的变体类型,variant<X,Y,Z>是可存放X,Y,Z三种类型数据的变体类型
// 和C的union相同的是:他也是union类型
// 和C的union不同的是:他是可以辨识类型安全的union
using std::variant;
using std::map;
using std::string;
using std::string_view;
using std::stringstream;
using std::vector;
using std::get_if; // 判断变体类型下是否有某数据类型,返回类型值的地址
enum TYPE
{
T_NULL,
T_BOOL,
T_INT,
T_DOUBLE,
T_STR,
T_LIST,
T_DICT
};
class JObject;
using null_t = string;
using int_t = int32_t;
using bool_t = bool;
using double_t = double;
using str_t = string;
using list_t = vector<JObject>;
using dict_t = map<string, JObject>;
// c++11中 std::is_same可以判断输入的类型是否是指定的模版类型
// std::is_same<T, int>::value
#define IS_TYPE(typea, typeb) std::is_same<typea, typeb>::value
// 判断是否为基本类型
template<typename T>
constexpr bool is_basic_type()
{
if constexpr(IS_TYPE(T, str_t) ||
IS_TYPE(T, bool_t) ||
IS_TYPE(T, double_t) ||
IS_TYPE(T, int_t))
return true;
return false;
}
class JObject
{
public:
using value_t = variant<bool_t, int_t, double_t, str_t, list_t, dict_t>;
JObject()// 默认为null类型
{
m_type = T_NULL;
m_value = "null";
}
JObject(int_t value)
{
Int(value);
}
JObject(bool_t value)
{
Bool(value);
}
JObject(double_t value)
{
Double(value);
}
JObject(str_t const &value)
{
Str(value);
}
JObject(list_t value)
{
List(std::move(value));
}
JObject(dict_t value)
{
Dict(std::move(value));
}
void Null()
{
m_type = T_NULL;
m_value = "null";
}
void Int(int_t value)
{
m_type = T_INT;
m_value = value;
}
void Bool(bool_t value)
{
m_type = T_BOOL;
m_value = value;
}
void Double(double_t value)
{
m_type = T_DOUBLE;
m_value = value;
}
void Str(str_t value)
{
m_type = T_STR;
m_value = value;
}
void List(list_t value)
{
m_type = T_LIST;
m_value = std::move(value); // 将左值转换为右值直接赋给m_value
}
void Dict(dict_t value)
{
m_type = T_DICT;
m_value = std::move(value);
}
#define THROW_GET_ERROR(erron) throw std::logic_error("type error in get"#erron" value!")
template<typename V>
V &Value()
{
// 添加安全检查
if constexpr(IS_TYPE(V, str_t))
{
if(m_type != T_STR)
THROW_GET_ERROR(string);
}else if constexpr(IS_TYPE(V, bool_t))
{
if(m_type != T_BOOL)
THROW_GET_ERROR(BOOL);
}else if constexpr(IS_TYPE(V, double_t))
{
if(m_type != T_DOUBLE)
THROW_GET_ERROR(DOUBLE);
}else if constexpr(IS_TYPE(V, list_t))
{
if(m_type != T_LIST)
THROW_GET_ERROR(LIST);
}else if constexpr(IS_TYPE(V, dict_t))
{
if(m_type != T_DICT)
THROW_GET_ERROR(DICT);
}
void *v = value(); // 指向对象值的指针,用void* 的原因是因为该函数返回的是variant类型的值
if(v == nullptr)
throw std::logic_error("unknown type in JObject::Value()");
return *((V *) v); // void*类型转换为V*类型指针,然后通过*获取该指针指向的值
}
TYPE Type()
{
return m_type;
}
string to_string();
void push_back(JObject item)
{
if(m_type == T_LIST)
{
auto &list = Value<list_t>();
list.push_back(std::move(item));
return;
}
throw std::logic_error("not a list type! JObject::push_back()");
}
void pop_back()
{
if(m_type == T_LIST)
{
auto &list = Value<list_t>();
list.pop_back();
return;
}
throw std::logic_error("not a list type! JObject::pop_back()");
}
JObject &operator[](string const key)
{
if(m_type == T_DICT)
{
auto &dict =Value<dict_t>();
return dict[key];
}
throw std::logic_error("not a dict type! JObject::operator[]()");
}
private:
// 根据类型获取值的地址,直接硬转为void*类型,然后外界调用Value函数进行类型的强转
void *value();
private:
TYPE m_type;
value_t m_value;
};
}
#endif