OLD | NEW |
| (Empty) |
1 #include "my_json.h" | |
2 | |
3 namespace json { | |
4 Null::Null() {} | |
5 | |
6 Value* Null::Clone() const { | |
7 return new Null; | |
8 } | |
9 | |
10 void Null::Accept(Visitor* vis) { | |
11 vis->Visit(*this); | |
12 } | |
13 | |
14 bool signed_; | |
15 long size_; | |
16 | |
17 Number::Number() | |
18 : value_(0), signed_(false), size_(0) { | |
19 } | |
20 | |
21 Number::Number(long long n) | |
22 : value_(n), signed_(true), size_(sizeof(n)) { | |
23 } | |
24 | |
25 Number::Number(const Number& n) | |
26 : value_(n.value_), signed_(n.signed_), size_(n.size_) { | |
27 } | |
28 | |
29 Number::Number(const debug::Blob& blob) { | |
30 | |
31 } | |
32 | |
33 Value* Number::Clone() const { | |
34 return new Number(*this); | |
35 } | |
36 | |
37 void Number::Accept(Visitor* vis) { | |
38 vis->Visit(*this); | |
39 } | |
40 | |
41 void Number::SetInteger(const void* i_ptr, size_t int_size, bool sign) { | |
42 size_ = int_size; | |
43 signed_ = sign; | |
44 long long x = 0; | |
45 switch (int_size) { | |
46 case 1: x = *(static_cast<const char*>(i_ptr)); break; | |
47 case 2: x = *(static_cast<const short*>(i_ptr)); break; | |
48 case 4: x = *(static_cast<const int*>(i_ptr)); break; | |
49 case 8: x = *(static_cast<const long long*>(i_ptr)); break; | |
50 } | |
51 value_ = x; | |
52 // long long xx = value(); | |
53 // printf("Number::SetInteger %I64d %I64d\n", x, xx); | |
54 } | |
55 | |
56 std::string Number::AsHexString() const { | |
57 if (0 == size_) | |
58 return "null"; | |
59 | |
60 char tmp[100]; | |
61 switch (size_) { | |
62 case 1: | |
63 case 2: | |
64 case 4: _snprintf_s(tmp, sizeof(tmp), sizeof(tmp) - 1, "%X", static_cast<int
>(value_)); break; | |
65 case 8: _snprintf_s(tmp, sizeof(tmp), sizeof(tmp) - 1, "%I64X", value_); bre
ak; | |
66 } | |
67 tmp[sizeof(tmp) - 1] = 0; | |
68 return tmp; | |
69 } | |
70 | |
71 int Number::AsInt() const { | |
72 return static_cast<int>(value()); | |
73 } | |
74 | |
75 std::string Number::AsDecString() const { | |
76 if (0 == size_) | |
77 return "null"; | |
78 | |
79 char tmp[100]; | |
80 switch (size_) { | |
81 case 1: | |
82 case 2: | |
83 case 4: { | |
84 int x = static_cast<int>(value_); | |
85 _snprintf_s(tmp, sizeof(tmp), sizeof(tmp) - 1, "%d", x); | |
86 break; | |
87 } | |
88 case 8: _snprintf_s(tmp, sizeof(tmp), sizeof(tmp) - 1, "%I64d", value_); bre
ak; | |
89 } | |
90 tmp[sizeof(tmp) - 1] = 0; | |
91 return tmp; | |
92 } | |
93 | |
94 void Number::SetPrintHint(const std::string& hint) { | |
95 //TODO:implement ? | |
96 } | |
97 | |
98 String::String() {} | |
99 | |
100 String::String(const std::string& str) | |
101 : value_(str) { | |
102 } | |
103 | |
104 Value* String::Clone() const { | |
105 return new String(value_); | |
106 } | |
107 | |
108 void String::Accept(Visitor* vis) { | |
109 vis->Visit(*this); | |
110 } | |
111 | |
112 void String::SetStr(const std::string& str) { | |
113 value_ = str; | |
114 } | |
115 | |
116 const std::string& String::GetStr() const { | |
117 return value_; | |
118 } | |
119 | |
120 std::string& String::GetStr() { | |
121 return value_; | |
122 } | |
123 | |
124 Object::Object() { | |
125 } | |
126 | |
127 Object::~Object() { | |
128 Clear(); | |
129 } | |
130 | |
131 Value* Object::Clone() const { | |
132 Object* copy = new Object; | |
133 for (size_t i = 0; i < value_.size(); i++) { | |
134 const std::pair<std::string, Value*>& prop = value_[i]; | |
135 Value* value_copy = prop.second->Clone(); | |
136 if (NULL != value_copy) | |
137 copy->SetProperty(prop.first, value_copy); | |
138 } | |
139 return copy; | |
140 } | |
141 | |
142 void Object::Accept(Visitor* vis) { | |
143 vis->Visit(*this); | |
144 } | |
145 | |
146 const Value* Object::GetProperty(const std::string& name) const { | |
147 for (size_t i = 0; i < value_.size(); i++) { | |
148 const std::pair<std::string, Value*>& prop = value_[i]; | |
149 if (prop.first == name) | |
150 return prop.second; | |
151 } | |
152 return NULL; | |
153 } | |
154 | |
155 Value* Object::GetProperty(const std::string& name) { | |
156 for (size_t i = 0; i < value_.size(); i++) { | |
157 std::pair<std::string, Value*>& prop = value_[i]; | |
158 if (prop.first == name) | |
159 return prop.second; | |
160 } | |
161 return NULL; | |
162 } | |
163 | |
164 int Object::GetIntProperty(const std::string& name) const { | |
165 int result = 0; | |
166 const Number* num_val = dynamic_value_cast<Number>(GetProperty(name)); | |
167 if (NULL != num_val) | |
168 result = num_val->AsInt(); | |
169 return result; | |
170 } | |
171 | |
172 void Object::SetProperty(const std::string& name, Value* value) { | |
173 DeleteProperty(name); | |
174 value_.push_back(std::pair<std::string, Value*>(name, value)); | |
175 } | |
176 | |
177 void Object::SetProperty(const std::string& name, const std::string& value) { | |
178 SetProperty(name, new String(value)); | |
179 } | |
180 | |
181 void Object::Clear() { | |
182 for (size_t i = 0; i < value_.size(); i++) { | |
183 std::pair<std::string, Value*>& prop = value_[i]; | |
184 delete prop.second; | |
185 } | |
186 value_.clear(); | |
187 } | |
188 | |
189 void Object::GetPropertyNames(std::deque<std::string>* names) const { | |
190 for (size_t i = 0; i < value_.size(); i++) { | |
191 const std::pair<std::string, Value*>& prop = value_[i]; | |
192 names->push_back(prop.first); | |
193 } | |
194 } | |
195 | |
196 void Object::DeleteProperty(const std::string& name) { | |
197 TypeOf_value_::iterator it = value_.begin(); | |
198 TypeOf_value_::iterator end = value_.end(); | |
199 while (it != end) { | |
200 std::pair<std::string, Value*>& prop = *it; | |
201 if (prop.first == name) { | |
202 if (NULL != prop.second) | |
203 delete prop.second; | |
204 value_.erase(it); | |
205 return; | |
206 } | |
207 ++it; | |
208 } | |
209 } | |
210 | |
211 Blob::Blob() { | |
212 } | |
213 | |
214 Blob::Blob(const Blob& other) | |
215 : value_(other.value_) { | |
216 } | |
217 | |
218 Blob::Blob(debug::Blob& blob) | |
219 : value_(blob) { | |
220 } | |
221 | |
222 Blob::~Blob() { | |
223 } | |
224 | |
225 Value* Blob::Clone() const { | |
226 return new Blob(*this); | |
227 } | |
228 | |
229 void Blob::Accept(Visitor* vis) { | |
230 vis->Visit(*this); | |
231 } | |
232 | |
233 size_t Blob::Size() const { | |
234 return value_.size(); | |
235 } | |
236 | |
237 void Blob::Append(const void* data, size_t data_sz) { | |
238 debug::Blob other(data, data_sz); | |
239 value_.Append(other); | |
240 } | |
241 | |
242 void Blob::Clear() { | |
243 value_.Clear(); | |
244 } | |
245 | |
246 Array::Array() { | |
247 } | |
248 | |
249 Array::~Array() { | |
250 Clear(); | |
251 } | |
252 | |
253 Value* Array::Clone() const { | |
254 Array* copy = new Array; | |
255 if (NULL != copy) { | |
256 size_t num = Size(); | |
257 for (size_t i = 0; i < num; i++) { | |
258 Value* el_copy = GetAt(i)->Clone(); | |
259 if (NULL != el_copy) | |
260 copy->SetAt(i, el_copy); | |
261 } | |
262 } | |
263 return copy; | |
264 } | |
265 | |
266 void Array::Accept(Visitor* vis) { | |
267 vis->Visit(*this); | |
268 } | |
269 | |
270 size_t Array::Size() const { | |
271 return value_.size(); | |
272 } | |
273 | |
274 const Value* Array::GetAt(size_t i) const { | |
275 return value_[i]; | |
276 } | |
277 | |
278 Value* Array::GetAt(size_t i) { | |
279 return value_[i]; | |
280 } | |
281 | |
282 void Array::SetAt(size_t i, Value* value) { | |
283 while (Size() <= i) | |
284 Append(new Null); | |
285 if (NULL != value_[i]) | |
286 delete value_[i]; | |
287 value_[i] = value; | |
288 } | |
289 | |
290 void Array::Append(Value* value) { | |
291 value_.push_back(value); | |
292 } | |
293 | |
294 void Array::Clear() { | |
295 size_t num = Size(); | |
296 for (size_t i = 0; i < num; i++) | |
297 delete GetAt(i); | |
298 value_.clear(); | |
299 } | |
300 | |
301 } // namespace json | |
302 | |
303 /* | |
304 #define DBG_LOG(id, fmt, ...) do {\ | |
305 static EventIdChecker(id); | |
306 if (NULL != debug::Logger::Get()) {\ | |
307 debug::Logger::Get()->Log(id, fmt, __VA_ARGS__);\ | |
308 }\ | |
309 } while(0)\ | |
310 */ | |
OLD | NEW |