diff --git a/sane/gb_json.cpp b/sane/gb_json.cpp index 5fc5c90..264c511 100644 --- a/sane/gb_json.cpp +++ b/sane/gb_json.cpp @@ -96,11 +96,18 @@ namespace gb node->valuestring = NULL; node->child = NULL; } - cJSON* json::create_element_with_name(const char* name) + cJSON* json::create_element(bool is_array) { - cJSON* obj = cJSON_CreateObject(); + cJSON* obj = is_array ? cJSON_CreateArray() : cJSON_CreateObject(); bzero(obj, sizeof(*obj)); + + return obj; + } + cJSON* json::create_element_with_name(const char* name) + { + cJSON* obj = json::create_element(); + if(name) { obj->string = (char*)malloc(strlen(name) + 4); @@ -153,8 +160,13 @@ namespace gb return now; now = json::create_element_with_name(path[0].c_str()); - if(prev) - *prev = now; + if (prev) + { + cJSON* pr = (cJSON*)((DWORD_PTR)prev - (DWORD_PTR)&((cJSON*)0)->next); + pr->next = now; + now->prev = pr; + // *prev = now; + } else { obj_->child = now; @@ -185,7 +197,7 @@ namespace gb if(!create) return NULL; - obj_ = cJSON_CreateObject(); + obj_ = json::create_element(); obj_->child = json::create_element_with_name(tree[0].c_str()); } @@ -221,7 +233,7 @@ namespace gb { clear(); - obj_ = array ? cJSON_CreateArray() : cJSON_CreateObject(); + obj_ = json::create_element(array); is_array_ = array; return true; @@ -406,7 +418,7 @@ namespace gb if(is_array_) { if(!obj_) - obj_ = cJSON_CreateArray(); + obj_ = json::create_element(true); cJSON_AddItemToArray(obj_, val ? cJSON_CreateTrue() : cJSON_CreateFalse()); } @@ -434,7 +446,7 @@ namespace gb if(is_array_) { if(!obj_) - obj_ = cJSON_CreateArray(); + obj_ = json::create_element(true); cJSON_AddItemToArray(obj_, cJSON_CreateNumber(val)); } @@ -460,7 +472,7 @@ namespace gb if(is_array_) { if(!obj_) - obj_ = cJSON_CreateArray(); + obj_ = json::create_element(true); cJSON_AddItemToArray(obj_, cJSON_CreateNumber(val)); } @@ -486,7 +498,7 @@ namespace gb if(is_array_) { if(!obj_) - obj_ = cJSON_CreateArray(); + obj_ = json::create_element(true); cJSON_AddItemToArray(obj_, cJSON_CreateString(val.c_str())); } @@ -518,7 +530,7 @@ namespace gb if(is_array_) { if(!obj_) - obj_ = cJSON_CreateArray(); + obj_ = json::create_element(true); if(obj && obj->obj_) { cJSON_AddItemToArray(obj_, obj->obj_); diff --git a/sane/gb_json.h b/sane/gb_json.h index 8eea348..c886364 100644 --- a/sane/gb_json.h +++ b/sane/gb_json.h @@ -29,6 +29,7 @@ namespace gb static std::string to_string(cJSON* root, bool formatted); static std::string get_value_as_string(cJSON* root, bool integer = false); static void free_node_data(cJSON* node); + static cJSON* create_element(bool is_array = false); static cJSON* create_element_with_name(const char* name); public: