2 * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
15 #include "strerror_override.h"
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif /* HAVE_SYS_TYPES_H */
28 #ifdef HAVE_SYS_STAT_H
30 #endif /* HAVE_SYS_STAT_H */
34 #endif /* HAVE_FCNTL_H */
38 #endif /* HAVE_UNISTD_H */
41 #define WIN32_LEAN_AND_MEAN
44 #endif /* defined(WIN32) */
46 #if !defined(HAVE_OPEN) && defined(WIN32)
50 #include "snprintf_compat.h"
53 #include "json_inttypes.h"
54 #include "json_object.h"
55 #include "json_tokener.h"
56 #include "json_util.h"
59 static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
61 static char _last_err[256] = "";
63 const char *json_util_get_last_err(void)
65 if (_last_err[0] == '\0')
70 void _json_c_set_last_err(const char *err_fmt, ...)
73 va_start(ap, err_fmt);
74 // Ignore (attempted) overruns from snprintf
75 (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
79 struct json_object *json_object_from_fd(int fd)
81 return json_object_from_fd_ex(fd, -1);
83 struct json_object *json_object_from_fd_ex(int fd, int in_depth)
86 struct json_object *obj;
87 char buf[JSON_FILE_BUF_SIZE];
89 int depth = JSON_TOKENER_DEFAULT_DEPTH;
92 if (!(pb = printbuf_new()))
94 _json_c_set_last_err("json_object_from_fd_ex: printbuf_new failed\n");
100 tok = json_tokener_new_ex(depth);
103 _json_c_set_last_err(
104 "json_object_from_fd_ex: unable to allocate json_tokener(depth=%d): %s\n",
105 depth, strerror(errno));
110 while ((ret = read(fd, buf, sizeof(buf))) > 0)
112 if (printbuf_memappend(pb, buf, ret) < 0)
114 #if JSON_FILE_BUF_SIZE > INT_MAX
115 #error "Can't append more than INT_MAX bytes at a time"
117 _json_c_set_last_err(
118 "json_object_from_fd_ex: failed to printbuf_memappend after reading %d+%d bytes: %s", printbuf_length(pb), (int)ret, strerror(errno));
119 json_tokener_free(tok);
126 _json_c_set_last_err("json_object_from_fd_ex: error reading fd %d: %s\n", fd,
128 json_tokener_free(tok);
133 obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
135 _json_c_set_last_err("json_tokener_parse_ex failed: %s\n",
136 json_tokener_error_desc(json_tokener_get_error(tok)));
138 json_tokener_free(tok);
143 struct json_object *json_object_from_file(const char *filename)
145 struct json_object *obj;
148 if ((fd = open(filename, O_RDONLY)) < 0)
150 _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
151 filename, strerror(errno));
154 obj = json_object_from_fd(fd);
159 /* extended "format and write to file" function */
161 int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
168 _json_c_set_last_err("json_object_to_file_ext: object is null\n");
172 if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
174 _json_c_set_last_err("json_object_to_file_ext: error opening file %s: %s\n",
175 filename, strerror(errno));
178 ret = _json_object_to_fd(fd, obj, flags, filename);
185 int json_object_to_fd(int fd, struct json_object *obj, int flags)
189 _json_c_set_last_err("json_object_to_fd: object is null\n");
193 return _json_object_to_fd(fd, obj, flags, NULL);
195 static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
198 const char *json_str;
201 filename = filename ? filename : "(fd)";
203 if (!(json_str = json_object_to_json_string_ext(obj, flags)))
208 wsize = strlen(json_str);
212 if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0)
214 _json_c_set_last_err("json_object_to_fd: error writing file %s: %s\n",
215 filename, strerror(errno));
219 /* because of the above check for ret < 0, we can safely cast and add */
226 // backwards compatible "format and write to file" function
228 int json_object_to_file(const char *filename, struct json_object *obj)
230 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
233 // Deprecated json_parse_double function. See json_tokener_parse_double instead.
234 int json_parse_double(const char *buf, double *retval)
237 *retval = strtod(buf, &end);
238 return end == buf ? 1 : 0;
241 int json_parse_int64(const char *buf, int64_t *retval)
247 val = strtoll(buf, &end, 10);
250 if ((val == 0 && errno != 0) || (end == buf))
258 int json_parse_uint64(const char *buf, uint64_t *retval)
267 return 1; /* error: uint cannot be negative */
269 val = strtoull(buf, &end, 10);
272 if ((val == 0 && errno != 0) || (end == buf))
281 void *rpl_realloc(void *p, size_t n)
287 return realloc(p, n);
291 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
292 /* clang-format off */
293 static const char *json_type_name[] = {
294 /* If you change this, be sure to update the enum json_type definition too */
303 /* clang-format on */
305 const char *json_type_to_name(enum json_type o_type)
307 int o_type_int = (int)o_type;
308 if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
310 _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%u]\n", o_type,
311 (unsigned)NELEM(json_type_name));
314 return json_type_name[o_type];