-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.h
More file actions
198 lines (158 loc) · 4.64 KB
/
json.h
File metadata and controls
198 lines (158 loc) · 4.64 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
#ifndef HIGHLOAD_JSON_H
#define HIGHLOAD_JSON_H
#include <iostream>
#include <string>
#include <vector>
#include <exception>
#include <stdexcept>
#include <map>
#include <cstring>
#include <cassert>
using namespace std;
namespace json
{
class Object;
class Value;
enum ValueType
{
StringVal,
IntVal
};
class Value
{
ValueType _type;
char* _val;
int _int_val;
void _copy_value(const char* src, int begin, int end)
{
int length = end - begin;
_val = (char*) malloc(length + 1);
memcpy(_val, src + begin, length);
_val[length] = 0;
}
public:
~Value()
{
//delete _val; // skip memory leak
}
Value(const Value &value)
{
_type = value._type;
if (value._val != NULL)
//_copy_value(value._val, 0, strlen(value._val));
_val = value._val;
else
_val = NULL;
_int_val = value._int_val;
}
Value(Value &&value)
{
_type = value._type;
_val = value._val;
_int_val = value._int_val;
value._val = NULL;
}
Value()
{
_val = NULL;
_int_val = 0;
_type = IntVal;
}
ValueType GetType() const
{
return _type;
}
explicit operator char*() const
{
assert(_type == StringVal);
return _val;
}
explicit operator int() const
{
assert(_type == IntVal);
return _int_val;
}
friend class Object;
};
class Object
{
#define _expect(str, pos, expected) if (str[pos] != (expected)) goto fail;
bool _parse_simple_object(const char *str, int n, int &end_pos)
{
int pos = 0;
char *key = NULL;
while (isspace(str[pos])) pos++;
_expect(str, pos++, '{');
while (isspace(str[pos])) pos++;
while (str[pos] != '}')
{
key = NULL;
Value value;
_expect(str, pos++, '"');
int key_start_pos = pos;
while (pos < n && str[pos] != '"')
pos++;
key = (char*) malloc(pos - key_start_pos + 1);
memcpy(key, str + key_start_pos, pos - key_start_pos);
key[pos - key_start_pos] = 0;
_expect(str, pos++, '"');
while (isspace(str[pos])) pos++;
_expect(str, pos++, ':');
while (isspace(str[pos])) pos++;
if (str[pos] == '-' || isdigit(str[pos]))
{
// IntVal
bool is_negative = false;
if (str[pos] == '-')
is_negative = true, pos++;
// TODO: check any digit exists
while (isdigit(str[pos]))
value._int_val = value._int_val * 10 + str[pos++] - '0';
if (is_negative)
value._int_val *= -1;
value._type = IntVal;
}
else
{
// StringVal
_expect(str, pos++, '"');
int value_start_pos = pos;
while (pos < n && str[pos] != '"')
pos++;
value._copy_value(str, value_start_pos, pos);
_expect(str, pos++, '"');
value._type = StringVal;
}
while (isspace(str[pos])) pos++;
if (str[pos] == ',')
pos++;
while (isspace(str[pos])) pos++;
properties.emplace_back(key, value);
}
end_pos = pos;
return true;
fail:
delete key;
return false;
}
public:
vector<pair<char*, Value> > properties;
bool parse_simple_object(const char* str, int length)
{
int end_pos;
return _parse_simple_object(str, length, end_pos);
}
bool parse_simple_object(const char* str, int length, int start_pos, int &end_pos)
{
auto ret = _parse_simple_object(str + start_pos, length - start_pos, end_pos);
end_pos += start_pos;
return ret;
}
~Object()
{
for (auto &pair : properties)
delete pair.first;
}
};
};
#endif //HIGHLOAD_JSON_H