13 #include "json_object.h"
14 #include "json_tokener.h"
15 #include "json_util.h"
18 struct json_object* json_object_from_file(char *filename)
21 struct json_object *obj;
22 char buf[JSON_FILE_BUF_SIZE];
25 if((fd = open(filename, O_RDONLY)) < 0) {
26 mc_error("json_object_from_file: error reading file %s: %s\n",
27 filename, strerror(errno));
30 if(!(pb = printbuf_new())) {
31 mc_error("json_object_from_file: printbuf_new failed\n");
34 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
35 printbuf_memappend(pb, buf, ret);
39 mc_abort("json_object_from_file: error reading file %s: %s\n",
40 filename, strerror(errno));
44 obj = json_tokener_parse(pb->buf);
49 int json_object_to_file(char *filename, struct json_object *obj)
52 int fd, ret, wpos, wsize;
55 mc_error("json_object_to_file: object is null\n");
59 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
60 mc_error("json_object_to_file: error opening file %s: %s\n",
61 filename, strerror(errno));
64 if(!(json_str = json_object_to_json_string(obj))) return -1;
65 wsize = strlen(json_str);
68 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
70 mc_error("json_object_to_file: error writing file %s: %s\n",
71 filename, strerror(errno));