Skip to content

Commit 6235dc7

Browse files
committed
Merge branch 'master' of https://github.com/Phando/Arduino_JSON into phando/master
2 parents ff955e9 + 607acab commit 6235dc7

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/JSONVar.cpp

+81-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ bool JSONVar::hasOwnProperty(const char* key) const
428428
}
429429

430430
cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
431-
432431
return (json != NULL);
433432
}
434433

@@ -506,4 +505,85 @@ void JSONVar::replaceJson(struct cJSON* json)
506505
}
507506
}
508507

508+
//---------------------------------------------------------------------
509+
510+
bool JSONVar::hasPropertyEqual(const char* key, const char* value) const {
511+
if (!cJSON_IsObject(_json)) {
512+
return false;
513+
}
514+
515+
cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
516+
return json != NULL && strcmp(value, json->valuestring) == 0;
517+
}
518+
519+
//---------------------------------------------------------------------
520+
521+
bool JSONVar::hasPropertyEqual(const char* key, const JSONVar& value) const {
522+
return this->hasPropertyEqual(key, (const char*)value);
523+
}
524+
525+
//---------------------------------------------------------------------
526+
527+
bool JSONVar::hasPropertyEqual(const String& key, const String& value) const {
528+
return this->hasPropertyEqual(key.c_str(), value.c_str());
529+
}
530+
531+
//---------------------------------------------------------------------
532+
533+
bool JSONVar::hasPropertyEqual(const String& key, const JSONVar& value) const {
534+
return this->hasPropertyEqual(key.c_str(), (const char*)value);
535+
}
536+
537+
//---------------------------------------------------------------------
538+
539+
JSONVar JSONVar::filter(const char* key, const char* value) const {
540+
cJSON* item;
541+
cJSON* test;
542+
cJSON* json = cJSON_CreateArray();
543+
544+
if(cJSON_IsObject(_json)){
545+
test = cJSON_GetObjectItem(_json, key);
546+
547+
if(test != NULL && strcmp(value, test->valuestring) == 0){
548+
return (*this);
549+
}
550+
}
551+
552+
for (int i = 0; i < cJSON_GetArraySize(_json); i++) {
553+
item = cJSON_GetArrayItem(_json, i);
554+
555+
if (item == NULL) {
556+
continue;
557+
}
558+
559+
test = cJSON_GetObjectItem(item, key);
560+
561+
if(test != NULL && strcmp(value, test->valuestring) == 0){
562+
cJSON_AddItemToArray(json, cJSON_Duplicate(item,true));
563+
}
564+
}
565+
566+
if(cJSON_GetArraySize(json) == 0){
567+
return JSONVar(NULL, NULL);
568+
}
569+
570+
if(cJSON_GetArraySize(json) == 1){
571+
return JSONVar(cJSON_GetArrayItem(json, 0), _json);
572+
}
573+
574+
return JSONVar(json, _json);
575+
}
576+
577+
JSONVar JSONVar::filter(const char* key, const JSONVar& value) const {
578+
return this->filter(key, (const char*)value);
579+
}
580+
581+
JSONVar JSONVar::filter(const String& key, const String& value) const {
582+
return this->filter(key.c_str(), value.c_str());
583+
}
584+
585+
JSONVar JSONVar::filter(const String& key, const JSONVar& value) const {
586+
return this->filter(key.c_str(), (const char*)value);
587+
}
588+
509589
JSONVar undefined;

src/JSONVar.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,22 @@ class JSONVar : public Printable {
8888
JSONVar operator[](const char* key);
8989
JSONVar operator[](const String& key);
9090
JSONVar operator[](int index);
91-
JSONVar operator[](const JSONVar& key);
91+
JSONVar operator[](const JSONVar& key);
9292

9393
int length() const;
9494
JSONVar keys() const;
9595
bool hasOwnProperty(const char* key) const;
9696
bool hasOwnProperty(const String& key) const;
97+
98+
bool hasPropertyEqual(const char* key, const char* value) const;
99+
bool hasPropertyEqual(const char* key, const JSONVar& value) const;
100+
bool hasPropertyEqual(const String& key, const String& value) const;
101+
bool hasPropertyEqual(const String& key, const JSONVar& value) const;
102+
103+
JSONVar filter(const char* key, const char* value) const;
104+
JSONVar filter(const char* key, const JSONVar& value) const;
105+
JSONVar filter(const String& key, const String& value) const;
106+
JSONVar filter(const String& key, const JSONVar& value) const;
97107

98108
static JSONVar parse(const char* s);
99109
static JSONVar parse(const String& s);

0 commit comments

Comments
 (0)