jansson.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2009-2013 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #ifndef JANSSON_H
  8. #define JANSSON_H
  9. #include <stdio.h>
  10. #include <stdlib.h> /* for size_t */
  11. #include <stdarg.h>
  12. #include <jansson_config.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* version */
  17. #define JANSSON_MAJOR_VERSION 2
  18. #define JANSSON_MINOR_VERSION 5
  19. #define JANSSON_MICRO_VERSION 0
  20. /* Micro version is omitted if it's 0 */
  21. #define JANSSON_VERSION "2.5"
  22. /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
  23. for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
  24. #define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
  25. (JANSSON_MINOR_VERSION << 8) | \
  26. (JANSSON_MICRO_VERSION << 0))
  27. /* types */
  28. typedef enum {
  29. JSON_OBJECT,
  30. JSON_ARRAY,
  31. JSON_STRING,
  32. JSON_INTEGER,
  33. JSON_REAL,
  34. JSON_TRUE,
  35. JSON_FALSE,
  36. JSON_NULL
  37. } json_type;
  38. typedef struct json_t {
  39. json_type type;
  40. size_t refcount;
  41. } json_t;
  42. #ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
  43. #if JSON_INTEGER_IS_LONG_LONG
  44. #ifdef _WIN32
  45. #define JSON_INTEGER_FORMAT "I64d"
  46. #else
  47. #define JSON_INTEGER_FORMAT "lld"
  48. #endif
  49. typedef long long json_int_t;
  50. #else
  51. #define JSON_INTEGER_FORMAT "ld"
  52. typedef long json_int_t;
  53. #endif /* JSON_INTEGER_IS_LONG_LONG */
  54. #endif
  55. #define json_typeof(json) ((json)->type)
  56. #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
  57. #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
  58. #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
  59. #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
  60. #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
  61. #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
  62. #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
  63. #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
  64. #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
  65. #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
  66. /* construction, destruction, reference counting */
  67. json_t *json_object(void);
  68. json_t *json_array(void);
  69. json_t *json_string(const char *value);
  70. json_t *json_string_nocheck(const char *value);
  71. json_t *json_integer(json_int_t value);
  72. json_t *json_real(double value);
  73. json_t *json_true(void);
  74. json_t *json_false(void);
  75. #define json_boolean(val) ((val) ? json_true() : json_false())
  76. json_t *json_null(void);
  77. static JSON_INLINE
  78. json_t *json_incref(json_t *json)
  79. {
  80. if(json && json->refcount != (size_t)-1)
  81. ++json->refcount;
  82. return json;
  83. }
  84. /* do not call json_delete directly */
  85. void json_delete(json_t *json);
  86. static JSON_INLINE
  87. void json_decref(json_t *json)
  88. {
  89. if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
  90. json_delete(json);
  91. }
  92. /* error reporting */
  93. #define JSON_ERROR_TEXT_LENGTH 160
  94. #define JSON_ERROR_SOURCE_LENGTH 80
  95. typedef struct {
  96. int line;
  97. int column;
  98. int position;
  99. char source[JSON_ERROR_SOURCE_LENGTH];
  100. char text[JSON_ERROR_TEXT_LENGTH];
  101. } json_error_t;
  102. /* getters, setters, manipulation */
  103. size_t json_object_size(const json_t *object);
  104. json_t *json_object_get(const json_t *object, const char *key);
  105. int json_object_set_new(json_t *object, const char *key, json_t *value);
  106. int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
  107. int json_object_del(json_t *object, const char *key);
  108. int json_object_clear(json_t *object);
  109. int json_object_update(json_t *object, json_t *other);
  110. int json_object_update_existing(json_t *object, json_t *other);
  111. int json_object_update_missing(json_t *object, json_t *other);
  112. void *json_object_iter(json_t *object);
  113. void *json_object_iter_at(json_t *object, const char *key);
  114. void *json_object_key_to_iter(const char *key);
  115. void *json_object_iter_next(json_t *object, void *iter);
  116. const char *json_object_iter_key(void *iter);
  117. json_t *json_object_iter_value(void *iter);
  118. int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
  119. #define json_object_foreach(object, key, value) \
  120. for(key = json_object_iter_key(json_object_iter(object)); \
  121. key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
  122. key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
  123. #define json_array_foreach(array, index, value) \
  124. for(index = 0; \
  125. index < json_array_size(array) && (value = json_array_get(array, index)); \
  126. index++)
  127. static JSON_INLINE
  128. int json_object_set(json_t *object, const char *key, json_t *value)
  129. {
  130. return json_object_set_new(object, key, json_incref(value));
  131. }
  132. static JSON_INLINE
  133. int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
  134. {
  135. return json_object_set_new_nocheck(object, key, json_incref(value));
  136. }
  137. static JSON_INLINE
  138. int json_object_iter_set(json_t *object, void *iter, json_t *value)
  139. {
  140. return json_object_iter_set_new(object, iter, json_incref(value));
  141. }
  142. size_t json_array_size(const json_t *array);
  143. json_t *json_array_get(const json_t *array, size_t index);
  144. int json_array_set_new(json_t *array, size_t index, json_t *value);
  145. int json_array_append_new(json_t *array, json_t *value);
  146. int json_array_insert_new(json_t *array, size_t index, json_t *value);
  147. int json_array_remove(json_t *array, size_t index);
  148. int json_array_clear(json_t *array);
  149. int json_array_extend(json_t *array, json_t *other);
  150. static JSON_INLINE
  151. int json_array_set(json_t *array, size_t ind, json_t *value)
  152. {
  153. return json_array_set_new(array, ind, json_incref(value));
  154. }
  155. static JSON_INLINE
  156. int json_array_append(json_t *array, json_t *value)
  157. {
  158. return json_array_append_new(array, json_incref(value));
  159. }
  160. static JSON_INLINE
  161. int json_array_insert(json_t *array, size_t ind, json_t *value)
  162. {
  163. return json_array_insert_new(array, ind, json_incref(value));
  164. }
  165. const char *json_string_value(const json_t *string);
  166. json_int_t json_integer_value(const json_t *integer);
  167. double json_real_value(const json_t *real);
  168. double json_number_value(const json_t *json);
  169. int json_string_set(json_t *string, const char *value);
  170. int json_string_set_nocheck(json_t *string, const char *value);
  171. int json_integer_set(json_t *integer, json_int_t value);
  172. int json_real_set(json_t *real, double value);
  173. /* pack, unpack */
  174. json_t *json_pack(const char *fmt, ...);
  175. json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
  176. json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
  177. #define JSON_VALIDATE_ONLY 0x1
  178. #define JSON_STRICT 0x2
  179. int json_unpack(json_t *root, const char *fmt, ...);
  180. int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
  181. int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
  182. /* equality */
  183. int json_equal(json_t *value1, json_t *value2);
  184. /* copying */
  185. json_t *json_copy(json_t *value);
  186. json_t *json_deep_copy(const json_t *value);
  187. /* decoding */
  188. #define JSON_REJECT_DUPLICATES 0x1
  189. #define JSON_DISABLE_EOF_CHECK 0x2
  190. #define JSON_DECODE_ANY 0x4
  191. #define JSON_DECODE_INT_AS_REAL 0x8
  192. typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
  193. json_t *json_loads(const char *input, size_t flags, json_error_t *error);
  194. json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
  195. json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
  196. json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
  197. json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
  198. /* encoding */
  199. #define JSON_INDENT(n) (n & 0x1F)
  200. #define JSON_COMPACT 0x20
  201. #define JSON_ENSURE_ASCII 0x40
  202. #define JSON_SORT_KEYS 0x80
  203. #define JSON_PRESERVE_ORDER 0x100
  204. #define JSON_ENCODE_ANY 0x200
  205. #define JSON_ESCAPE_SLASH 0x400
  206. typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
  207. char *json_dumps(const json_t *json, size_t flags);
  208. int json_dumpf(const json_t *json, FILE *output, size_t flags);
  209. int json_dump_file(const json_t *json, const char *path, size_t flags);
  210. int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
  211. /* custom memory allocation */
  212. typedef void *(*json_malloc_t)(size_t);
  213. typedef void (*json_free_t)(void *);
  214. void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #endif