diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-04-28 13:26:09 -0500 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-04-28 13:26:09 -0500 |
commit | 3fcffe1bb01de6978f4516abd49a3b3c9e2cd2f2 (patch) | |
tree | 732aac674963768bf2aa9e3abc3cf5b7f1937758 /json_util.c | |
parent | f931f61851c949f8ab943dee8935b0d775346178 (diff) | |
download | json-c-3fcffe1bb01de6978f4516abd49a3b3c9e2cd2f2.tar.gz |
Add a json_object_to_json_string_ext() function to allow the formatting of output to be selected.
There are now three options: JSON_C_TO_STRING_SPACED, JSON_C_TO_STRING_PLAIN and JSON_C_TO_STRING_PRETTY.
This also add a json_object_to_file_ext() that takes the same flags.
Existing output of json_object_to_json_string() is unchanged, and uses JSON_C_TO_STRING_SPACED.
Thanks fo Grant Edwards for the initial patches.
Diffstat (limited to 'json_util.c')
-rw-r--r-- | json_util.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/json_util.c b/json_util.c index c6db882..e551d2d 100644 --- a/json_util.c +++ b/json_util.c @@ -65,12 +65,12 @@ struct json_object* json_object_from_file(const char *filename) if((fd = open(filename, O_RDONLY)) < 0) { MC_ERROR("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno)); - return (struct json_object*)error_ptr(-1); + return NULL; // XAX this is an API change! } if(!(pb = printbuf_new())) { close(fd); MC_ERROR("json_object_from_file: printbuf_new failed\n"); - return (struct json_object*)error_ptr(-1); + return NULL; } while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) { printbuf_memappend(pb, buf, ret); @@ -80,14 +80,16 @@ struct json_object* json_object_from_file(const char *filename) MC_ABORT("json_object_from_file: error reading file %s: %s\n", filename, strerror(errno)); printbuf_free(pb); - return (struct json_object*)error_ptr(-1); + return NULL; } obj = json_tokener_parse(pb->buf); printbuf_free(pb); return obj; } -int json_object_to_file(char *filename, struct json_object *obj) +/* extended "format and write to file" function */ + +int json_object_to_file_ext(char *filename, struct json_object *obj, int flags) { const char *json_str; int fd, ret; @@ -104,7 +106,7 @@ int json_object_to_file(char *filename, struct json_object *obj) return -1; } - if(!(json_str = json_object_to_json_string(obj))) { + if(!(json_str = json_object_to_json_string_ext(obj,flags))) { close(fd); return -1; } @@ -127,6 +129,13 @@ int json_object_to_file(char *filename, struct json_object *obj) return 0; } +// backwards compatible "format and write to file" function + +int json_object_to_file(char *filename, struct json_object *obj) +{ + return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN); +} + int json_parse_int64(const char *buf, int64_t *retval) { int64_t num64; |