@@ -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
469489JSONVar undefined;
0 commit comments