10 #include "json_util.h"
11 #include "json_tokener.h"
16 #include "arraylist.h"
17 #include "json_object.h"
18 #include "ossupport.h"
19 #include "json_object_private.h"
23 struct json_object* json_object_from_file(char *filename)
26 struct json_object *obj;
27 char buf[JSON_FILE_BUF_SIZE];
30 if((fd = open(filename, O_RDONLY)) < 0) {
31 mc_error("json_object_from_file: error reading file %s: %s\n",
32 filename, strerror(errno));
35 if(!(pb = printbuf_new())) {
36 mc_error("json_object_from_file: printbuf_new failed\n");
39 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
40 printbuf_memappend(pb, buf, ret);
44 mc_abort("json_object_from_file: error reading file %s: %s\n",
45 filename, strerror(errno));
49 obj = json_tokener_parse(pb->buf);
54 int json_object_to_file(char *filename, struct json_object *obj)
57 int fd, ret, wpos, wsize;
60 mc_error("json_object_to_file: object is null\n");
64 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
65 mc_error("json_object_to_file: error opening file %s: %s\n",
66 filename, strerror(errno));
69 if(!(json_str = json_object_to_json_string(obj))) return -1;
70 wsize = strlen(json_str);
73 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
75 mc_error("json_object_to_file: error writing file %s: %s\n",
76 filename, strerror(errno));