@@ -334,7 +334,6 @@ bool JSONVar::hasOwnProperty(const char* key) const
334
334
}
335
335
336
336
cJSON* json = cJSON_GetObjectItemCaseSensitive (_json, key);
337
-
338
337
return (json != NULL );
339
338
}
340
339
@@ -414,56 +413,77 @@ void JSONVar::replaceJson(struct cJSON* json)
414
413
415
414
// ---------------------------------------------------------------------
416
415
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);
422
418
423
- bool JSONVar::hasPropertyEqualTo (const char * key, const char * value) const {
424
- if (!this .hasProperty (key)){
419
+ if (!cJSON_IsObject (_json)) {
425
420
return false ;
426
421
}
427
422
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
+ }
434
427
435
428
// ---------------------------------------------------------------------
436
429
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
+ }
440
433
441
434
// ---------------------------------------------------------------------
442
435
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);
450
457
}
458
+ return JSONVar (json, _json);
451
459
}
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);
463
471
}
464
472
}
465
473
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);
467
487
}
468
488
469
489
JSONVar undefined;
0 commit comments