X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fdump.c;h=0ffdfc64be1a16efc86780c6e7063cef2fd1891b;hb=fa7c2ea070672d05b34b6341142a193e9a7d0a8c;hp=870f30ecfe970904e83095b1dcbf92c2a2efaf35;hpb=453e4c0aa2fbca95676fa966cc5602f5daf0a1f2;p=jansson.git diff --git a/src/dump.c b/src/dump.c index 870f30e..0ffdfc6 100644 --- a/src/dump.c +++ b/src/dump.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Petri Lehtinen + * Copyright (c) 2009-2011 Petri Lehtinen * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -41,10 +41,10 @@ static int dump_to_file(const char *buffer, int size, void *data) return 0; } -/* 256 spaces (the maximum indentation size) */ -static char whitespace[] = " "; +/* 32 spaces (the maximum indentation size) */ +static char whitespace[] = " "; -static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) +static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data) { if(JSON_INDENT(flags) > 0) { @@ -153,12 +153,19 @@ static int dump_string(const char *str, int ascii, dump_func dump, void *data) return dump("\"", 1, data); } -static int object_key_cmp(const void *key1, const void *key2) +static int object_key_compare_keys(const void *key1, const void *key2) { - return strcmp(*(const char **)key1, *(const char **)key2); + return strcmp((*(const object_key_t **)key1)->key, + (*(const object_key_t **)key2)->key); } -static int do_dump(const json_t *json, unsigned long flags, int depth, +static int object_key_compare_serials(const void *key1, const void *key2) +{ + return (*(const object_key_t **)key1)->serial - + (*(const object_key_t **)key2)->serial; +} + +static int do_dump(const json_t *json, size_t flags, int depth, dump_func dump, void *data) { int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0; @@ -178,7 +185,9 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, char buffer[MAX_INTEGER_STR_LENGTH]; int size; - size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json)); + size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, + "%" JSON_INTEGER_FORMAT, + json_integer_value(json)); if(size >= MAX_INTEGER_STR_LENGTH) return -1; @@ -297,36 +306,39 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(dump_indent(flags, depth + 1, 0, dump, data)) goto object_error; - if(flags & JSON_SORT_KEYS) + if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER) { - /* Sort keys */ - - const char **keys; - unsigned int size; - unsigned int i; + const object_key_t **keys; + size_t size, i; + int (*cmp_func)(const void *, const void *); size = json_object_size(json); - keys = malloc(size * sizeof(const char *)); + keys = malloc(size * sizeof(object_key_t *)); if(!keys) goto object_error; i = 0; while(iter) { - keys[i] = json_object_iter_key(iter); + keys[i] = jsonp_object_iter_fullkey(iter); iter = json_object_iter_next((json_t *)json, iter); i++; } assert(i == size); - qsort(keys, size, sizeof(const char *), object_key_cmp); + if(flags & JSON_SORT_KEYS) + cmp_func = object_key_compare_keys; + else + cmp_func = object_key_compare_serials; + + qsort(keys, size, sizeof(object_key_t *), cmp_func); for(i = 0; i < size; i++) { const char *key; json_t *value; - key = keys[i]; + key = keys[i]->key; value = json_object_get(json, key); assert(value); @@ -404,7 +416,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, } -char *json_dumps(const json_t *json, unsigned long flags) +char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; char *result; @@ -426,7 +438,7 @@ char *json_dumps(const json_t *json, unsigned long flags) return result; } -int json_dumpf(const json_t *json, FILE *output, unsigned long flags) +int json_dumpf(const json_t *json, FILE *output, size_t flags) { if(!json_is_array(json) && !json_is_object(json)) return -1; @@ -434,7 +446,7 @@ int json_dumpf(const json_t *json, FILE *output, unsigned long flags) return do_dump(json, flags, 0, dump_to_file, (void *)output); } -int json_dump_file(const json_t *json, const char *path, unsigned long flags) +int json_dump_file(const json_t *json, const char *path, size_t flags) { int result;