Dynamic Alloc Remove array Call back function

Dependents:   WIZwiki-REST_mc

Fork of MbedJSONValue by Lawrence Lee

Files at this revision

API Documentation at this revision

Comitter:
MidnightCow
Date:
Tue Apr 05 11:37:53 2016 +0000
Parent:
5:e15fd7be9e74
Commit message:
1st Release
; - MbedJson Modified (Dynamic alloc)

Changed in this revision

MbedJSONValue.cpp Show annotated file Show diff for this revision Revisions of this file
MbedJSONValue.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
 }
 
+
--- a/MbedJSONValue.h	Wed Mar 02 03:27:22 2016 +0000
+++ b/MbedJSONValue.h	Tue Apr 05 11:37:53 2016 +0000
@@ -33,6 +33,7 @@
 
 #define NB_TOKEN 10
 
+
 //#define DEBUG
 
 /*!< Number maximum of MbedJSONValue in an array or an object */
@@ -41,6 +42,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <vector>
+
+
+typedef  bool (*json_call_back)(void* param);
+
+bool default_json_cb(void* param);
 
 /** MbedJSONValue class
  *
@@ -104,7 +111,6 @@
  
 class MbedJSONValue {
 public:
-
     /**
     * \enum Type
     * \brief All types which can be used
@@ -122,14 +128,14 @@
     /**
     * MbedJSONValue constructor of type TypeNull
     */
-    MbedJSONValue() : _type(TypeNull), index_array(0), index_token(0) {}
+    MbedJSONValue() : accessible(false), cb_action(default_json_cb), _type(TypeNull), index_array(0), index_token(0)  {}
     
     /**
     * MbedJSONValue constructor of type TypeBoolean
     *
     * @param value the object created will be initialized with this boolean
     */
-    MbedJSONValue(bool value) : _type(TypeBoolean), index_array(0), index_token(0) {
+    MbedJSONValue(bool value) : accessible(false), cb_action(default_json_cb), _type(TypeBoolean), index_array(0), index_token(0){
         _value.asBool = value;
     }
     
@@ -138,7 +144,7 @@
     *
     * @param value the object created will be initialized with this integer
     */
-    MbedJSONValue(int value)  : _type(TypeInt), index_array(0), index_token(0) {
+    MbedJSONValue(int value)  :accessible(false), cb_action(default_json_cb), _type(TypeInt), index_array(0), index_token(0) {
         _value.asInt = value;
     }
     
@@ -147,7 +153,7 @@
     *
     * @param value the object created will be initialized with this double
     */
-    MbedJSONValue(double value)  : _type(TypeDouble), index_array(0), index_token(0) {
+    MbedJSONValue(double value)  : accessible(false), cb_action(default_json_cb), _type(TypeDouble), index_array(0), index_token(0) {
         _value.asDouble = value;
     }
 
@@ -156,7 +162,9 @@
     *
     * @param value the object created will be initialized with this string
     */
-    MbedJSONValue(std::string const& value) : _type(TypeString), index_array(0), index_token(0) {
+    MbedJSONValue(std::string const& value) : accessible(false), cb_action(default_json_cb), _type(TypeString), index_array(0), index_token(0) {
+//        printf("constructor:%s\r\n",value.c_str());
+        token.resize(0);
         _value.asString = new std::string(value);
     }
 
@@ -165,7 +173,7 @@
     *
     * @param value the object created will be initialized with this string
     */
-    MbedJSONValue(const char* value)  : _type(TypeString), index_array(0), index_token(0) {
+    MbedJSONValue(const char* value)  : accessible(false), cb_action(default_json_cb), _type(TypeString), index_array(0), index_token(0) {
         _value.asString = new std::string(value);
     }
 
@@ -297,8 +305,12 @@
     */
     std::string serialize();
 
-protected:
 
+    
+//protected:
+
+    bool accessible;
+    json_call_back  cb_action;
     // object type
     Type _type;
     
@@ -307,11 +319,14 @@
     int index_token;
 
     //an object can contain NB_TOKEN tokens. Each token have a name
-    MbedJSONValue * token[NB_TOKEN];
-    std::string * token_name[NB_TOKEN];
+    //MbedJSONValue * token[NB_TOKEN];
+    std::vector<MbedJSONValue*> token;
+    //std::string * token_name[NB_TOKEN];
+    std::vector<std::string*> token_name;
 
     //an object can contain an array of NB_TOKEN elements
-    MbedJSONValue * array[NB_TOKEN];
+    //MbedJSONValue * array[NB_TOKEN];
+    //std::vector<MbedJSONValue*> array;
 
     // Clean up
     void clean();
@@ -324,7 +339,8 @@
     } _value;
 
 
-    MbedJSONValue& operator[](int i) const { return *(array[i]); }
+    //MbedJSONValue& operator[](int i) const { return *(array[i]); }
+    MbedJSONValue& operator[](int i) const { return *(token[i]); }
     MbedJSONValue& operator[](std::string k) const;
     
     std::string to_str();