dump.c (jansson-2.13.1.tar.bz2) | : | dump.c (jansson-2.14.tar.bz2) | ||
---|---|---|---|---|
skipping to change at line 198 | skipping to change at line 198 | |||
if (dump(text, length, data)) | if (dump(text, length, data)) | |||
return -1; | return -1; | |||
str = pos = end; | str = pos = end; | |||
} | } | |||
return dump("\"", 1, data); | return dump("\"", 1, data); | |||
} | } | |||
struct key_len { | ||||
const char *key; | ||||
int len; | ||||
}; | ||||
static int compare_keys(const void *key1, const void *key2) { | static int compare_keys(const void *key1, const void *key2) { | |||
return strcmp(*(const char **)key1, *(const char **)key2); | const struct key_len *k1 = key1; | |||
const struct key_len *k2 = key2; | ||||
const size_t min_size = k1->len < k2->len ? k1->len : k2->len; | ||||
int res = memcmp(k1->key, k2->key, min_size); | ||||
if (res) | ||||
return res; | ||||
return k1->len - k2->len; | ||||
} | } | |||
static int do_dump(const json_t *json, size_t flags, int depth, hashtable_t *par ents, | static int do_dump(const json_t *json, size_t flags, int depth, hashtable_t *par ents, | |||
json_dump_callback_t dump, void *data) { | json_dump_callback_t dump, void *data) { | |||
int embed = flags & JSON_EMBED; | int embed = flags & JSON_EMBED; | |||
flags &= ~JSON_EMBED; | flags &= ~JSON_EMBED; | |||
if (!json) | if (!json) | |||
return -1; | return -1; | |||
skipping to change at line 256 | skipping to change at line 269 | |||
case JSON_STRING: | case JSON_STRING: | |||
return dump_string(json_string_value(json), json_string_length(json) , dump, | return dump_string(json_string_value(json), json_string_length(json) , dump, | |||
data, flags); | data, flags); | |||
case JSON_ARRAY: { | case JSON_ARRAY: { | |||
size_t n; | size_t n; | |||
size_t i; | size_t i; | |||
/* Space for "0x", double the sizeof a pointer for the hex and a | /* Space for "0x", double the sizeof a pointer for the hex and a | |||
* terminator. */ | * terminator. */ | |||
char key[2 + (sizeof(json) * 2) + 1]; | char key[2 + (sizeof(json) * 2) + 1]; | |||
size_t key_len; | ||||
/* detect circular references */ | /* detect circular references */ | |||
if (jsonp_loop_check(parents, json, key, sizeof(key))) | if (jsonp_loop_check(parents, json, key, sizeof(key), &key_len)) | |||
return -1; | return -1; | |||
n = json_array_size(json); | n = json_array_size(json); | |||
if (!embed && dump("[", 1, data)) | if (!embed && dump("[", 1, data)) | |||
return -1; | return -1; | |||
if (n == 0) { | if (n == 0) { | |||
hashtable_del(parents, key); | hashtable_del(parents, key, key_len); | |||
return embed ? 0 : dump("]", 1, data); | return embed ? 0 : dump("]", 1, data); | |||
} | } | |||
if (dump_indent(flags, depth + 1, 0, dump, data)) | if (dump_indent(flags, depth + 1, 0, dump, data)) | |||
return -1; | return -1; | |||
for (i = 0; i < n; ++i) { | for (i = 0; i < n; ++i) { | |||
if (do_dump(json_array_get(json, i), flags, depth + 1, parents, dump, | if (do_dump(json_array_get(json, i), flags, depth + 1, parents, dump, | |||
data)) | data)) | |||
return -1; | return -1; | |||
if (i < n - 1) { | if (i < n - 1) { | |||
if (dump(",", 1, data) || | if (dump(",", 1, data) || | |||
dump_indent(flags, depth + 1, 1, dump, data)) | dump_indent(flags, depth + 1, 1, dump, data)) | |||
return -1; | return -1; | |||
} else { | } else { | |||
if (dump_indent(flags, depth, 0, dump, data)) | if (dump_indent(flags, depth, 0, dump, data)) | |||
return -1; | return -1; | |||
} | } | |||
} | } | |||
hashtable_del(parents, key); | hashtable_del(parents, key, key_len); | |||
return embed ? 0 : dump("]", 1, data); | return embed ? 0 : dump("]", 1, data); | |||
} | } | |||
case JSON_OBJECT: { | case JSON_OBJECT: { | |||
void *iter; | void *iter; | |||
const char *separator; | const char *separator; | |||
int separator_length; | int separator_length; | |||
char loop_key[LOOP_KEY_LEN]; | char loop_key[LOOP_KEY_LEN]; | |||
size_t loop_key_len; | ||||
if (flags & JSON_COMPACT) { | if (flags & JSON_COMPACT) { | |||
separator = ":"; | separator = ":"; | |||
separator_length = 1; | separator_length = 1; | |||
} else { | } else { | |||
separator = ": "; | separator = ": "; | |||
separator_length = 2; | separator_length = 2; | |||
} | } | |||
/* detect circular references */ | /* detect circular references */ | |||
if (jsonp_loop_check(parents, json, loop_key, sizeof(loop_key))) | if (jsonp_loop_check(parents, json, loop_key, sizeof(loop_key), | |||
&loop_key_len)) | ||||
return -1; | return -1; | |||
iter = json_object_iter((json_t *)json); | iter = json_object_iter((json_t *)json); | |||
if (!embed && dump("{", 1, data)) | if (!embed && dump("{", 1, data)) | |||
return -1; | return -1; | |||
if (!iter) { | if (!iter) { | |||
hashtable_del(parents, loop_key); | hashtable_del(parents, loop_key, loop_key_len); | |||
return embed ? 0 : dump("}", 1, data); | return embed ? 0 : dump("}", 1, data); | |||
} | } | |||
if (dump_indent(flags, depth + 1, 0, dump, data)) | if (dump_indent(flags, depth + 1, 0, dump, data)) | |||
return -1; | return -1; | |||
if (flags & JSON_SORT_KEYS) { | if (flags & JSON_SORT_KEYS) { | |||
const char **keys; | struct key_len *keys; | |||
size_t size, i; | size_t size, i; | |||
size = json_object_size(json); | size = json_object_size(json); | |||
keys = jsonp_malloc(size * sizeof(const char *)); | keys = jsonp_malloc(size * sizeof(struct key_len)); | |||
if (!keys) | if (!keys) | |||
return -1; | return -1; | |||
i = 0; | i = 0; | |||
while (iter) { | while (iter) { | |||
keys[i] = json_object_iter_key(iter); | struct key_len *keylen = &keys[i]; | |||
keylen->key = json_object_iter_key(iter); | ||||
keylen->len = json_object_iter_key_len(iter); | ||||
iter = json_object_iter_next((json_t *)json, iter); | iter = json_object_iter_next((json_t *)json, iter); | |||
i++; | i++; | |||
} | } | |||
assert(i == size); | assert(i == size); | |||
qsort(keys, size, sizeof(const char *), compare_keys); | qsort(keys, size, sizeof(struct key_len), compare_keys); | |||
for (i = 0; i < size; i++) { | for (i = 0; i < size; i++) { | |||
const char *key; | const struct key_len *key; | |||
json_t *value; | json_t *value; | |||
key = keys[i]; | key = &keys[i]; | |||
value = json_object_get(json, key); | value = json_object_getn(json, key->key, key->len); | |||
assert(value); | assert(value); | |||
dump_string(key, strlen(key), dump, data, flags); | dump_string(key->key, key->len, dump, data, flags); | |||
if (dump(separator, separator_length, data) || | if (dump(separator, separator_length, data) || | |||
do_dump(value, flags, depth + 1, parents, dump, data)) { | do_dump(value, flags, depth + 1, parents, dump, data)) { | |||
jsonp_free(keys); | jsonp_free(keys); | |||
return -1; | return -1; | |||
} | } | |||
if (i < size - 1) { | if (i < size - 1) { | |||
if (dump(",", 1, data) || | if (dump(",", 1, data) || | |||
dump_indent(flags, depth + 1, 1, dump, data)) { | dump_indent(flags, depth + 1, 1, dump, data)) { | |||
jsonp_free(keys); | jsonp_free(keys); | |||
skipping to change at line 375 | skipping to change at line 395 | |||
} | } | |||
} | } | |||
jsonp_free(keys); | jsonp_free(keys); | |||
} else { | } else { | |||
/* Don't sort keys */ | /* Don't sort keys */ | |||
while (iter) { | while (iter) { | |||
void *next = json_object_iter_next((json_t *)json, iter); | void *next = json_object_iter_next((json_t *)json, iter); | |||
const char *key = json_object_iter_key(iter); | const char *key = json_object_iter_key(iter); | |||
const size_t key_len = json_object_iter_key_len(iter); | ||||
dump_string(key, strlen(key), dump, data, flags); | dump_string(key, key_len, dump, data, flags); | |||
if (dump(separator, separator_length, data) || | if (dump(separator, separator_length, data) || | |||
do_dump(json_object_iter_value(iter), flags, depth + 1, parents, | do_dump(json_object_iter_value(iter), flags, depth + 1, parents, | |||
dump, data)) | dump, data)) | |||
return -1; | return -1; | |||
if (next) { | if (next) { | |||
if (dump(",", 1, data) || | if (dump(",", 1, data) || | |||
dump_indent(flags, depth + 1, 1, dump, data)) | dump_indent(flags, depth + 1, 1, dump, data)) | |||
return -1; | return -1; | |||
} else { | } else { | |||
if (dump_indent(flags, depth, 0, dump, data)) | if (dump_indent(flags, depth, 0, dump, data)) | |||
return -1; | return -1; | |||
} | } | |||
iter = next; | iter = next; | |||
} | } | |||
} | } | |||
hashtable_del(parents, loop_key); | hashtable_del(parents, loop_key, loop_key_len); | |||
return embed ? 0 : dump("}", 1, data); | return embed ? 0 : dump("}", 1, data); | |||
} | } | |||
default: | default: | |||
/* not reached */ | /* not reached */ | |||
return -1; | return -1; | |||
} | } | |||
} | } | |||
char *json_dumps(const json_t *json, size_t flags) { | char *json_dumps(const json_t *json, size_t flags) { | |||
End of changes. 19 change blocks. | ||||
16 lines changed or deleted | 37 lines changed or added |