-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathJObject.h
More file actions
255 lines (216 loc) · 5.4 KB
/
JObject.h
File metadata and controls
255 lines (216 loc) · 5.4 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// Created by Alone on 2022-7-25.
//
#ifndef MYUTIL_JOBJECT_H
#define MYUTIL_JOBJECT_H
#include<stdexcept>
#include <utility>
#include<variant>
#include <map>
#include<vector>
#include<string>
#include<string_view>
#include<sstream>
namespace json
{
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>;
#define IS_TYPE(typea, typeb) std::is_same<typea,typeb>::value
template<class 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_value = value;
m_type = T_INT;
}
void Bool(bool_t value)
{
m_value = value;
m_type = T_BOOL;
}
void Double(double_t value)
{
m_type = T_DOUBLE;
m_value = value;
}
void Str(string_view value)
{
m_value = string(value);
m_type = T_STR;
}
void List(list_t value)
{
m_value = std::move(value);
m_type = T_LIST;
}
void Dict(dict_t value)
{
m_value = std::move(value);
m_type = T_DICT;
}
// operator string()
// {
// return Value<string>();
// }
//
// operator int()
// {
// return Value<int>();
// }
//
// operator bool()
// {
// return Value<bool>();
// }
//
// operator double()
// {
// return Value<double>();
// }
#define THROW_GET_ERROR(erron) throw std::logic_error("type error in get "#erron" value!")
template<class 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, int_t))
{
if (m_type != T_INT)
THROW_GET_ERROR(INT);
} 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();
if (v == nullptr)
throw std::logic_error("unknown type in JObject::Value()");
return *((V *) 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! JObjcct::push_back()");
}
void pop_back()
{
if (m_type == T_LIST)
{
auto &list = Value<list_t>();
list.pop_back();
return;
}
throw std::logic_error("not list type! JObjcct::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 dict type! JObject::opertor[]()");
}
private:
//根据类型获取值的地址,直接硬转为void*类型,然后外界调用Value函数进行类型的强转
void *value();
private:
TYPE m_type;
value_t m_value;
};
}
#endif //MYUTIL_JOBJECT_H