MbedJSONValue Fork and Delete Array

Fork of MbedJSONValue by Midnight Cow

Files at this revision

API Documentation at this revision

Comitter:
joon874
Date:
Tue May 31 10:30:22 2016 +0000
Parent:
7:62ce68f5a38c
Commit message:
WIZwiki-REST-io ver.2 ; POST Method Realized

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	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:
--- a/MbedJSONValue.h	Tue May 03 02:31:26 2016 +0000
+++ b/MbedJSONValue.h	Tue May 31 10:30:22 2016 +0000
@@ -164,7 +164,6 @@
     * @param value the object created will be initialized with this string
     */
     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);
     }
@@ -455,7 +454,7 @@
 
 
 inline bool _parse_string(MbedJSONValue& out, input& in) {
-#ifdef DEBUG
+#ifdef DEBUG_MJSON
     printf("string detected\r\n");
 #endif
     out = MbedJSONValue(std::string(""));
@@ -492,7 +491,7 @@
 }
 
 inline bool _parse_array(MbedJSONValue& out, input& in) {
-#ifdef DEBUG
+#ifdef DEBUG_MJSON
     printf("array detected\r\n");
 #endif
     int i = 0;
@@ -509,7 +508,7 @@
 }
 
 inline bool _parse_object(MbedJSONValue& out, input& in) {
-#ifdef DEBUG
+#ifdef DEBUG_MJSON
     printf("object detected\r\n");
 #endif
     if (in.expect('}')) {
@@ -519,7 +518,7 @@
         MbedJSONValue key, val;
         if (in.expect('"') && _parse_string(key, in) && in.expect(':') && _parse(val, in)) {
             out[key.get<std::string>().c_str()] = val;
-#ifdef DEBUG
+#ifdef DEBUG_MJSON
             printf("key: %s \r\n", key.get<std::string>().c_str());
 #endif
         } else {
@@ -530,7 +529,7 @@
 }
 
 inline bool _parse_number(MbedJSONValue& out, input& in) {
-#ifdef DEBUG
+#ifdef DEBUG_MJSON
     printf("number detected\r\n");
 #endif
     std::string num_str;
@@ -572,6 +571,7 @@
         case '[':
             return _parse_array(out, in);
         case '{':
+            out._type = MbedJSONValue::TypeObject; 
             return _parse_object(out, in);
         default:
             if (('0' <= ch && ch <= '9') || ch == '-') {
@@ -593,6 +593,7 @@
 
 inline const char * parse(MbedJSONValue& out, const char * first, const char * last, std::string* err) {
     input in = input(first, last);
+
     if (! _parse(out, in) && err != NULL) {
         char buf[64];
         sprintf(buf, "syntax error at line %d near: ", in.line());