diff --git a/include/rjp.h b/include/rjp.h index 73447b0..9ec3ed4 100644 --- a/include/rjp.h +++ b/include/rjp.h @@ -182,9 +182,11 @@ RJP_data_type rjp_value_type(const RJP_value* value); /***************** OBJECT OPERATIONS *******************/ //add a member to a json object, allocating a copy of the key -RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen); +RJP_value* rjp_new_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen); //add a member to a json object without allocation. key must be allocated using rjp_alloc/rjp_calloc -RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen); +RJP_value* rjp_new_member(RJP_value* dest, char* key, RJP_index keylen); +RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen, RJP_value* src); +RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen, RJP_value* src); //Remove member without freeing RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key); @@ -218,7 +220,8 @@ RJP_value* rjp_object_iterator_peek(const RJP_object_iterator* it); /***************** ARRAY OPERATIONS *******************/ //add an element to a json array -RJP_value* rjp_add_element(RJP_value* dest); +RJP_value* rjp_new_element(RJP_value* dest); +RJP_value* rjp_add_element(RJP_value* dest, RJP_value* src); RJP_value* rjp_remove_element(RJP_value* arr, RJP_value* elem); void rjp_free_element(RJP_value* arr, RJP_value* elem); diff --git a/rjp++/src/array.cpp b/rjp++/src/array.cpp index 0c4f528..5ed1280 100644 --- a/rjp++/src/array.cpp +++ b/rjp++/src/array.cpp @@ -37,7 +37,7 @@ namespace rjp{ rjp_set_array(m_value); } value array::add(void){ - RJP_value* newelem = rjp_add_element(m_value); + RJP_value* newelem = rjp_new_element(m_value); return create_unmanaged(newelem); } diff --git a/rjp++/src/object.cpp b/rjp++/src/object.cpp index 50e41dc..9b99adc 100644 --- a/rjp++/src/object.cpp +++ b/rjp++/src/object.cpp @@ -36,7 +36,7 @@ namespace rjp{ rjp_set_object(m_value); } member object::add(const rexy::string_base& key){ - RJP_value* newmemb = rjp_add_member_key_copy(m_value, key.get(), key.length()); + RJP_value* newmemb = rjp_new_member_key_copy(m_value, key.get(), key.length()); return create_unmanaged(newmemb); } diff --git a/src/rjp.c b/src/rjp.c index 63a0a25..1b16e58 100644 --- a/src/rjp.c +++ b/src/rjp.c @@ -76,6 +76,7 @@ RJP_value* rjp_copy_value(RJP_value* dest, const RJP_value* src){ } RJP_value* rjp_move_value(RJP_value* dest, RJP_value* src){ *dest = *src; + src->type = rjp_json_null; return dest; } @@ -109,7 +110,7 @@ void rjp_free_value(RJP_value* root){ if(!root) return; irjp_delete_value(root); - free(root); + rjp_free(root); } /* VALUE SETTING */ diff --git a/src/rjp_array.c b/src/rjp_array.c index d84ec38..e9f39d0 100644 --- a/src/rjp_array.c +++ b/src/rjp_array.c @@ -27,7 +27,7 @@ void irjp_copy_array(RJP_value* dest, const RJP_value* src){ dest->array.elements = dest->array.last = NULL; for(RJP_array_element* curr = src->array.elements;curr;curr = curr->next){ RJP_value* copy_mem; - copy_mem = rjp_add_element(dest); + copy_mem = rjp_new_element(dest); rjp_copy_value(copy_mem, &curr->value); } } @@ -42,11 +42,19 @@ void irjp_add_element(RJP_array* j){ j->last->prev = j->last; } } -RJP_value* rjp_add_element(RJP_value* dest){ +RJP_value* rjp_new_element(RJP_value* dest){ irjp_add_element(&dest->array); dest->array.last->value.parent = dest; return &dest->array.last->value; } +RJP_value* rjp_add_element(RJP_value* dest, RJP_value* src){ + RJP_value* newelm = rjp_new_element(dest); + if(!newelm) + return NULL; + rjp_move_value(newelm, src); + rjp_free(src); + return newelm; +} RJP_value* rjp_remove_element(RJP_value* dest, RJP_value* value){ if(value->parent != dest) return NULL; diff --git a/src/rjp_object.c b/src/rjp_object.c index 336440e..b12df38 100644 --- a/src/rjp_object.c +++ b/src/rjp_object.c @@ -46,7 +46,7 @@ void irjp_delete_object(RJP_value* obj){ default: break; }; } -RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen){ +RJP_value* rjp_new_member(RJP_value* dest, char* key, RJP_index keylen){ switch(dest->type){ case rjp_json_object: return irjp_add_unordered_member(dest, key, keylen); @@ -56,13 +56,29 @@ RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen){ }; return NULL; } -RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen){ +RJP_value* rjp_new_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen){ if(!keylen) keylen = rjp_escape_strlen(key); char* newkey = rjp_alloc(keylen+1); rjp_escape_strcpy(newkey, key); newkey[keylen] = 0; - return rjp_add_member(dest, newkey, keylen); + return rjp_new_member(dest, newkey, keylen); +} +RJP_value* rjp_add_member(RJP_value* dest, char* key, RJP_index keylen, RJP_value* src){ + RJP_value* newval = rjp_new_member(dest, key, keylen); + if(!newval) + return NULL; + rjp_move_value(newval, src); + rjp_free(src); + return newval; +} +RJP_value* rjp_add_member_key_copy(RJP_value* dest, const char* key, RJP_index keylen, RJP_value* src){ + RJP_value* newval = rjp_new_member_key_copy(dest, key, keylen); + if(!newval) + return NULL; + rjp_move_value(newval, src); + rjp_free(src); + return newval; } RJP_value* rjp_remove_member_by_key(RJP_value* obj, const char* key){ RJP_value* member = rjp_search_member(obj, key); diff --git a/src/rjp_parse.c b/src/rjp_parse.c index 54aaa7c..b8bf481 100644 --- a/src/rjp_parse.c +++ b/src/rjp_parse.c @@ -142,7 +142,7 @@ static int irjp_init_value(RJP_value* newval, RJP_lex_category cat, RJP_parse_st return 0; } static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state* state){ - state->lastadded = rjp_add_element(state->curr); + state->lastadded = rjp_new_element(state->curr); if(irjp_init_value(state->lastadded, cat, state)) return NULL; return state->lastadded; @@ -150,7 +150,7 @@ static RJP_value* irjp_add_value_to_array(RJP_lex_category cat, RJP_parse_state* static RJP_value* irjp_add_value_to_object(RJP_parse_state* state, const char* key, RJP_index keylen){ RJP_index newlen; char* newkey = irjp_convert_string(key, keylen, &newlen); - return (state->lastadded = rjp_add_member(state->curr, newkey, newlen)); + return (state->lastadded = rjp_new_member(state->curr, newkey, newlen)); }