X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fdump.c;h=ac0b8dc75cafa99ad01319682bc01cc14df52b94;hb=f236c14dc5457a41119492ebce41b8920fd31f65;hp=28c00b94ef706260fbcb38ad29832c7adb044d89;hpb=9c5a8430dbb05f5308ab19fbd0a26d92483b7118;p=jansson.git diff --git a/src/dump.c b/src/dump.c index 28c00b9..ac0b8dc 100644 --- a/src/dump.c +++ b/src/dump.c @@ -9,11 +9,14 @@ #include #include #include -#include #include +#include "jansson_private.h" #include "strbuffer.h" +#define MAX_INTEGER_STR_LENGTH 100 +#define MAX_REAL_STR_LENGTH 100 + typedef int (*dump_func)(const char *buffer, int size, void *data); struct string @@ -36,22 +39,27 @@ static int dump_to_file(const char *buffer, int size, void *data) return 0; } -static int dump_indent(uint32_t flags, int depth, dump_func dump, void *data) +/* 256 spaces (the maximum indentation size) */ +static char whitespace[] = " "; + +static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) { if(JSON_INDENT(flags) > 0) { - char *ws_buffer; - int ws_count = JSON_INDENT(flags) * depth; + int i, ws_count = JSON_INDENT(flags); if(dump("\n", 1, data)) return -1; - if(ws_count == 0) - return 0; - - ws_buffer = alloca(ws_count); - memset(ws_buffer, ' ', ws_count); - return dump(ws_buffer, ws_count, data); + for(i = 0; i < depth; i++) + { + if(dump(whitespace, ws_count, data)) + return -1; + } + } + else if(space && !(flags & JSON_COMPACT)) + { + return dump(" ", 1, data); } return 0; } @@ -111,7 +119,7 @@ static int dump_string(const char *str, dump_func dump, void *data) return dump("\"", 1, data); } -static int do_dump(const json_t *json, uint32_t flags, int depth, +static int do_dump(const json_t *json, unsigned long flags, int depth, dump_func dump, void *data) { switch(json_typeof(json)) { @@ -126,30 +134,26 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, case JSON_INTEGER: { - char *buffer; - int size, ret; + char buffer[MAX_INTEGER_STR_LENGTH]; + int size; - size = asprintf(&buffer, "%d", json_integer_value(json)); - if(size == -1) + size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json)); + if(size >= MAX_INTEGER_STR_LENGTH) return -1; - ret = dump(buffer, size, data); - free(buffer); - return ret; + return dump(buffer, size, data); } case JSON_REAL: { - char *buffer; - int size, ret; + char buffer[MAX_REAL_STR_LENGTH]; + int size; - size = asprintf(&buffer, "%.17f", json_real_value(json)); - if(size == -1) + size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%0.17f", json_real_value(json)); + if(size >= MAX_REAL_STR_LENGTH) return -1; - ret = dump(buffer, size, data); - free(buffer); - return ret; + return dump(buffer, size, data); } case JSON_STRING: @@ -158,13 +162,22 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, case JSON_ARRAY: { int i; - int n = json_array_size(json); + int n; + json_array_t *array; + + /* detect circular references */ + array = json_to_array(json); + if(array->visited) + return -1; + array->visited = 1; + + n = json_array_size(json); if(dump("[", 1, data)) return -1; if(n == 0) return dump("]", 1, data); - if(dump_indent(flags, depth + 1, dump, data)) + if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; for(i = 0; i < n; ++i) { @@ -175,27 +188,49 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, if(i < n - 1) { if(dump(",", 1, data) || - dump_indent(flags, depth + 1, dump, data)) + dump_indent(flags, depth + 1, 1, dump, data)) return -1; } else { - if(dump_indent(flags, depth, dump, data)) + if(dump_indent(flags, depth, 0, dump, data)) return -1; } } + + array->visited = 0; return dump("]", 1, data); } case JSON_OBJECT: { - void *iter = json_object_iter((json_t *)json); + json_object_t *object; + void *iter; + const char *separator; + int separator_length; + + if(flags & JSON_COMPACT) { + separator = ":"; + separator_length = 1; + } + else { + separator = ": "; + separator_length = 2; + } + + /* detect circular references */ + object = json_to_object(json); + if(object->visited) + return -1; + object->visited = 1; + + iter = json_object_iter((json_t *)json); if(dump("{", 1, data)) return -1; if(!iter) return dump("}", 1, data); - if(dump_indent(flags, depth + 1, dump, data)) + if(dump_indent(flags, depth + 1, 0, dump, data)) return -1; while(iter) @@ -203,7 +238,7 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, void *next = json_object_iter_next((json_t *)json, iter); dump_string(json_object_iter_key(iter), dump, data); - if(dump(": ", 2, data) || + if(dump(separator, separator_length, data) || do_dump(json_object_iter_value(iter), flags, depth + 1, dump, data)) return -1; @@ -211,17 +246,19 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, if(next) { if(dump(",", 1, data) || - dump_indent(flags, depth + 1, dump, data)) + dump_indent(flags, depth + 1, 1, dump, data)) return -1; } else { - if(dump_indent(flags, depth, dump, data)) + if(dump_indent(flags, depth, 0, dump, data)) return -1; } iter = next; } + + object->visited = 0; return dump("}", 1, data); } @@ -232,7 +269,7 @@ static int do_dump(const json_t *json, uint32_t flags, int depth, } -char *json_dumps(const json_t *json, uint32_t flags) +char *json_dumps(const json_t *json, unsigned long flags) { strbuffer_t strbuff; char *result; @@ -241,13 +278,12 @@ char *json_dumps(const json_t *json, uint32_t flags) return NULL; if(strbuffer_init(&strbuff)) - return NULL; - - if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) return NULL; - if(dump_to_strbuffer("\n", 1, (void *)&strbuff)) + if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) { + strbuffer_close(&strbuff); return NULL; + } result = strdup(strbuffer_value(&strbuff)); strbuffer_close(&strbuff); @@ -255,17 +291,15 @@ char *json_dumps(const json_t *json, uint32_t flags) return result; } -int json_dumpf(const json_t *json, FILE *output, uint32_t flags) +int json_dumpf(const json_t *json, FILE *output, unsigned long flags) { if(!json_is_array(json) && !json_is_object(json)) return -1; - if(do_dump(json, flags, 0, dump_to_file, (void *)output)) - return -1; - return dump_to_file("\n", 1, (void *)output); + return do_dump(json, flags, 0, dump_to_file, (void *)output); } -int json_dump_file(const json_t *json, const char *path, uint32_t flags) +int json_dump_file(const json_t *json, const char *path, unsigned long flags) { int result;