From 99cc9a5154e5262d4b37ffda7b585baf4b1889ff Mon Sep 17 00:00:00 2001 From: rexy712 Date: Thu, 27 Feb 2020 10:17:33 -0800 Subject: [PATCH] Fix pretty printing --- src/output.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/output.c b/src/output.c index 81f29fa..e4a6f0b 100644 --- a/src/output.c +++ b/src/output.c @@ -173,9 +173,17 @@ static RJP_index irjp_write_value_pretty(char* dest, const RJP_value* val, int d static RJP_index irjp_dump_array_pretty(const RJP_value* arr, char* dest, int depth){ RJP_array_iterator it; rjp_init_array_iterator(&it, arr); - RJP_index pos = 2; - sprintf(dest, "[\n"); - for(RJP_value* current = rjp_array_iterator_current(&it);current;current = rjp_array_iterator_next(&it)){ + RJP_index pos = 0; + RJP_value* current = rjp_array_iterator_current(&it); + + if(!current){ + pos += sprintf(dest, "[]"); + rjp_delete_array_iterator(&it); + return pos; + } + pos += sprintf(dest+pos, "[\n"); + + for(;current;current = rjp_array_iterator_next(&it)){ for(int i = 0;i < (depth+1);++i) pos += sprintf(dest+pos, "\t"); pos += irjp_write_value_pretty(dest+pos, current, depth+1); @@ -187,15 +195,23 @@ static RJP_index irjp_dump_array_pretty(const RJP_value* arr, char* dest, int de for(int i = 0;i < depth;++i) pos += sprintf(dest+pos, "\t"); pos += sprintf(dest+pos, "]"); + + rjp_delete_array_iterator(&it); return pos; } static RJP_index irjp_dump_object_pretty(const RJP_value* root, char* dest, int depth){ RJP_object_iterator it; rjp_init_object_iterator(&it, root); - RJP_index pos = 2; - sprintf(dest, "{\n"); + RJP_index pos = 0; + RJP_value* current = rjp_object_iterator_current(&it); + if(!current){ + pos += sprintf(dest+pos, "{}"); + rjp_delete_object_iterator(&it); + return pos; + } + pos += sprintf(dest, "{\n"); - for(RJP_value* current = rjp_object_iterator_current(&it);current;current = rjp_object_iterator_next(&it)){ + for(;current;current = rjp_object_iterator_next(&it)){ for(int i = 0;i < (depth+1);++i) pos += sprintf(dest+pos, "\t"); pos += sprintf(dest+pos, "\"%s\": ", rjp_member_key(current)->value); @@ -208,6 +224,7 @@ static RJP_index irjp_dump_object_pretty(const RJP_value* root, char* dest, int for(int i = 0;i < depth;++i) pos += sprintf(dest+pos, "\t"); pos += sprintf(dest+pos, "}"); + rjp_delete_object_iterator(&it); return pos; } @@ -256,6 +273,7 @@ RJP_index rjp_dump_array(const RJP_value* arr, char* dest){ break; } pos += sprintf(dest+pos, "]"); + rjp_delete_array_iterator(&it); return pos; }