Skip to content

Commit 3d1af55

Browse files
committed
Add undefined and hasOwnProperty
1 parent ad99dc2 commit 3d1af55

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ JSON KEYWORD1
88
JSONVar KEYWORD1
99
var KEYWORD1
1010
null KEYWORD1
11+
undefined KEYWORD1
1112

1213
#######################################
1314
# Methods and Functions
@@ -19,6 +20,7 @@ stringify KEYWORD2
1920

2021
length KEYWORD2
2122
keys KEYWORD2
23+
hasOwnProperty KEYWORD2
2224

2325
#######################################
2426
# Constants

src/JSONVar.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,18 @@ JSONVar::operator const char*() const
141141

142142
void JSONVar::operator=(const JSONVar& v)
143143
{
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+
}
145156
}
146157

147158
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
@@ -195,12 +206,13 @@ void JSONVar::operator=(nullptr_t)
195206

196207
bool JSONVar::operator==(const JSONVar& v) const
197208
{
198-
return cJSON_Compare(_json, v._json, 1);
209+
return cJSON_Compare(_json, v._json, 1) ||
210+
(_json == NULL && v._json == NULL);
199211
}
200212

201213
bool JSONVar::operator==(nullptr_t) const
202214
{
203-
return (_json == NULL || cJSON_IsNull(_json));
215+
return (cJSON_IsNull(_json));
204216
}
205217

206218
JSONVar JSONVar::operator[](const char* key)
@@ -283,6 +295,17 @@ JSONVar JSONVar::keys() const
283295
return JSONVar(cJSON_CreateStringArray(keys, length), NULL);
284296
}
285297

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+
286309
JSONVar JSONVar::parse(const char* s)
287310
{
288311
cJSON* json = cJSON_Parse(s);
@@ -314,9 +337,11 @@ const char* JSONVar::typeof_(const JSONVar& value)
314337
{
315338
struct cJSON* json = value._json;
316339

317-
if (cJSON_IsBool(json)) {
340+
if (json == NULL || cJSON_IsInvalid(json)) {
341+
return "undefined";
342+
} else if (cJSON_IsBool(json)) {
318343
return "boolean";
319-
} else if (json == NULL || cJSON_IsNull(json)) {
344+
} else if (cJSON_IsNull(json)) {
320345
return "null"; // TODO: should this return "object" to be more JS like?
321346
} else if (cJSON_IsNumber(json)) {
322347
return "number";
@@ -349,3 +374,5 @@ void JSONVar::replaceJson(struct cJSON* json)
349374
}
350375
}
351376
}
377+
378+
JSONVar undefined;

src/JSONVar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class JSONVar : public Printable {
6969

7070
int length() const;
7171
JSONVar keys() const;
72+
bool hasOwnProperty(const char* key) const;
7273

7374
static JSONVar parse(const char* s);
7475
static JSONVar parse(const String& s);
@@ -89,4 +90,6 @@ class JSONVar : public Printable {
8990
typedef JSONVar var;
9091
#endif
9192

93+
extern JSONVar undefined;
94+
9295
#endif

0 commit comments

Comments
 (0)