json_util.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 -1 if something fails. See json_util_get_last_err() for details.
  34. */
  35. extern 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. * Returns -1 if something fails. See json_util_get_last_err() for details.
  45. */
  46. extern struct json_object* json_object_from_fd(int fd);
  47. /**
  48. * Equivalent to:
  49. * json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  50. *
  51. * Returns -1 if something fails. See json_util_get_last_err() for details.
  52. */
  53. extern int json_object_to_file(const char *filename, struct json_object *obj);
  54. /**
  55. * Open and truncate the given file, creating it if necessary, then
  56. * convert the json_object to a string and write it to the file.
  57. *
  58. * Returns -1 if something fails. See json_util_get_last_err() for details.
  59. */
  60. extern int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
  61. /**
  62. * Convert the json_object to a string and write it to the file descriptor.
  63. * Handles partial writes and will keep writing until done, or an error
  64. * occurs.
  65. *
  66. * @param fd an open, writable file descriptor to write to
  67. * @param obj the object to serializer and write
  68. * @param flags flags to pass to json_object_to_json_string_ext()
  69. * @return -1 if something fails. See json_util_get_last_err() for details.
  70. */
  71. extern int json_object_to_fd(int fd, struct json_object *obj, int flags);
  72. /**
  73. * Return the last error from various json-c functions, including:
  74. * json_object_to_file{,_ext}, json_object_to_fd() or
  75. * json_object_from_{file,fd}, or NULL if there is none.
  76. */
  77. const char *json_util_get_last_err(void);
  78. extern int json_parse_int64(const char *buf, int64_t *retval);
  79. extern int json_parse_double(const char *buf, double *retval);
  80. /**
  81. * Return a string describing the type of the object.
  82. * e.g. "int", or "object", etc...
  83. */
  84. extern const char *json_type_to_name(enum json_type o_type);
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. #endif