@@ -141,7 +141,18 @@ JSONVar::operator const char*() const
141
141
142
142
void JSONVar::operator =(const JSONVar& v)
143
143
{
144
- replaceJson (cJSON_Duplicate (v._json , true ));
144
+ if (&v == &undefined) {
145
+ if (cJSON_IsObject (_parent)) {
146
+ cJSON_DeleteItemFromObjectCaseSensitive (_parent, _json->string );
147
+
148
+ _json = NULL ;
149
+ _parent = NULL ;
150
+ } else {
151
+ replaceJson (cJSON_CreateNull ());
152
+ }
153
+ } else {
154
+ replaceJson (cJSON_Duplicate (v._json , true ));
155
+ }
145
156
}
146
157
147
158
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
@@ -195,12 +206,13 @@ void JSONVar::operator=(nullptr_t)
195
206
196
207
bool JSONVar::operator ==(const JSONVar& v) const
197
208
{
198
- return cJSON_Compare (_json, v._json , 1 );
209
+ return cJSON_Compare (_json, v._json , 1 ) ||
210
+ (_json == NULL && v._json == NULL );
199
211
}
200
212
201
213
bool JSONVar::operator ==(nullptr_t ) const
202
214
{
203
- return (_json == NULL || cJSON_IsNull (_json));
215
+ return (cJSON_IsNull (_json));
204
216
}
205
217
206
218
JSONVar JSONVar::operator [](const char * key)
@@ -283,6 +295,17 @@ JSONVar JSONVar::keys() const
283
295
return JSONVar (cJSON_CreateStringArray (keys, length), NULL );
284
296
}
285
297
298
+ bool JSONVar::hasOwnProperty (const char * key) const
299
+ {
300
+ if (!cJSON_IsObject (_json)) {
301
+ return false ;
302
+ }
303
+
304
+ cJSON* json = cJSON_GetObjectItemCaseSensitive (_json, key);
305
+
306
+ return (json != NULL );
307
+ }
308
+
286
309
JSONVar JSONVar::parse (const char * s)
287
310
{
288
311
cJSON* json = cJSON_Parse (s);
@@ -314,9 +337,11 @@ const char* JSONVar::typeof_(const JSONVar& value)
314
337
{
315
338
struct cJSON * json = value._json ;
316
339
317
- if (cJSON_IsBool (json)) {
340
+ if (json == NULL || cJSON_IsInvalid (json)) {
341
+ return " undefined" ;
342
+ } else if (cJSON_IsBool (json)) {
318
343
return " boolean" ;
319
- } else if (json == NULL || cJSON_IsNull (json)) {
344
+ } else if (cJSON_IsNull (json)) {
320
345
return " null" ; // TODO: should this return "object" to be more JS like?
321
346
} else if (cJSON_IsNumber (json)) {
322
347
return " number" ;
@@ -349,3 +374,5 @@ void JSONVar::replaceJson(struct cJSON* json)
349
374
}
350
375
}
351
376
}
377
+
378
+ JSONVar undefined;
0 commit comments