Skip to content

Commit 1da9978

Browse files
author
Joe Andolina
committed
Added filtering and property comparison
1 parent 1dac132 commit 1da9978

File tree

2 files changed

+66
-42
lines changed

2 files changed

+66
-42
lines changed

src/JSONVar.cpp

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ bool JSONVar::hasOwnProperty(const char* key) const
334334
}
335335

336336
cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
337-
338337
return (json != NULL);
339338
}
340339

@@ -414,56 +413,77 @@ void JSONVar::replaceJson(struct cJSON* json)
414413

415414
//---------------------------------------------------------------------
416415

417-
bool JSONVar::hasPropertyEqualTo(const String& key, String& value) const{
418-
return this.hasPropertyEqualTo(key.c_str(), value.c_str());
419-
}
420-
421-
//---------------------------------------------------------------------
416+
bool JSONVar::hasPropertyEqual(const char* key, const char* value) const {
417+
// Serial.printf("JSONVar::hasPropertyEqual - %s == %s\n", key, value);
422418

423-
bool JSONVar::hasPropertyEqualTo(const char* key, const char* value) const {
424-
if(!this.hasProperty(key)){
419+
if (!cJSON_IsObject(_json)) {
425420
return false;
426421
}
427422

428-
if(strcmp(value, this[key]) == 0){
429-
return true;
430-
}
431-
432-
return false;
433-
}
423+
cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
424+
// Serial.printf("JSONVar::hasPropertyEqual - Found %s\n", json->valuestring);
425+
return json != NULL && strcmp(value, json->valuestring) == 0;
426+
}
434427

435428
//---------------------------------------------------------------------
436429

437-
JSONVar JSONVar::getPropertyWithValue(const String& key, String& value, String child = "") const {
438-
return this.getPropertyWithValue(key..c_str(), value.c_str(), child.c_str());
439-
}
430+
bool JSONVar::hasPropertyEqual(const char* key, const JSONVar& value) const {
431+
return this->hasPropertyEqual(key, (const char*)value);
432+
}
440433

441434
//---------------------------------------------------------------------
442435

443-
JSONVar JSONVar::getPropertyWithValue(const char* key, const char* value, const char* child = '') const {
444-
if(this.hasOwnPropertyEqualTo(key, value)){
445-
if(this.hasOwnProperty(child)){
446-
return this[child];
447-
}
448-
else {
449-
return this;
436+
bool JSONVar::hasPropertyEqual(const String& key, const String& value) const {
437+
return this->hasPropertyEqual(key.c_str(), value.c_str());
438+
}
439+
440+
//---------------------------------------------------------------------
441+
442+
bool JSONVar::hasPropertyEqual(const String& key, const JSONVar& value) const {
443+
return this->hasPropertyEqual(key.c_str(), (const char*)value);
444+
}
445+
446+
//---------------------------------------------------------------------
447+
448+
JSONVar JSONVar::filter(const char* key, const char* value) const {
449+
cJSON* item;
450+
cJSON* test;
451+
cJSON* json = cJSON_CreateArray();
452+
453+
if(JSONVar::typeof_((*this)) != "array"){
454+
test = cJSON_GetObjectItemCaseSensitive(_json, key);
455+
if(test != NULL && strcmp(value, test->valuestring) == 0){
456+
cJSON_AddItemToArray(json, _json);
450457
}
458+
return JSONVar(json, _json);
451459
}
452-
453-
if(JSON.typeof_(this) == "array"){
454-
for (int i = 0; i < this.length(); ++i) {
455-
if(this.hasPropertyEqualTo(key, value)){
456-
if(this[i].hasOwnProperty(child)){
457-
return this[i][child];
458-
}
459-
else {
460-
return this[i];
461-
}
462-
}
460+
461+
for (int i = 0; i < cJSON_GetArraySize(_json); ++i) {
462+
item = cJSON_GetArrayItem(_json, i);
463+
if (item == NULL) {
464+
continue;
465+
}
466+
467+
test = cJSON_GetObjectItemCaseSensitive(item, key);
468+
469+
if(test != NULL && strcmp(value, test->valuestring) == 0){
470+
cJSON_AddItemToArray(json, item);
463471
}
464472
}
465473

466-
return null;
474+
return JSONVar(json, _json);
475+
}
476+
477+
JSONVar JSONVar::filter(const char* key, const JSONVar& value) const {
478+
return this->filter(key, (const char*)value);
479+
}
480+
481+
JSONVar JSONVar::filter(const String& key, const String& value) const {
482+
return this->filter(key.c_str(), value.c_str());
483+
}
484+
485+
JSONVar JSONVar::filter(const String& key, const JSONVar& value) const {
486+
return this->filter(key.c_str(), (const char*)value);
467487
}
468488

469489
JSONVar undefined;

src/JSONVar.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,22 @@ class JSONVar : public Printable {
7171
JSONVar operator[](const char* key);
7272
JSONVar operator[](const String& key);
7373
JSONVar operator[](int index);
74-
JSONVar operator[](const JSONVar& key);
74+
JSONVar operator[](const JSONVar& key);
7575

7676
int length() const;
7777
JSONVar keys() const;
7878
bool hasOwnProperty(const char* key) const;
7979
bool hasOwnProperty(const String& key) const;
8080

81-
bool haPropertyEqualTo(const String& key, String& value) const;
82-
bool hasPropertyEqualTo(const char* key, const char* value) const;
83-
84-
JSONVar getPropertyWithValue(const String& key, String& value, String child = "") const;
85-
JSONVar getPropertyWithValue(const char* key, const char* value, const char* child = '') const;
81+
bool hasPropertyEqual(const char* key, const char* value) const;
82+
bool hasPropertyEqual(const char* key, const JSONVar& value) const;
83+
bool hasPropertyEqual(const String& key, const String& value) const;
84+
bool hasPropertyEqual(const String& key, const JSONVar& value) const;
85+
86+
JSONVar filter(const char* key, const char* value) const;
87+
JSONVar filter(const char* key, const JSONVar& value) const;
88+
JSONVar filter(const String& key, const String& value) const;
89+
JSONVar filter(const String& key, const JSONVar& value) const;
8690

8791
static JSONVar parse(const char* s);
8892
static JSONVar parse(const String& s);

0 commit comments

Comments
 (0)