2 * $Id: json_util.h,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.
14 * @brief Miscllaneous utility functions and macros.
19 #include "json_object.h"
22 #define json_min(a, b) ((a) < (b) ? (a) : (b))
26 #define json_max(a, b) ((a) > (b) ? (a) : (b))
33 #define JSON_FILE_BUF_SIZE 4096
35 /* utility functions */
37 * Read the full contents of the given file, then convert it to a
38 * json_object using json_tokener_parse().
40 * Returns NULL on failure. See json_util_get_last_err() for details.
42 JSON_EXPORT struct json_object *json_object_from_file(const char *filename);
45 * Create a JSON object from already opened file descriptor.
47 * This function can be helpful, when you opened the file already,
48 * e.g. when you have a temp file.
49 * Note, that the fd must be readable at the actual position, i.e.
50 * use lseek(fd, 0, SEEK_SET) before.
52 * The depth argument specifies the maximum object depth to pass to
53 * json_tokener_new_ex(). When depth == -1, JSON_TOKENER_DEFAULT_DEPTH
56 * Returns NULL on failure. See json_util_get_last_err() for details.
58 JSON_EXPORT struct json_object *json_object_from_fd_ex(int fd, int depth);
61 * Create a JSON object from an already opened file descriptor, using
62 * the default maximum object depth. (JSON_TOKENER_DEFAULT_DEPTH)
64 * See json_object_from_fd_ex() for details.
66 JSON_EXPORT struct json_object *json_object_from_fd(int fd);
70 * json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
72 * Returns -1 if something fails. See json_util_get_last_err() for details.
74 JSON_EXPORT int json_object_to_file(const char *filename, struct json_object *obj);
77 * Open and truncate the given file, creating it if necessary, then
78 * convert the json_object to a string and write it to the file.
80 * Returns -1 if something fails. See json_util_get_last_err() for details.
82 JSON_EXPORT int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
85 * Convert the json_object to a string and write it to the file descriptor.
86 * Handles partial writes and will keep writing until done, or an error
89 * @param fd an open, writable file descriptor to write to
90 * @param obj the object to serializer and write
91 * @param flags flags to pass to json_object_to_json_string_ext()
92 * @return -1 if something fails. See json_util_get_last_err() for details.
94 JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags);
97 * Return the last error from various json-c functions, including:
98 * json_object_to_file{,_ext}, json_object_to_fd() or
99 * json_object_from_{file,fd}, or NULL if there is none.
101 JSON_EXPORT const char *json_util_get_last_err(void);
103 /* these parsing helpers return zero on success */
104 JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
105 JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
109 JSON_EXPORT int json_parse_double(const char *buf, double *retval);
112 * Return a string describing the type of the object.
113 * e.g. "int", or "object", etc...
115 JSON_EXPORT const char *json_type_to_name(enum json_type o_type);