change code

Fork of MbedJSONValue_copy by Lawrence Lee

Revision:
8:fb820325a830
Parent:
7:62ce68f5a38c
Child:
9:c917476c802b
--- a/MbedJSONValue.cpp	Tue May 03 02:31:26 2016 +0000
+++ b/MbedJSONValue.cpp	Tue May 31 10:30:22 2016 +0000
@@ -5,7 +5,7 @@
 
 bool default_json_cb(void* param)
 {
-    printf("No callback function\n");
+    printf("No callback function\r\n");
     return false;
 }
 
@@ -17,22 +17,21 @@
             delete _value.asString;
             break;
         case TypeArray:
-            //for (int i = 0; i < index_array; i++)
-                //delete array[i];
-            index_array = 0;
-            break;
         case TypeObject:
             for (int i = 0; i < index_token; i++) {
+#ifdef  DEBUG_MJSON               
+                printf("%d : this[%s] = %d\r\n",i, token_name[i]->c_str(), token[i]->_value.asInt);
+#endif                
                 delete token[i];
                 delete token_name[i];
             }
             index_token = 0;
+            index_array = 0;
             break;
         default:
             break;
     }
     _type = TypeNull;
-    _type = TypeNull;
 }
 
 bool MbedJSONValue::hasMember(char * name)
@@ -118,7 +117,7 @@
 #endif        
             serialize_str(*_value.asString, oi);
             break;
-        case TypeArray: {
+        case TypeArray: /*{
 #ifdef DEBUG_MJSON                
         printf("typearray\r\n");
 #endif        
@@ -130,7 +129,7 @@
             }
             *oi++ = ']';
             break;
-        }
+        }*/
         case TypeObject: {
 #ifdef DEBUG_MJSON                
             printf("typeObject\r\n");
@@ -164,22 +163,22 @@
 
 
 MbedJSONValue& MbedJSONValue::operator[](int i) {
-    _type = TypeArray;
+    _type = TypeObject;
     //if (i < NB_TOKEN && index_array == i ) {
-    if( index_array == i){
-#ifdef DEBUG
+    if( index_token == i){
+#ifdef  DEBUG_MJSON
         printf("will add an element to the array\r\n");
 #endif
         //array[i] = new MbedJSONValue();
         //array.push_back(new MbedJSONValue());
         token.push_back(new MbedJSONValue());
-        index_array++;
+        index_token++;
         //return *(array[i]);
         return *(token[i]);
     }
     //if (i < NB_TOKEN && index_array > i)
-    if(index_array > i)
-//        return *(array[i]);
+    if(index_token > i)
+    //        return *(array[i]);
         return *(token[i]);
 
     //if the user is not doing something wrong, this code is never executed!!
@@ -190,7 +189,6 @@
 MbedJSONValue& MbedJSONValue::operator[](std::string k) {
     _type = TypeObject;
     for (int i = 0; i < index_token; i++) {
-#ifdef DEBUG
 #ifdef DEBUG_MJSON
         printf("k: %s\r\n", k.c_str());
         printf("str: %s\r\n", token_name[i]->c_str());
@@ -198,7 +196,6 @@
         //existing token
 
         if (!strcmp(k.c_str(), token_name[i]->c_str())) {
-#ifdef DEBUG
 #ifdef DEBUG_MJSON
             printf("token found: %d\r\n", i);
 #endif
@@ -226,7 +223,6 @@
 MbedJSONValue& MbedJSONValue::operator[](std::string k) const
 {
     for (int i = 0; i < index_token; i++) {
-#ifdef DEBUG
 #ifdef DEBUG_MJSON
         printf("k: %s\r\n", k.c_str());
         //printf("str: %s\r\n", token_name[i]->c_str());
@@ -235,7 +231,6 @@
         printf(" comp ");
         printf("[%s]\r\n", token_name[i]->c_str());
         if (!strcmp(k.c_str(), token_name[i]->c_str())) {
-#ifdef DEBUG
 #ifdef DEBUG_MJSON
             printf("token found: %d\r\n", i);
 #endif
@@ -266,7 +261,6 @@
                 _value.asDouble = rhs._value.asDouble;
                 break;
             case TypeString:
-                  //printf("=json=%s\r\n",rhs._value.asString->c_str());
                 _value.asString = new std::string(*rhs._value.asString);
                 break;
             case TypeArray: