change code

Fork of MbedJSONValue_copy by Lawrence Lee

Revision:
6:f8bec9cada34
Parent:
5:e15fd7be9e74
Child:
7:62ce68f5a38c
--- a/MbedJSONValue.cpp	Wed Mar 02 03:27:22 2016 +0000
+++ b/MbedJSONValue.cpp	Tue Apr 05 11:37:53 2016 +0000
@@ -3,6 +3,13 @@
 # include <stdlib.h>
 # include <stdio.h>
 
+bool default_json_cb(void* param)
+{
+    printf("No callback function\n");
+    return false;
+}
+
+
 // Clean up
 void MbedJSONValue::clean() {
     switch (_type) {
@@ -10,8 +17,8 @@
             delete _value.asString;
             break;
         case TypeArray:
-            for (int i = 0; i < index_array; i++)
-                delete array[i];
+            //for (int i = 0; i < index_array; i++)
+                //delete array[i];
             index_array = 0;
             break;
         case TypeObject:
@@ -100,11 +107,15 @@
 
 
 void MbedJSONValue::serialize(std::back_insert_iterator<std::string> oi) {
+    printf("SP:%X   _type=%d, index=%d\r\n",__current_sp(),_type, index_token);
+
     switch (_type) {
         case TypeString:
+        printf("typestring\r\n");
             serialize_str(*_value.asString, oi);
             break;
         case TypeArray: {
+        printf("typearray\r\n");
             *oi++ = '[';
             for (int i = 0; i < index_array; i++) {
                 if (i)
@@ -115,10 +126,13 @@
             break;
         }
         case TypeObject: {
+            printf("typeObject\r\n");
             *oi++ = '{';
             for (int i = 0; i < index_token; i++) {
+                printf("i=%d ",i);
                 if (i)
                     *oi++ = ',';
+                printf("token_name[%d]=%s\r\n",i,token_name[i]->c_str());
                 serialize_str(*(token_name[i]), oi);
                 *oi++ = ':';
                 (*(token[i])).serialize(oi);
@@ -127,6 +141,7 @@
             break;
         }
         default:
+        printf("default\r\n");
             copy(to_str(), oi);
             break;
     }
@@ -136,19 +151,26 @@
 
 MbedJSONValue& MbedJSONValue::operator[](int i) {
     _type = TypeArray;
-    if (i < NB_TOKEN && index_array == i ) {
+    //if (i < NB_TOKEN && index_array == i ) {
+    if( index_array == i){
 #ifdef DEBUG
         printf("will add an element to the array\r\n");
 #endif
-        array[i] = new MbedJSONValue();
+        //array[i] = new MbedJSONValue();
+        //array.push_back(new MbedJSONValue());
+        token.push_back(new MbedJSONValue());
         index_array++;
-        return *(array[i]);
+        //return *(array[i]);
+        return *(token[i]);
     }
-    if (i < NB_TOKEN && index_array > i)
-        return *(array[i]);
+    //if (i < NB_TOKEN && index_array > i)
+    if(index_array > i)
+//        return *(array[i]);
+        return *(token[i]);
 
     //if the user is not doing something wrong, this code is never executed!!
     return *(new MbedJSONValue());
+    //return 0;
 }
 
 MbedJSONValue& MbedJSONValue::operator[](std::string k) {
@@ -159,6 +181,7 @@
         printf("str: %s\r\n", token_name[i]->c_str());
 #endif
         //existing token
+
         if (!strcmp(k.c_str(), token_name[i]->c_str())) {
 #ifdef DEBUG
             printf("token found: %d\r\n", i);
@@ -167,11 +190,15 @@
         }
     }
 
-    if(index_token >= NB_TOKEN)
-        index_token = NB_TOKEN - 1;
+    //if(index_token >= NB_TOKEN)
+    //    index_token = NB_TOKEN - 1;
     //non existing token
-    token_name[index_token] = new std::string(k);
-    token[index_token] = new MbedJSONValue();
+    //token_name[index_token] = new std::string(k);
+    token_name.push_back(new std::string(k));
+    printf("New token_name[%d]=%X, %s\r\n", index_token, token_name[index_token], token_name[index_token]->c_str());
+    //token[index_token] = new MbedJSONValue();
+    token.push_back(new MbedJSONValue());
+    printf("New token[%d]=%X, %d\r\n", index_token, token[index_token], token[index_token]->getType());
     index_token++;
     return *(token[index_token - 1]);
 }
@@ -181,8 +208,11 @@
     for (int i = 0; i < index_token; i++) {
 #ifdef DEBUG
         printf("k: %s\r\n", k.c_str());
-        printf("str: %s\r\n", token_name[i]->c_str());
+        //printf("str: %s\r\n", token_name[i]->c_str());
 #endif
+        printf("[%s]", k.c_str());
+        printf(" comp ");
+        printf("[%s]\r\n", token_name[i]->c_str());
         if (!strcmp(k.c_str(), token_name[i]->c_str())) {
 #ifdef DEBUG
             printf("token found: %d\r\n", i);
@@ -193,12 +223,13 @@
     
     //if the user is not doing something wrong, this code is never executed!!
     return *(new MbedJSONValue());
-
+    //return 0;
 }
 
 
 // Operators
 MbedJSONValue& MbedJSONValue::operator=(MbedJSONValue const& rhs) {
+    //printf("=json\r\n");
     if (this != &rhs) {
         clean();
         _type = rhs._type;
@@ -213,6 +244,7 @@
                 _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:
@@ -244,3 +276,4 @@
     return -1;
 }
 
+