1 /* 2 * $Id: json_util.h,v 1.4 2006/01/30 23:07:57 mclark Exp $ 3 * 4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. 5 * Michael Clark <michael@metaparadigm.com> 6 * 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. 9 * 10 */ 11 12 /** 13 * @file 14 * @brief Miscllaneous utility functions and macros. 15 */ 16 #ifndef _json_util_h_ 17 #define _json_util_h_ 18 19 #include "json_object.h" 20 21 #ifndef json_min 22 #define json_min(a,b) ((a) < (b) ? (a) : (b)) 23 #endif 24 25 #ifndef json_max 26 #define json_max(a,b) ((a) > (b) ? (a) : (b)) 27 #endif 28 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #define JSON_FILE_BUF_SIZE 4096 35 36 /* utility functions */ 37 /** 38 * Read the full contents of the given file, then convert it to a 39 * json_object using json_tokener_parse(). 40 * 41 * Returns NULL on failure. See json_util_get_last_err() for details. 42 */ 43 JSON_EXPORT struct json_object* json_object_from_file(const char *filename); 44 45 /** 46 * Create a JSON object from already opened file descriptor. 47 * 48 * This function can be helpful, when you opened the file already, 49 * e.g. when you have a temp file. 50 * Note, that the fd must be readable at the actual position, i.e. 51 * use lseek(fd, 0, SEEK_SET) before. 52 * 53 * The depth argument specifies the maximum object depth to pass to 54 * json_tokener_new_ex(). When depth == -1, JSON_TOKENER_DEFAULT_DEPTH 55 * is used instead. 56 * 57 * Returns NULL on failure. See json_util_get_last_err() for details. 58 */ 59 JSON_EXPORT struct json_object* json_object_from_fd_ex(int fd, int depth); 60 61 /** 62 * Create a JSON object from an already opened file descriptor, using 63 * the default maximum object depth. (JSON_TOKENER_DEFAULT_DEPTH) 64 * 65 * See json_object_from_fd_ex() for details. 66 */ 67 JSON_EXPORT struct json_object* json_object_from_fd(int fd); 68 69 /** 70 * Equivalent to: 71 * json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN); 72 * 73 * Returns -1 if something fails. See json_util_get_last_err() for details. 74 */ 75 JSON_EXPORT int json_object_to_file(const char *filename, struct json_object *obj); 76 77 /** 78 * Open and truncate the given file, creating it if necessary, then 79 * convert the json_object to a string and write it to the file. 80 * 81 * Returns -1 if something fails. See json_util_get_last_err() for details. 82 */ 83 JSON_EXPORT int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags); 84 85 /** 86 * Convert the json_object to a string and write it to the file descriptor. 87 * Handles partial writes and will keep writing until done, or an error 88 * occurs. 89 * 90 * @param fd an open, writable file descriptor to write to 91 * @param obj the object to serializer and write 92 * @param flags flags to pass to json_object_to_json_string_ext() 93 * @return -1 if something fails. See json_util_get_last_err() for details. 94 */ 95 JSON_EXPORT int json_object_to_fd(int fd, struct json_object *obj, int flags); 96 97 /** 98 * Return the last error from various json-c functions, including: 99 * json_object_to_file{,_ext}, json_object_to_fd() or 100 * json_object_from_{file,fd}, or NULL if there is none. 101 */ 102 JSON_EXPORT const char *json_util_get_last_err(void); 103 104 105 JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval); 106 JSON_EXPORT int json_parse_double(const char *buf, double *retval); 107 108 /** 109 * Return a string describing the type of the object. 110 * e.g. "int", or "object", etc... 111 */ 112 JSON_EXPORT const char *json_type_to_name(enum json_type o_type); 113 114 #ifdef __cplusplus 115 } 116 #endif 117 118 #endif 119