-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPaser.h
248 lines (209 loc) · 4.28 KB
/
JsonPaser.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
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
#ifndef MY_JSON_PASER_JSONPASER_H
#define MY_JSON_PASER_JSONPASER_H
#include<string>
#include<vector>
#include<map>
#include<memory>
enum {
PARSE_OK = 0,
PARSE_EXPECT_VALUE,
PARSE_INVALID_VALUE,
PARSE_ROOT_NOT_SINGULAR,
PARSE_NUMBER_TOO_BIG,
PARSE_INVALID_STRING_ESCAPE,
PARSE_MISS_QUOTATION_MARK,
PARSE_INVALID_STRING_CHAR,
PARSE_INVALID_UNICODE_HEX,
PARSE_INVALID_UNICODE_SURROGATE,
PARSE_MISS_COMMA_OR_SQUARE_BRACKET,
PARSE_MISS_KEY,
PARSE_MISS_COLON,
PARSE_MISS_COMMA_OR_CURLY_BRACKET,
STRINGIFY_OK
};// some return status of the functions
class document;
class Value
{
friend class document;
private:
typedef enum {
NUll,
FALSE,
TRUE,
NUMBER,
STRING,
ARRAY,
OBJECT } json_type;
double n = 0.0;
bool b = false;
std::string s;
std::vector<std::shared_ptr<Value>> array;
std::map<std::string,std::shared_ptr<Value>> object;
json_type type=NUll;
public:
bool IsNull() const
{
return (NUll == type );
}
bool IsBool() const
{
return (type == FALSE || type == TRUE);
}
bool IsNumber() const
{
return (type == NUMBER);
}
bool IsString() const
{
return (type == STRING);
}
bool IsArray() const
{
return (type == ARRAY);
}
bool IsObject() const
{
return (type == OBJECT);
}
const std::string& GetString() const
{
return s;
}
std::string& GetString()
{
return s;
}
const double& GetNumber() const
{
return n;
}
double& GetNumber()
{
return n;
}
const bool& GetBool() const
{
return b;
}
bool& GetBool()
{
return b;
}
const Value& operator[] (const std::string &a) const
{
return *((*object.find(a)).second);
}
Value& operator[] (const std::string &a)
{
return *object[a];
}
const Value& operator[] (size_t t) const
{
return *array[t];
}
Value& operator[] (size_t t)
{
return *array[t];
}
size_t GetArraysize() const
{
return array.size();
}
size_t GetObjectsize() const
{
return object.size();
}
};
class document
{
public:
int parse(const std::string& json);
bool IsNull() const
{
return (jsonvalue.type == Value::NUll );
}
bool IsBool() const
{
return (jsonvalue.type == Value::FALSE || jsonvalue.type == Value::TRUE);
}
bool IsNumber() const
{
return (jsonvalue.type == Value::NUMBER);
}
bool IsString() const
{
return (jsonvalue.type == Value::STRING);
}
bool IsArray() const
{
return (jsonvalue.type == Value::ARRAY);
}
bool IsObject() const
{
return (jsonvalue.type == Value::OBJECT);
}
const std::string& GetString() const
{
return jsonvalue.s;
}
std::string& GetString()
{
return jsonvalue.s;
}
const double& GetNumber() const
{
return jsonvalue.n;
}
double& GetNumber()
{
return jsonvalue.n;
}
const bool& GetBool() const
{
return jsonvalue.b;
}
bool& GetBool()
{
return jsonvalue.b;
}
size_t GetArraysize() const
{
return jsonvalue.array.size();
}
size_t GetObjectsize() const
{
return jsonvalue.object.size();
}
const Value& operator[] (const std::string &a) const
{
return *((*jsonvalue.object.find(a)).second);
}
Value& operator[] (const std::string &a)
{
return *jsonvalue.object[a];
}
const Value& operator[] (size_t t) const
{
return *jsonvalue.array[t];
}
Value& operator[] (size_t t)
{
return *jsonvalue.array[t];
}
private:
std::string::iterator current; //the current position of the iterator when in parse
Value jsonvalue;
std::string json_doc;
int parse_value();
int parse_whitespace();
int parse_null();
int parse_false();
int parse_true();
int parse_numble();
int parse_string();
int parse_array();
int parse_object();
bool parse_hex4(unsigned& u);
void encode_utf8(const unsigned& u);
};
#endif