-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathjson.cpp
241 lines (209 loc) · 5.97 KB
/
json.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
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
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "json.h"
#include <fstream>
#include <tbox/base/json.hpp>
#include <tbox/base/assert.h>
namespace tbox {
namespace util {
namespace json {
bool Get(const Json &js, bool &field_value)
{
if (!js.is_boolean())
return false;
field_value = js.get<bool>();
return true;
}
bool Get(const Json &js, unsigned int &field_value) {
if (!js.is_number_unsigned())
return false;
field_value = js.get<unsigned int>();
return true;
}
bool Get(const Json &js, double &field_value)
{
if (!js.is_number())
return false;
field_value = js.get<double>();
return true;
}
bool Get(const Json &js, std::string &field_value)
{
if (!js.is_string())
return false;
field_value = js.get<std::string>();
return true;
}
bool Get(const Json &js,int &field_value)
{
if (!js.is_number_integer())
return false;
field_value = js.get<int>();
return true;
}
bool GetField(const Json &js, const std::string &field_name, bool &field_value)
{
if (!js.contains(field_name))
return false;
return Get(js.at(field_name), field_value);
}
bool GetField(const Json &js, const std::string &field_name, unsigned int &field_value)
{
if (!js.contains(field_name))
return false;
return Get(js.at(field_name), field_value);
}
bool GetField(const Json &js, const std::string &field_name, int &field_value)
{
if (!js.contains(field_name))
return false;
return Get(js.at(field_name), field_value);
}
bool GetField(const Json &js, const std::string &field_name, double &field_value)
{
if (!js.contains(field_name))
return false;
return Get(js.at(field_name), field_value);
}
bool GetField(const Json &js, const std::string &field_name, std::string &field_value)
{
if (!js.contains(field_name))
return false;
return Get(js.at(field_name), field_value);
}
bool HasObjectField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_object();
}
bool HasArrayField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_array();
}
bool HasBooleanField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_boolean();
}
bool HasNumberField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_number();
}
bool HasFloatField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_number_float();
}
bool HasIntegerField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_number_integer();
}
bool HasUnsignedField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_number_unsigned();
}
bool HasStringField(const Json &js, const std::string &field_name)
{
if (!js.contains(field_name))
return false;
auto &js_field = js.at(field_name);
return js_field.is_string();
}
Json Load(const std::string &filename)
{
Json js;
std::ifstream input_json_file(filename);
if (input_json_file.is_open()) {
try {
input_json_file >> js;
} catch (const std::exception &e) {
throw ParseJsonFileError(filename, e.what());
}
} else {
throw OpenFileError(filename);
}
return js;
}
/**
* 通过数[],{},"的方式找JSON字串的结束位置
*/
int FindEndPos(const char *str_ptr, size_t str_len)
{
TBOX_ASSERT(str_ptr != nullptr);
bool is_started = false;//! 是否已开始,防止开头是空格或TAB
int braces_level = 0; //! 花括号的层级
int square_level = 0; //! 方括号的层级
bool in_string = false; //! 是否在字串中
for (size_t i = 0; i < str_len; ++i) {
char ch = str_ptr[i];
if (!is_started && ::isgraph(ch))
is_started = true;
if (ch == '"') {
if (in_string) {
//! 要防止 \" 与 \\" \\\\\" 这些转义符的情况
//! 如果 " 前面是连续的 \ 那就对应数 \ 的个数。如果是单数,则"失效
in_string = false;
for (size_t j = (i - 1); j != 0 && str_ptr[j] == '\\'; --j)
in_string = !in_string;
} else {
in_string = true;
}
} else {
if (in_string) //! 如果在字串里,就直接略过
continue;
switch (ch) {
case '[': ++square_level; break;
case ']': --square_level; break;
case '{': ++braces_level; break;
case '}': --braces_level; break;
}
}
if (braces_level == 0 &&
square_level == 0 &&
!in_string &&
is_started) {
return i + 1;
}
if (braces_level < 0 || square_level < 0)
return -1;
}
return 0;
}
}
}
}