Skip to content

Commit 2058d1f

Browse files
author
Joe Andolina
committed
Adding JSON helpers
1 parent f9cd374 commit 2058d1f

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/JSONVar.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,58 @@ void JSONVar::replaceJson(struct cJSON* json)
412412
}
413413
}
414414

415+
//---------------------------------------------------------------------
416+
417+
bool JSONVar::hasPropertyEqualTo(const String& key, String& value) const{
418+
return hasPropertyEqualTo(key.c_str(), value.c_str());
419+
}
420+
421+
//---------------------------------------------------------------------
422+
423+
bool JSONVar::hasPropertyEqualTo(const char* key, const char* value) const {
424+
if(!hasProperty(sourceProperty)){
425+
return false;
426+
}
427+
428+
if(strcmp(targetValue, source[sourceProperty]) == 0){
429+
return true;
430+
}
431+
432+
return false;
433+
}
434+
435+
//---------------------------------------------------------------------
436+
437+
JSONVar JSONVar::getPropertyWithValue(const String& key, String& value, String child = "") const {
438+
return getPropertyWithValue(key..c_str(), value.c_str(), child.c_str());
439+
}
440+
441+
//---------------------------------------------------------------------
442+
443+
JSONVar JSONVar::getPropertyWithValue(const char* key, const char* value, const char* child = '') const {
444+
if(this.hasOwnPropertyEqualTo(key, value)){
445+
if(source.hasOwnProperty(childName)){
446+
return source[childName];
447+
}
448+
else {
449+
return source;
450+
}
451+
}
452+
453+
if(JSON.typeof_(source) == "array"){
454+
for (int i = 0; i < source.length(); ++i) {
455+
if(_hasMatch(source[i], sourceProperty, targetValue)){
456+
if(source[i].hasOwnProperty(childName)){
457+
return source[i][childName];
458+
}
459+
else {
460+
return source[i];
461+
}
462+
}
463+
}
464+
}
465+
466+
return null;
467+
}
468+
415469
JSONVar undefined;

src/JSONVar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class JSONVar : public Printable {
7777
JSONVar keys() const;
7878
bool hasOwnProperty(const char* key) const;
7979
bool hasOwnProperty(const String& key) const;
80+
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;
8086

8187
static JSONVar parse(const char* s);
8288
static JSONVar parse(const String& s);

0 commit comments

Comments
 (0)