Implement all other encoding functions using json_dump_callback

This way we can check for JSON_ENCODE_ANY flag in one place only.
This commit is contained in:
Petri Lehtinen 2011-09-24 21:10:23 +03:00
parent 2a70d62251
commit b6d0191e51

View File

@ -413,39 +413,26 @@ static int do_dump(const json_t *json, size_t flags, int depth,
} }
} }
char *json_dumps(const json_t *json, size_t flags) char *json_dumps(const json_t *json, size_t flags)
{ {
strbuffer_t strbuff; strbuffer_t strbuff;
char *result; char *result;
if(!(flags & JSON_ENCODE_ANY)) {
if(!json_is_array(json) && !json_is_object(json))
return NULL;
}
if(strbuffer_init(&strbuff)) if(strbuffer_init(&strbuff))
return NULL; return NULL;
if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) { if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
strbuffer_close(&strbuff); result = NULL;
return NULL; else
}
result = jsonp_strdup(strbuffer_value(&strbuff)); result = jsonp_strdup(strbuffer_value(&strbuff));
strbuffer_close(&strbuff);
strbuffer_close(&strbuff);
return result; return result;
} }
int json_dumpf(const json_t *json, FILE *output, size_t flags) int json_dumpf(const json_t *json, FILE *output, size_t flags)
{ {
if(!(flags & JSON_ENCODE_ANY)) { return json_dump_callback(json, dump_to_file, (void *)output, flags);
if(!json_is_array(json) && !json_is_object(json))
return -1;
}
return do_dump(json, flags, 0, dump_to_file, (void *)output);
} }
int json_dump_file(const json_t *json, const char *path, size_t flags) int json_dump_file(const json_t *json, const char *path, size_t flags)