@@ -445,32 +445,97 @@ bool JSONVar::hasPropertyEqual(const String& key, const JSONVar& value) const
445
445
446
446
// ---------------------------------------------------------------------
447
447
448
+ // JSONVar JSONVar::filter(const char* key, const char* value) const {
449
+ // JSONVar item;
450
+ // cJSON* test;
451
+ // cJSON* json = cJSON_CreateArray();
452
+
453
+ // Serial.printf("JSONVar::filter - %s == %s\n", key, value);
454
+
455
+ // // if(!cJSON_IsArray(_json)){
456
+ // // // target = cJSON_CreateArray();
457
+ // // // cJSON_AddItemToArray(target, _json);
458
+ // // return (*this);
459
+ // // }
460
+
461
+ // // Serial.printf("JSON SIZE %d", cJSON_GetArraySize(_json));
462
+
463
+ // Serial.printf("This an array %d\n", (*this).length());
464
+
465
+ // for (int i = 0; i < (*this).length(); i++) {
466
+ // Serial.println("GettingItem");
467
+ // item = this[(int)i];
468
+ // Serial.println(item);
469
+ // Serial.println(item[(const char*)key]);
470
+ // Serial.println("GotItem");
471
+
472
+ // // Serial.println("Loop " + String(i));
473
+ // // Serial.println(this->operator[](i));//cJSON_GetArrayItem(_json, i);
474
+
475
+ // // if (item == NULL) {
476
+ // // Serial.println("Loop Null");
477
+ // // continue;
478
+ // // }
479
+
480
+ // if(item.hasPropertyEqual(key, value)){
481
+ // Serial.println("Got Match");
482
+ // return item;
483
+ // }
484
+ // else {
485
+ // Serial.println("NO Match");
486
+ // }
487
+ // }
488
+
489
+ // if(cJSON_GetArraySize(json) == 0){
490
+ // Serial.println("Returning Null");
491
+ // return NULL;
492
+ // }
493
+ // else if(cJSON_GetArraySize(json) == 1){
494
+ // Serial.println("Returning Single");
495
+ // return JSONVar(cJSON_GetArrayItem(json, 0), (*this)._json);
496
+ // }
497
+
498
+ // // Serial.println("Returning Array");
499
+ // return JSONVar();
500
+ // }
501
+
448
502
JSONVar JSONVar::filter (const char * key, const char * value) const {
449
503
cJSON* item;
450
504
cJSON* test;
451
505
cJSON* json = cJSON_CreateArray ();
452
506
453
- if (JSONVar::typeof_ ((*this )) != " array" ){
454
- test = cJSON_GetObjectItemCaseSensitive (_json, key);
507
+ Serial.printf (" JSONVar::filter - %s == %s\n " , key, value);
508
+
509
+ if (!cJSON_IsArray (_json)){
510
+ test = cJSON_GetObjectItem (_json, key);
511
+
455
512
if (test != NULL && strcmp (value, test->valuestring ) == 0 ){
456
- cJSON_AddItemToArray (json , _json);
513
+ return JSONVar ( cJSON_Duplicate (item, true ) , _json);
457
514
}
458
- return JSONVar (json, _json);
459
515
}
460
516
461
- for (int i = 0 ; i < cJSON_GetArraySize (_json); ++i ) {
517
+ for (int i = 0 ; i < cJSON_GetArraySize (_json); i++ ) {
462
518
item = cJSON_GetArrayItem (_json, i);
519
+
463
520
if (item == NULL ) {
464
521
continue ;
465
522
}
466
523
467
- test = cJSON_GetObjectItemCaseSensitive (item, key);
524
+ test = cJSON_GetObjectItem (item, key);
468
525
469
526
if (test != NULL && strcmp (value, test->valuestring ) == 0 ){
470
- cJSON_AddItemToArray (json, item);
527
+ cJSON_AddItemToArray (json, cJSON_Duplicate ( item, true ) );
471
528
}
472
529
}
473
530
531
+ if (cJSON_GetArraySize (json) == 0 ){
532
+ return JSONVar (NULL , NULL );
533
+ }
534
+
535
+ if (cJSON_GetArraySize (json) == 1 ){
536
+ return JSONVar (cJSON_GetArrayItem (json, 0 ), _json);
537
+ }
538
+
474
539
return JSONVar (json, _json);
475
540
}
476
541
0 commit comments