-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.h
206 lines (188 loc) · 5.52 KB
/
json.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
#ifndef JSON_H
#define JSON_H
#include <map>
#include <vector>
#include <string.h>
#include <exception>
namespace json
{
bool parseString(size_t &pos, const std::string json, std::string &value);
class Value;
class Object;
class Array;
class Object {
private:
std::map<std::string, Value*> valueMap;
bool freeItem(const std::string key, bool removeItFromMap);
public:
Object() {
}
~Object(){
this->clear();
}
void import(const std::string key, const Value &value);
bool remove(const std::string key) {
return this->freeItem(key, true);
}
void set(std::string key, const int value);
void set(std::string key, const double value);
Value* get(const std::string key) const;
bool hasKey(const std::string key) const;
int count();
void clear();
std::string json();
void json(std::stringstream &ss);
int parse(const std::string json);
int parse(size_t &pos, const std::string json);
};
class Array {
private:
std::vector<Value*> values;
public:
Array(){
}
~Array(){
this->clear();
}
void json(std::stringstream &ss);
int parse(size_t &pos, const std::string json);
int count() {
return this->values.size();
}
Value* get(int pos) {
return this->values.at(pos);
}
void clear();
};
class Value {
public:
enum Type {
nullType,
intType,
stringType,
boolType,
arrayType,
objectType,
doubleType
};
private:
Value::Type type;
union {
int valueInt;
double valueDouble;
std::string* valueString;
bool valueBool;
Array* valueArray;
Object* valueObject;
};
public:
void json(std::stringstream &ss);
Value() {
this->type = nullType;
}
Value(const int value) {
this->type = intType;
this->valueInt = value;
}
Value(const double value) {
this->type = doubleType;
this->valueDouble = value;
}
~Value() {}
void import( const Value &other ){
if (this != &other){
}
}
void reset(){
//muna að hreinsa
switch (this->type){
case stringType:
delete this->valueString;
break;
case arrayType:
delete this->valueArray;
break;
case objectType:
delete this->valueObject;
break;
case boolType:
case intType:
case doubleType:
case nullType:
break;
}
this->type = nullType;
}
void set(const int value) {
this->reset();
this->type = intType;
this->valueInt = value;
}
void set(const double value) {
this->reset();
this->type = doubleType;
this->valueDouble = value;
}
bool isNull(){
return this->type == nullType;
}
bool isInt(){
return this->type == intType;
}
int getInt(){
if (this->type == intType){
return this->valueInt;
}
throw 1;
}
bool isDouble(){
return this->type == intType || this->type == doubleType;
}
double getDouble(){
if (this->type == intType){
return this->valueInt;
} else if (this->type == doubleType) {
return this->valueDouble;
}
throw 1;
}
bool isBool(){
return this->type == boolType;
}
int getBool(){
if (this->type == boolType){
return this->valueBool;
}
throw 1;
}
bool isString(){
return this->type == stringType;
}
std::string getString(){
if (this->type == stringType){
return *this->valueString;
}
throw 1;
}
bool isArray(){
return this->type == arrayType;
}
Array* getArray(){
if (this->type == arrayType){
return this->valueArray;
}
throw 1;
}
bool isObject(){
return this->type == objectType;
}
Object* getObject(){
if (this->type == objectType){
return this->valueObject;
}
throw 1;
}
int parse(size_t &pos, const std::string json);
};
}
#endif