json_t: debug some leak memories
This commit is contained in:
+221
-110
@@ -46,10 +46,7 @@ struct js_value * prev_(struct js_value *js){
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void free_js_value(struct js_value *js){
|
||||
|
||||
void free_js_value__(struct js_value *js){
|
||||
if(js->code_type == jstype_string){
|
||||
free(js->type.string);
|
||||
}else if(js->code_type == jstype_object){
|
||||
@@ -62,7 +59,7 @@ void free_js_value(struct js_value *js){
|
||||
ttmp = tmpV;
|
||||
tmpV = ttmp->type.object.next_object ; //next_(ttmp);
|
||||
ttmp->type.object.next_object = NULL;
|
||||
//print_value(ttmp);
|
||||
//print_js_value(ttmp);
|
||||
free_js_value(ttmp);
|
||||
}
|
||||
if(js->type.object.index == 0 && js->type.object.iter){
|
||||
@@ -78,7 +75,7 @@ void free_js_value(struct js_value *js){
|
||||
ttmp = tmpV;
|
||||
tmpV = ttmp->type.array.next_element ; //next_(ttmp);
|
||||
ttmp->type.array.next_element = NULL;
|
||||
//print_value(ttmp);
|
||||
//print_js_value(ttmp);
|
||||
free_js_value(ttmp);
|
||||
}
|
||||
if(js->type.array.index == 0 && js->type.array.iter){
|
||||
@@ -92,9 +89,92 @@ void free_js_value(struct js_value *js){
|
||||
|
||||
}
|
||||
|
||||
void _free_js_value_no_free_iter(struct js_value *js){
|
||||
if(js!=NULL){
|
||||
|
||||
if(js->code_type == jstype_number){
|
||||
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_string){
|
||||
free(js->type.string);
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_bool){
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_null){
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_object){
|
||||
struct js_value *tmpjs = NULL;
|
||||
|
||||
while(js){
|
||||
tmpjs=js;
|
||||
|
||||
_free_js_value_no_free_iter(tmpjs->type.object.value);
|
||||
|
||||
js = tmpjs->type.object.next_object;
|
||||
free(tmpjs);
|
||||
}
|
||||
}else if(js->code_type == jstype_array){
|
||||
struct js_value *tmpjs = NULL;
|
||||
while(js){
|
||||
tmpjs = js;
|
||||
_free_js_value_no_free_iter(tmpjs->type.array.value);
|
||||
|
||||
js = tmpjs->type.array.next_element; // next_(tmpjs);
|
||||
free(tmpjs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void free_js_value(struct js_value *js){
|
||||
if(js!=NULL){
|
||||
|
||||
if(js->code_type == jstype_number){
|
||||
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_string){
|
||||
free(js->type.string);
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_bool){
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_null){
|
||||
free(js);
|
||||
}else if(js->code_type == jstype_object){
|
||||
struct js_value *tmpjs = NULL;
|
||||
|
||||
while(js){
|
||||
tmpjs=js;
|
||||
if((tmpjs->type.object.iter != NULL) && (tmpjs->type.object.prev_object == NULL)){
|
||||
free(tmpjs->type.object.iter);
|
||||
}
|
||||
|
||||
free_js_value(tmpjs->type.object.value);
|
||||
|
||||
js = tmpjs->type.object.next_object;
|
||||
free(tmpjs);
|
||||
}
|
||||
}else if(js->code_type == jstype_array){
|
||||
struct js_value *tmpjs = NULL;
|
||||
while(js){
|
||||
tmpjs = js;
|
||||
if((tmpjs->type.array.iter!=NULL) && (tmpjs->type.array.prev_element == NULL)){
|
||||
free(tmpjs->type.array.iter);
|
||||
}
|
||||
free_js_value(tmpjs->type.array.value);
|
||||
|
||||
js = tmpjs->type.array.next_element; // next_(tmpjs);
|
||||
free(tmpjs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct js_value * create_js_value_string(char * input, struct js_value * parent){
|
||||
struct js_value * js = malloc(sizeof(struct js_value));
|
||||
js->parent = parent;
|
||||
|
||||
if(*input == '"')
|
||||
js->str_value = input+1;
|
||||
else
|
||||
@@ -270,7 +350,8 @@ return js;
|
||||
|
||||
js->type.array.value = create_js_value(str_value, js);
|
||||
|
||||
cur = str_value + js->type.array.value->length + (js->type.array.value->code_type != jstype_number) ; //1 ;
|
||||
//cur = str_value + js->type.array.value->length + (js->type.array.value->code_type != jstype_number) ; //1 ;
|
||||
cur = str_value + js->type.array.value->length + (js->type.array.value->code_type == jstype_string) ; //1 ;
|
||||
// printf("<a>>strVal:%s\n",cur);
|
||||
|
||||
if(js->type.array.value->code_type == jstype_string)
|
||||
@@ -289,7 +370,7 @@ struct js_value * create_js_value(char *_input, struct js_value *parent){
|
||||
char *input = _input;
|
||||
for(; is_js_space(*input); ++input);
|
||||
|
||||
// printf(">>strVal:%s\n",input);
|
||||
//printf("\n>>strVal:%s\n",input);
|
||||
if(*input == '"'){
|
||||
return create_js_value_string(input+1, parent);
|
||||
}else if(strncmp(input,"true",4) == 0){
|
||||
@@ -325,14 +406,12 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
|
||||
}
|
||||
js->length = cur - input;
|
||||
|
||||
// printf("iin:%s\noo cur:%s\nlenObj:%ld\n",input,cur, js->length);
|
||||
|
||||
js->type.object.next_object = NULL;
|
||||
js->type.object.prev_object = NULL;
|
||||
js->type.object.index = 0;
|
||||
|
||||
js->type.object.iter = malloc(sizeof(struct js_iterator));
|
||||
|
||||
js->type.object.iter->size = js->type.object.index + 1;
|
||||
js->type.object.iter->begin = js;
|
||||
js->type.object.iter->end = js;
|
||||
@@ -348,9 +427,7 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
|
||||
|
||||
cur = input;
|
||||
for(; is_js_space(*cur); ++cur);
|
||||
// printf("oocp::%s\n",cur);
|
||||
if(*cur == '}'){
|
||||
// printf(">>>iin:%s\noo cur:%s\nlenObj:%ld\n",input,cur, js->length);
|
||||
|
||||
js->type.object.value = create_js_value_null(NULL, js);// NULL;
|
||||
js->type.object.value->length = cur - input;
|
||||
@@ -362,11 +439,9 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
|
||||
}
|
||||
|
||||
|
||||
// printf("cooc::%s, diff:%ld\n",cur, input + js_parent->length - cur);
|
||||
|
||||
while(cur < input + js_parent->length)
|
||||
{
|
||||
// printf("cc::%c\n",*cur);
|
||||
if(*cur == ','){
|
||||
++cur;
|
||||
tmp_js = malloc(sizeof(struct js_value));
|
||||
@@ -424,7 +499,6 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
|
||||
|
||||
if(*cur == '"') ++cur;
|
||||
js->type.object.key = cur;
|
||||
// printf("cur:%s\n",cur);
|
||||
for(; *cur != '"'; ++cur);
|
||||
//js->key = cur;
|
||||
//for(; *cur != '"'; ++cur);
|
||||
@@ -440,9 +514,10 @@ struct js_value * create_js_value_object(char *_input /*, struct js_value *prev*
|
||||
char *str_value = cur; // + js->type.object.key->length;
|
||||
|
||||
js->type.object.value = create_js_value(str_value, js);
|
||||
//printf("\n[<<<: cur:%s\n(js->type.object.value->code_type != jstype_number)=%d\n",cur,(js->type.object.value->code_type != jstype_number));
|
||||
|
||||
cur = str_value + js->type.object.value->length + (js->type.object.value->code_type != jstype_number);// 1 ;
|
||||
// printf("[>>>: cur:%s\n",cur);
|
||||
cur = str_value + js->type.object.value->length + (js->type.object.value->code_type == jstype_string); // ((js->type.object.value->code_type < jstype_bool)||(js->type.object.value->code_type > jstype_number));// 1 ;
|
||||
//printf("\n[>>>: cur:%s\n",cur);
|
||||
|
||||
if(js->type.object.value->code_type == jstype_string)
|
||||
for(; is_js_space(*cur) || *cur=='"' || *cur=='}' || *cur == ']'; ++cur);
|
||||
@@ -610,88 +685,67 @@ struct js_value * get_js_value_of_index_(size_t index, struct js_value *js){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void set_next_(struct js_value * dstjs, struct js_value * jsnxt){
|
||||
if(dstjs->code_type == jstype_object){
|
||||
struct js_value *tmpjs = dstjs->type.object.next_object;//, *ttmpjs;
|
||||
//ttmpjs = dstjs;
|
||||
//while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs);
|
||||
//struct js_iterator *iter = ttmpjs->type.object.iter;
|
||||
struct js_value *tmpjs = dstjs->type.object.next_object;
|
||||
struct js_iterator *iter = dstjs->type.object.iter;
|
||||
dstjs->type.object.next_object = jsnxt;
|
||||
jsnxt->type.object.prev_object = dstjs;
|
||||
size_t last_index = dstjs->type.object.index;
|
||||
struct js_value *ttmp = jsnxt;
|
||||
//struct js_iterator *olditer = jsnxt->type.object.iter;
|
||||
//bool deferFree = (jsnxt->type.object.index == 0);
|
||||
struct js_value *end = jsnxt->type.object.iter->end;
|
||||
while(next_(ttmp)){
|
||||
if(get_index_(ttmp)==0) free(ttmp->type.object.iter);
|
||||
ttmp->type.object.iter = iter;
|
||||
//end = ttmp;
|
||||
ttmp->type.object.index = (++last_index);
|
||||
ttmp = next_(ttmp);
|
||||
|
||||
if(jsnxt->code_type == jstype_object){
|
||||
dstjs->type.object.next_object = jsnxt;
|
||||
struct js_iterator *it_er = jsnxt->type.object.iter;
|
||||
jsnxt->type.object.prev_object = dstjs;
|
||||
while(jsnxt){
|
||||
jsnxt->type.object.iter = iter;
|
||||
jsnxt->type.object.index = ++last_index;
|
||||
jsnxt = jsnxt->type.object.next_object;
|
||||
}
|
||||
ttmp->type.object.next_object = tmpjs;
|
||||
if(tmpjs){
|
||||
// print_value(tmpjs);
|
||||
// printf("%d line\n",__LINE__);
|
||||
tmpjs->type.object.prev_object = ttmp;
|
||||
if(tmpjs == NULL && it_er){
|
||||
iter->end = it_er->end;
|
||||
}else{
|
||||
while(tmpjs){
|
||||
//end = tmpjs;
|
||||
tmpjs->type.object.index = (++last_index);
|
||||
tmpjs = next_(tmpjs);
|
||||
tmpjs->type.object.index = ++last_index;
|
||||
tmpjs = tmpjs->type.object.next_object;
|
||||
}
|
||||
}
|
||||
//struct js_iterator *iter = get_iterator_(dstjs);
|
||||
if(iter){
|
||||
iter->size = last_index + 1;
|
||||
if(get_index_(iter->end) < get_index_(end)){
|
||||
iter->end = end;
|
||||
if(it_er){
|
||||
free(it_er);
|
||||
}
|
||||
}
|
||||
//if(deferFree && olditer) free(olditer);
|
||||
}else if(dstjs->code_type == jstype_array){
|
||||
struct js_value *tmpjs = dstjs->type.array.next_element;//, *ttmpjs;
|
||||
//ttmpjs = dstjs;
|
||||
//while(prev_(ttmpjs)) ttmpjs = prev_(ttmpjs);
|
||||
//struct js_iterator *iter = ttmpjs->type.array.iter;
|
||||
}
|
||||
else if(dstjs->code_type == jstype_array){
|
||||
struct js_value *tmpjs = dstjs->type.array.next_element;
|
||||
struct js_iterator *iter = dstjs->type.array.iter;
|
||||
//if(jsnxt->type.array.iter) free(jsnxt->type.array.iter);
|
||||
dstjs->type.array.next_element = jsnxt;
|
||||
jsnxt->type.array.prev_element = dstjs;
|
||||
size_t last_index = dstjs->type.array.index;
|
||||
struct js_value *ttmp = jsnxt;
|
||||
struct js_value *end = jsnxt->type.array.iter->end;
|
||||
//struct js_iterator *olditer = jsnxt->type.array.iter;
|
||||
//bool deferFree = (jsnxt->type.array.index == 0);
|
||||
while(next_(ttmp)){
|
||||
if(get_index_(ttmp)==0) free(ttmp->type.object.iter);
|
||||
ttmp->type.array.iter = iter;
|
||||
//end = ttmp;
|
||||
ttmp->type.array.index = (++last_index);
|
||||
ttmp = next_(ttmp);
|
||||
|
||||
if(jsnxt->code_type == jstype_array){
|
||||
dstjs->type.array.next_element = jsnxt;
|
||||
struct js_iterator *it_er = jsnxt->type.array.iter;
|
||||
jsnxt->type.array.prev_element= dstjs;
|
||||
while(jsnxt){
|
||||
jsnxt->type.array.iter = iter;
|
||||
jsnxt->type.array.index = ++last_index;
|
||||
jsnxt = jsnxt->type.array.next_element;
|
||||
}
|
||||
ttmp->type.array.next_element = tmpjs;
|
||||
if(tmpjs){
|
||||
tmpjs->type.array.prev_element = ttmp;
|
||||
if(tmpjs == NULL && it_er){
|
||||
iter->end = it_er->end;
|
||||
}else{
|
||||
while(tmpjs){
|
||||
//end = tmpjs;
|
||||
tmpjs->type.array.index = (++last_index);
|
||||
tmpjs = next_(tmpjs);
|
||||
tmpjs->type.array.index = ++last_index;
|
||||
tmpjs = tmpjs->type.array.next_element;
|
||||
}
|
||||
}
|
||||
//struct js_iterator *iter = get_iterator_(dstjs);
|
||||
if(iter){
|
||||
iter->size = last_index + 1;
|
||||
if(get_index_(iter->end) < get_index_(end)){
|
||||
iter->end = end;
|
||||
if(it_er){
|
||||
free(it_er);
|
||||
}
|
||||
}
|
||||
//if(deferFree && olditer) free(olditer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void set_prev_(struct js_value ** _dst, struct js_value * jsprv){
|
||||
struct js_value *dst = *_dst;
|
||||
if(dst->code_type == jstype_object){
|
||||
@@ -801,10 +855,11 @@ void add_js_value_index(size_t index, struct js_value *js_to_add, struct js_valu
|
||||
}
|
||||
|
||||
void delete_index_js_value(size_t index, struct js_value **js_org){
|
||||
if((*js_org)->code_type >= jstype_object){
|
||||
if((*js_org) && ( (*js_org)->code_type >= jstype_object) ){
|
||||
struct js_value *ttmp=NULL;
|
||||
ttmp = get_js_value_of_index_(index, *js_org);
|
||||
if((*js_org)->code_type == jstype_object){
|
||||
|
||||
if(ttmp && ((*js_org)->code_type == jstype_object)){
|
||||
struct js_iterator *iter = (*js_org)->type.object.iter;
|
||||
struct js_value *prevJS = ttmp->type.object.prev_object;
|
||||
struct js_value *nextJS = ttmp->type.object.next_object;
|
||||
@@ -821,13 +876,13 @@ void delete_index_js_value(size_t index, struct js_value **js_org){
|
||||
}
|
||||
if(nextJS){
|
||||
nextJS->type.object.prev_object = prevJS;
|
||||
ttmp->type.object.next_object = NULL;
|
||||
}else{
|
||||
iter->end == prevJS;
|
||||
}
|
||||
|
||||
if(ttmp == *js_org){
|
||||
*js_org = nextJS;
|
||||
iter->begin = nextJS;
|
||||
}
|
||||
while(nextJS){
|
||||
--(nextJS->type.object.index);
|
||||
@@ -835,6 +890,13 @@ void delete_index_js_value(size_t index, struct js_value **js_org){
|
||||
}
|
||||
--(iter->size);
|
||||
|
||||
ttmp->type.object.next_object = NULL;
|
||||
ttmp->type.object.prev_object = NULL;
|
||||
|
||||
if(*js_org){
|
||||
ttmp->type.object.iter = NULL;
|
||||
}
|
||||
|
||||
free_js_value(ttmp);
|
||||
|
||||
}else{
|
||||
@@ -854,13 +916,13 @@ void delete_index_js_value(size_t index, struct js_value **js_org){
|
||||
}
|
||||
if(nextJS){
|
||||
nextJS->type.array.prev_element = prevJS;
|
||||
ttmp->type.array.next_element = NULL;
|
||||
}else{
|
||||
iter->end == prevJS;
|
||||
}
|
||||
|
||||
if(ttmp == *js_org){
|
||||
*js_org = nextJS;
|
||||
iter->begin = nextJS;
|
||||
}
|
||||
while(nextJS){
|
||||
--(nextJS->type.array.index);
|
||||
@@ -868,6 +930,12 @@ void delete_index_js_value(size_t index, struct js_value **js_org){
|
||||
}
|
||||
--(iter->size);
|
||||
|
||||
ttmp->type.array.next_element = NULL;
|
||||
ttmp->type.array.prev_element = NULL;
|
||||
|
||||
if(*js_org)
|
||||
ttmp->type.array.iter = NULL;
|
||||
|
||||
free_js_value(ttmp);
|
||||
|
||||
}
|
||||
@@ -879,41 +947,26 @@ struct js_iterator * get_iterator_(struct js_value *js){
|
||||
if(js->code_type == jstype_array) return js->type.array.iter;
|
||||
return NULL;
|
||||
}
|
||||
void set_iterator_(struct js_value *js, struct js_iterator *iter){
|
||||
|
||||
|
||||
void append_js_value(struct js_value *dst, char *input){/* if object type: input = { "key" : value }, if key already exists value replace the old value! */
|
||||
if(dst->code_type < jstype_object || get_iterator_(dst) == NULL) return;
|
||||
struct js_value *newval = create_js_value(input, dst);
|
||||
if(dst->code_type == jstype_object && newval->code_type != jstype_object){
|
||||
printf(" input needs to be at the format : { \"key\" : value } \n");
|
||||
free_js_value(newval);
|
||||
return;
|
||||
}else{
|
||||
if(dst->code_type == jstype_object){
|
||||
char key[newval->type.object.key_length + 1];
|
||||
strncpy(key, newval->type.object.key, newval->type.object.key_length);
|
||||
key[newval->type.object.key_length]='\0';
|
||||
struct js_value * valjs = get_js_value_of_key(key, dst);
|
||||
if(valjs){
|
||||
struct js_value *ttmp = value_of_(valjs);
|
||||
valjs->type.object.value = value_of_(newval);
|
||||
free_js_value(ttmp);
|
||||
if(newval->type.object.iter) free(newval->type.object.iter);
|
||||
free(newval); // but keep value_of_(newval)!
|
||||
return;
|
||||
if(js->code_type == jstype_object) js->type.object.iter = iter;
|
||||
if(js->code_type == jstype_array) js->type.array.iter = iter;
|
||||
}
|
||||
|
||||
void append_js_value(struct js_value *dst, char *input){/* if object type: input = { "key" : value }, if key already exists value replace the old value! */
|
||||
if(dst){
|
||||
if(dst->code_type < jstype_object || get_iterator_(dst) == NULL) return;
|
||||
struct js_value *newval = create_js_value(input, dst->parent);
|
||||
if(newval->code_type != dst->code_type) {
|
||||
free_js_value(newval);
|
||||
return;
|
||||
}
|
||||
struct js_iterator *iter = get_iterator_(dst);
|
||||
set_next_(iter->end, newval);
|
||||
struct js_value *tmpjs = newval;
|
||||
while(next_(tmpjs)) tmpjs = next_(tmpjs);
|
||||
iter->end = tmpjs;
|
||||
iter->current = tmpjs;
|
||||
iter->size = get_index_(tmpjs) + 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char * original_string_js_value(struct js_value *js){
|
||||
char *ret = malloc(js->length+1);
|
||||
@@ -927,7 +980,7 @@ char * original_string_js_value(struct js_value *js){
|
||||
|
||||
}
|
||||
|
||||
void print_value(struct js_value *js){
|
||||
void __print_js_value(struct js_value *js){
|
||||
if(js->code_type == jstype_number){
|
||||
printf("%Lf",js->type.number);
|
||||
}else if(js->code_type == jstype_string){
|
||||
@@ -949,7 +1002,7 @@ void print_value(struct js_value *js){
|
||||
strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length);
|
||||
key[tmpjs->type.object.key_length]='\0';
|
||||
printf("\n%*c\"%s\" : ", 4*(tmpjs->type.object.depth),' ', key );
|
||||
print_value(tmpjs->type.object.value);
|
||||
print_js_value(tmpjs->type.object.value);
|
||||
|
||||
if(tmpjs->type.object.next_object == NULL)
|
||||
printf("\n%*c} ",4*(tmpjs->type.object.depth),' '); //printf(" }");
|
||||
@@ -965,7 +1018,7 @@ void print_value(struct js_value *js){
|
||||
}
|
||||
if(tmpjs->type.array.value->code_type < jstype_object )
|
||||
printf("\n%*c ",4*(tmpjs->type.array.depth),' ');
|
||||
print_value(tmpjs->type.array.value);
|
||||
print_js_value(tmpjs->type.array.value);
|
||||
if(tmpjs->type.array.next_element == NULL) //printf(" ]");
|
||||
printf("\n%*c] ",4*(tmpjs->type.array.depth),' ');
|
||||
else printf(", ");
|
||||
@@ -975,6 +1028,59 @@ void print_value(struct js_value *js){
|
||||
|
||||
}
|
||||
|
||||
|
||||
void print_js_value(struct js_value *js){
|
||||
if(js->code_type == jstype_number){
|
||||
printf("%Lf",js->type.number);
|
||||
}else if(js->code_type == jstype_string){
|
||||
printf("\"%s\"",js->type.string);
|
||||
}else if(js->code_type == jstype_bool){
|
||||
printf("%s",js->type.boolean ? "true" : "false");
|
||||
}else if(js->code_type == jstype_null){
|
||||
printf("%s","null");
|
||||
}else if(js->code_type == jstype_object){
|
||||
struct js_value *tmpjs = js;
|
||||
|
||||
while(tmpjs){
|
||||
|
||||
if(tmpjs->type.object.prev_object == NULL){
|
||||
printf("\n%*c{ ",4*(tmpjs->type.object.depth),' ');
|
||||
}
|
||||
|
||||
char key[tmpjs->type.object.key_length + 1];
|
||||
strncpy(key, tmpjs->type.object.key, tmpjs->type.object.key_length);
|
||||
key[tmpjs->type.object.key_length]='\0';
|
||||
printf("\n%*c\"%s\" : ", 4*(tmpjs->type.object.depth),' ', key );
|
||||
print_js_value(tmpjs->type.object.value);
|
||||
|
||||
if(tmpjs->type.object.next_object == NULL)
|
||||
printf("\n%*c} ",4*(tmpjs->type.object.depth),' '); //printf(" }");
|
||||
else printf(", ");
|
||||
//printf("\n");
|
||||
tmpjs = tmpjs->type.object.next_object;
|
||||
}
|
||||
}else if(js->code_type == jstype_array){
|
||||
struct js_value *tmpjs = js;
|
||||
while(tmpjs){
|
||||
if(tmpjs->type.array.prev_element == NULL){
|
||||
printf("\n%*c[ ",4*(tmpjs->type.array.depth),' ');
|
||||
}
|
||||
if(tmpjs->type.array.value->code_type < jstype_object )
|
||||
printf("\n%*c ",4*(tmpjs->type.array.depth),' ');
|
||||
print_js_value(tmpjs->type.array.value);
|
||||
if(tmpjs->type.array.next_element == NULL) //printf(" ]");
|
||||
printf("\n%*c] ",4*(tmpjs->type.array.depth),' ');
|
||||
else printf(", ");
|
||||
tmpjs = tmpjs->type.array.next_element; // next_(tmpjs);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void print_js_value(struct js_value *js){
|
||||
|
||||
struct js_value * value_of_(struct js_value * js){
|
||||
if(js->code_type == jstype_object){
|
||||
return js->type.object.value;
|
||||
@@ -1005,6 +1111,11 @@ void delete_key_js_value(char * key, struct js_value **js_org){
|
||||
}
|
||||
}
|
||||
|
||||
size_t js_org_str_length(struct js_value *js){
|
||||
// if string add 2 \" values
|
||||
return js->length+1+2*(js->code_type == 0);
|
||||
}
|
||||
|
||||
/*
|
||||
void set_value_of_key(char *key, char * value, struct js_value *js ){
|
||||
if(js->code_type != jstype_object)
|
||||
|
||||
@@ -87,7 +87,7 @@ struct js_value {
|
||||
struct js_object object;
|
||||
struct js_array array;
|
||||
} type;
|
||||
struct js_value * parent;
|
||||
struct js_value * parent; /* inverse value of value of object or array */
|
||||
};
|
||||
|
||||
|
||||
@@ -110,23 +110,26 @@ struct js_value * create_js_value_false(char * input, struct js_value * parent);
|
||||
struct js_value * create_js_value_object(char *input, struct js_value * parent);
|
||||
struct js_value * create_js_value_array(char * input, struct js_value * parent);
|
||||
|
||||
struct js_iterator * create_js_iterator(struct js_value * js);
|
||||
//struct js_iterator * create_js_iterator(struct js_value * js);
|
||||
|
||||
void add_js_value_index(size_t index, struct js_value *js_to_add, struct js_value **js_org);
|
||||
void delete_key_js_value(char * key, struct js_value **js_org);
|
||||
|
||||
void print_value(struct js_value *js);
|
||||
void print_js_value(struct js_value *js);
|
||||
//char* sprint_js_value(struct js_value *js);
|
||||
|
||||
char * original_string_js_value(struct js_value *js);
|
||||
|
||||
struct js_value *get_js_value_of_key(char * key, struct js_value *js );
|
||||
struct js_iterator * get_iterator_(struct js_value *js);
|
||||
void set_iterator_(struct js_value *js, struct js_iterator *iter);
|
||||
|
||||
void append_js_value(struct js_value *dst, char *input);
|
||||
void delete_index_js_value(size_t index, struct js_value **js_org);
|
||||
|
||||
struct js_value * value_of_(struct js_value * js);
|
||||
|
||||
size_t js_org_str_length(struct js_value *js);
|
||||
|
||||
#endif /* __Y_JSON_T_H__C__ */
|
||||
|
||||
|
||||
+195
-19
@@ -41,7 +41,7 @@ TEST(object_val){
|
||||
LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string);
|
||||
*/
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
char *rrjson = original_string_js_value(js);
|
||||
printf("original :%s\n",rrjson);
|
||||
@@ -69,7 +69,7 @@ TEST(array_val){
|
||||
LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string);
|
||||
*/
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
/*
|
||||
char *rrjson = original_string_js_value(js);
|
||||
@@ -99,7 +99,7 @@ TEST(general_val){
|
||||
LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string);
|
||||
*/
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
/*
|
||||
char *rrjson = original_string_js_value(js);
|
||||
@@ -130,7 +130,7 @@ TEST(general_val2){
|
||||
LOG("value : %s\n",js->type.object.next_object->type.object.next_object->type.object.value->type.string);
|
||||
*/
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
/*
|
||||
char *rrjson = original_string_js_value(js);
|
||||
@@ -160,9 +160,9 @@ TEST(add_val){
|
||||
struct js_value *js_add = create_js_value(add, NULL);
|
||||
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
add_js_value_index(10, js_add, &js);
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
|
||||
@@ -178,36 +178,38 @@ TEST(append_val){
|
||||
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
//char *add ="{ \"add0\" : 0.0 }";
|
||||
char *add ="{ \"add0\" : 0.0 }";
|
||||
|
||||
// struct js_value *js_add = create_js_value(add, NULL);
|
||||
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
// add_js_value_index(10, js_add, &js);
|
||||
append_js_value(js,add);
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
char *add_arr ="{ \"array\" : [ 0.0 ] }";
|
||||
append_js_value(js,add_arr);
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
LOG("\nline %d\n",__LINE__);
|
||||
|
||||
|
||||
}
|
||||
TEST(delete_index){
|
||||
|
||||
char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }";
|
||||
//char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }";
|
||||
char *val = "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false } , \"nul\" : { } }";
|
||||
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
delete_index_js_value(2, &js);
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
|
||||
free_js_value(js);
|
||||
@@ -222,10 +224,10 @@ TEST(delete_key){
|
||||
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
delete_key_js_value("second", &js);
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
|
||||
|
||||
@@ -236,7 +238,8 @@ TEST(delete_key){
|
||||
}
|
||||
TEST(delete_key_format_in){
|
||||
|
||||
char *val =" { "
|
||||
char *val =
|
||||
" { "
|
||||
" \"array\" : "
|
||||
" [ "
|
||||
" \"message\", "
|
||||
@@ -295,10 +298,11 @@ TEST(delete_key_format_in){
|
||||
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
print_value(js);
|
||||
delete_key_js_value("second", &js);
|
||||
print_js_value(js);
|
||||
delete_key_js_value("person", &js);
|
||||
//delete_key_js_value("nul", &js);
|
||||
|
||||
print_value(js);
|
||||
print_js_value(js);
|
||||
|
||||
|
||||
|
||||
@@ -307,6 +311,178 @@ TEST(delete_key_format_in){
|
||||
|
||||
|
||||
}
|
||||
TEST(delete_key_format_in2){
|
||||
char *val= " { \"person\": \"Hee\" , \"second\" : 3.22 } ";
|
||||
#if 0
|
||||
" {"
|
||||
" \"array\" :"
|
||||
" ["
|
||||
" \"message\","
|
||||
" \"Hello! it's a string\","
|
||||
" ["
|
||||
" {"
|
||||
" \"\" : null"
|
||||
" } ,"
|
||||
" {"
|
||||
" \"\" : null"
|
||||
" } ,"
|
||||
" ["
|
||||
" ["
|
||||
" null"
|
||||
" ]"
|
||||
" ]"
|
||||
" ] ,"
|
||||
" {"
|
||||
" \"age\" : 48.000000,"
|
||||
" \"speaker\" : \" the president \""
|
||||
" } ,"
|
||||
" 45.000000,"
|
||||
" true"
|
||||
" ] ,"
|
||||
" \"second\" : 324.100000,"
|
||||
" \"person\" :"
|
||||
" {"
|
||||
" \"name\" : \"Fana\","
|
||||
" \"age\" : 37.000000,"
|
||||
" \"genre\" : \"male\","
|
||||
" \"married\" : false,"
|
||||
" \"note\" :"
|
||||
" ["
|
||||
" 2.100000,"
|
||||
" 4.450000,"
|
||||
" 4.000000,"
|
||||
" 10.000000,"
|
||||
" {"
|
||||
" \"math\" : 17.100000,"
|
||||
" \"malagasy\" : 12.000000,"
|
||||
" \"eps\" :"
|
||||
" ["
|
||||
" 5.000000,"
|
||||
" 6.000000,"
|
||||
" 4.000000"
|
||||
" ]"
|
||||
" }"
|
||||
" ]"
|
||||
" } ,"
|
||||
" \"nul\" :"
|
||||
" {"
|
||||
" \"\" : null"
|
||||
" }"
|
||||
" } ";
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
"{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }";
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
char *val =" { "
|
||||
" \"person\" : "
|
||||
" { "
|
||||
" \"name\" : \"Fana\", "
|
||||
" \"age\" : 37.000000, "
|
||||
" \"genre\" : \"male\", "
|
||||
" \"married\" : false, "
|
||||
" \"note\" : "
|
||||
" [ "
|
||||
" 2.100000, "
|
||||
" 4.450000, "
|
||||
" 4.000000, "
|
||||
" 10.000000, "
|
||||
" { "
|
||||
" \"math\" : 17.100000, "
|
||||
" \"malagasy\" : 12.000000, "
|
||||
" \"eps\" : "
|
||||
" [ "
|
||||
" 5.000000, "
|
||||
" 6.000000, "
|
||||
" 4.000000"
|
||||
" ] "
|
||||
" } "
|
||||
" ] "
|
||||
" } , "
|
||||
" \"nul\" : "
|
||||
" { "
|
||||
" \"\" : null"
|
||||
" } "
|
||||
" }";
|
||||
#endif
|
||||
|
||||
// "{ \"array\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } }";
|
||||
|
||||
struct js_value *tmp, *js = create_js_value(val, NULL);
|
||||
|
||||
print_js_value(js);
|
||||
tmp = js;
|
||||
delete_key_js_value("person", &js);
|
||||
|
||||
EXPECT_EQ(0, (js == tmp));
|
||||
|
||||
print_js_value(js);
|
||||
|
||||
|
||||
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
|
||||
|
||||
}
|
||||
TEST(general_val_with_extra_value){
|
||||
|
||||
char *val = "{ \"header\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } } here extra string";
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
|
||||
print_js_value(js);
|
||||
LOG("\ndebug: strlen(val)=%ld\n",strlen(val));
|
||||
LOG("debug: js->length=%ld\n",js->length);
|
||||
|
||||
LOG("extra value : %s\n",val+js_org_str_length(js));
|
||||
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
|
||||
}
|
||||
TEST(string_general_val_with_extra_value){
|
||||
|
||||
char *val = " \"header\" : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } here extra string";
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
|
||||
print_js_value(js);
|
||||
LOG("\ndebug: strlen(val)=%ld\n",strlen(val));
|
||||
LOG("debug: js->=str_value(%s)\n",js->str_value);
|
||||
LOG("debug: js->length=%ld\n",js->length);
|
||||
LOG("debug: js->code_type=%d\n",js->code_type);
|
||||
|
||||
LOG("extra value : (%s)\n",val+js_org_str_length(js)); // if string add 1
|
||||
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
|
||||
}
|
||||
TEST(general_val_string_with_extra_value){
|
||||
|
||||
char *val = "-1 header : [ \"message\" , \"Hello! it's a string\" , [{},{},[ [ ] ] ] , { \"age\" : 48 , \"speaker\" : \" the president \" } , 45 , true ] , \"second\" : 324.1 , \"person\" : { \"name\" : \"Fana\" , \"age\" : 37 , \"genre\" : \"male\" , \"married\" : false , \"note\" : [2.1,4.45,4,10, { \"math\" : 17.1, \"malagasy\" : 12, \"eps\" : [ 5, 6 , 4 ] }] } , \"nul\" : { } here extra string";
|
||||
struct js_value *js = create_js_value(val, NULL);
|
||||
|
||||
|
||||
print_js_value(js);
|
||||
LOG("\ndebug: strlen(val)=%ld\n",strlen(val));
|
||||
LOG("debug: js->=str_value(%s)\n",js->str_value);
|
||||
LOG("debug: js->length=%ld\n",js->length);
|
||||
LOG("debug: js->code_type=%d\n",js->code_type);
|
||||
|
||||
LOG("extra value : (%s)\n",val+js_org_str_length(js)); // if string add 1
|
||||
|
||||
free_js_value(js);
|
||||
LOG("line %d\n",__LINE__);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv){
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user