Skip to content

Added simple filtering and object property value checking #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/JSONVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ bool JSONVar::hasOwnProperty(const char* key) const
}

cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);

return (json != NULL);
}

Expand Down Expand Up @@ -412,4 +411,85 @@ void JSONVar::replaceJson(struct cJSON* json)
}
}

//---------------------------------------------------------------------

bool JSONVar::hasPropertyEqual(const char* key, const char* value) const {
if (!cJSON_IsObject(_json)) {
return false;
}

cJSON* json = cJSON_GetObjectItemCaseSensitive(_json, key);
return json != NULL && strcmp(value, json->valuestring) == 0;
}

//---------------------------------------------------------------------

bool JSONVar::hasPropertyEqual(const char* key, const JSONVar& value) const {
return this->hasPropertyEqual(key, (const char*)value);
}

//---------------------------------------------------------------------

bool JSONVar::hasPropertyEqual(const String& key, const String& value) const {
return this->hasPropertyEqual(key.c_str(), value.c_str());
}

//---------------------------------------------------------------------

bool JSONVar::hasPropertyEqual(const String& key, const JSONVar& value) const {
return this->hasPropertyEqual(key.c_str(), (const char*)value);
}

//---------------------------------------------------------------------

JSONVar JSONVar::filter(const char* key, const char* value) const {
cJSON* item;
cJSON* test;
cJSON* json = cJSON_CreateArray();

if(cJSON_IsObject(_json)){
test = cJSON_GetObjectItem(_json, key);

if(test != NULL && strcmp(value, test->valuestring) == 0){
return (*this);
}
}

for (int i = 0; i < cJSON_GetArraySize(_json); i++) {
item = cJSON_GetArrayItem(_json, i);

if (item == NULL) {
continue;
}

test = cJSON_GetObjectItem(item, key);

if(test != NULL && strcmp(value, test->valuestring) == 0){
cJSON_AddItemToArray(json, cJSON_Duplicate(item,true));
}
}

if(cJSON_GetArraySize(json) == 0){
return JSONVar(NULL, NULL);
}

if(cJSON_GetArraySize(json) == 1){
return JSONVar(cJSON_GetArrayItem(json, 0), _json);
}

return JSONVar(json, _json);
}

JSONVar JSONVar::filter(const char* key, const JSONVar& value) const {
return this->filter(key, (const char*)value);
}

JSONVar JSONVar::filter(const String& key, const String& value) const {
return this->filter(key.c_str(), value.c_str());
}

JSONVar JSONVar::filter(const String& key, const JSONVar& value) const {
return this->filter(key.c_str(), (const char*)value);
}

JSONVar undefined;
12 changes: 11 additions & 1 deletion src/JSONVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,22 @@ class JSONVar : public Printable {
JSONVar operator[](const char* key);
JSONVar operator[](const String& key);
JSONVar operator[](int index);
JSONVar operator[](const JSONVar& key);
JSONVar operator[](const JSONVar& key);

int length() const;
JSONVar keys() const;
bool hasOwnProperty(const char* key) const;
bool hasOwnProperty(const String& key) const;

bool hasPropertyEqual(const char* key, const char* value) const;
bool hasPropertyEqual(const char* key, const JSONVar& value) const;
bool hasPropertyEqual(const String& key, const String& value) const;
bool hasPropertyEqual(const String& key, const JSONVar& value) const;

JSONVar filter(const char* key, const char* value) const;
JSONVar filter(const char* key, const JSONVar& value) const;
JSONVar filter(const String& key, const String& value) const;
JSONVar filter(const String& key, const JSONVar& value) const;

static JSONVar parse(const char* s);
static JSONVar parse(const String& s);
Expand Down