You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_util.h 3.5 kB

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