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 2.8 kB

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