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

316 lines
10KB

  1. /*
  2. * Copyright (c) 2009-2016 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 10
  19. #define JANSSON_MICRO_VERSION 0
  20. /* Micro version is omitted if it's 0 */
  21. #define JANSSON_VERSION "2.10"
  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_boolean_value json_is_true
  65. #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
  66. #define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL)
  67. /* construction, destruction, reference counting */
  68. json_t *json_object(void);
  69. json_t *json_array(void);
  70. json_t *json_string(const char *value);
  71. json_t *json_stringn(const char *value, size_t len);
  72. json_t *json_string_nocheck(const char *value);
  73. json_t *json_stringn_nocheck(const char *value, size_t len);
  74. json_t *json_integer(json_int_t value);
  75. json_t *json_real(double value);
  76. json_t *json_true(void);
  77. json_t *json_false(void);
  78. #define json_boolean(val) ((val) ? json_true() : json_false())
  79. json_t *json_null(void);
  80. static JSON_INLINE
  81. json_t *json_incref(json_t *json)
  82. {
  83. if(json && json->refcount != (size_t)-1)
  84. ++json->refcount;
  85. return json;
  86. }
  87. /* do not call json_delete directly */
  88. void json_delete(json_t *json);
  89. static JSON_INLINE
  90. void json_decref(json_t *json)
  91. {
  92. if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
  93. json_delete(json);
  94. }
  95. #if defined(__GNUC__) || defined(__clang__)
  96. static JSON_INLINE
  97. void json_decrefp(json_t **json)
  98. {
  99. if(json) {
  100. json_decref(*json);
  101. *json = NULL;
  102. }
  103. }
  104. #define json_auto_t json_t __attribute__((cleanup(json_decrefp)))
  105. #endif
  106. /* error reporting */
  107. #define JSON_ERROR_TEXT_LENGTH 160
  108. #define JSON_ERROR_SOURCE_LENGTH 80
  109. typedef struct {
  110. int line;
  111. int column;
  112. int position;
  113. char source[JSON_ERROR_SOURCE_LENGTH];
  114. char text[JSON_ERROR_TEXT_LENGTH];
  115. } json_error_t;
  116. /* getters, setters, manipulation */
  117. void json_object_seed(size_t seed);
  118. size_t json_object_size(const json_t *object);
  119. json_t *json_object_get(const json_t *object, const char *key);
  120. int json_object_set_new(json_t *object, const char *key, json_t *value);
  121. int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
  122. int json_object_del(json_t *object, const char *key);
  123. int json_object_clear(json_t *object);
  124. int json_object_update(json_t *object, json_t *other);
  125. int json_object_update_existing(json_t *object, json_t *other);
  126. int json_object_update_missing(json_t *object, json_t *other);
  127. void *json_object_iter(json_t *object);
  128. void *json_object_iter_at(json_t *object, const char *key);
  129. void *json_object_key_to_iter(const char *key);
  130. void *json_object_iter_next(json_t *object, void *iter);
  131. const char *json_object_iter_key(void *iter);
  132. json_t *json_object_iter_value(void *iter);
  133. int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
  134. #define json_object_foreach(object, key, value) \
  135. for(key = json_object_iter_key(json_object_iter(object)); \
  136. key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
  137. key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
  138. #define json_object_foreach_safe(object, n, key, value) \
  139. for(key = json_object_iter_key(json_object_iter(object)), \
  140. n = json_object_iter_next(object, json_object_key_to_iter(key)); \
  141. key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
  142. key = json_object_iter_key(n), \
  143. n = json_object_iter_next(object, json_object_key_to_iter(key)))
  144. #define json_array_foreach(array, index, value) \
  145. for(index = 0; \
  146. index < json_array_size(array) && (value = json_array_get(array, index)); \
  147. index++)
  148. static JSON_INLINE
  149. int json_object_set(json_t *object, const char *key, json_t *value)
  150. {
  151. return json_object_set_new(object, key, json_incref(value));
  152. }
  153. static JSON_INLINE
  154. int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
  155. {
  156. return json_object_set_new_nocheck(object, key, json_incref(value));
  157. }
  158. static JSON_INLINE
  159. int json_object_iter_set(json_t *object, void *iter, json_t *value)
  160. {
  161. return json_object_iter_set_new(object, iter, json_incref(value));
  162. }
  163. size_t json_array_size(const json_t *array);
  164. json_t *json_array_get(const json_t *array, size_t index);
  165. int json_array_set_new(json_t *array, size_t index, json_t *value);
  166. int json_array_append_new(json_t *array, json_t *value);
  167. int json_array_insert_new(json_t *array, size_t index, json_t *value);
  168. int json_array_remove(json_t *array, size_t index);
  169. int json_array_clear(json_t *array);
  170. int json_array_extend(json_t *array, json_t *other);
  171. static JSON_INLINE
  172. int json_array_set(json_t *array, size_t ind, json_t *value)
  173. {
  174. return json_array_set_new(array, ind, json_incref(value));
  175. }
  176. static JSON_INLINE
  177. int json_array_append(json_t *array, json_t *value)
  178. {
  179. return json_array_append_new(array, json_incref(value));
  180. }
  181. static JSON_INLINE
  182. int json_array_insert(json_t *array, size_t ind, json_t *value)
  183. {
  184. return json_array_insert_new(array, ind, json_incref(value));
  185. }
  186. const char *json_string_value(const json_t *string);
  187. size_t json_string_length(const json_t *string);
  188. json_int_t json_integer_value(const json_t *integer);
  189. double json_real_value(const json_t *real);
  190. double json_number_value(const json_t *json);
  191. int json_string_set(json_t *string, const char *value);
  192. int json_string_setn(json_t *string, const char *value, size_t len);
  193. int json_string_set_nocheck(json_t *string, const char *value);
  194. int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
  195. int json_integer_set(json_t *integer, json_int_t value);
  196. int json_real_set(json_t *real, double value);
  197. /* pack, unpack */
  198. json_t *json_pack(const char *fmt, ...);
  199. json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
  200. json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
  201. #define JSON_VALIDATE_ONLY 0x1
  202. #define JSON_STRICT 0x2
  203. int json_unpack(json_t *root, const char *fmt, ...);
  204. int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
  205. int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
  206. /* equality */
  207. int json_equal(json_t *value1, json_t *value2);
  208. /* copying */
  209. json_t *json_copy(json_t *value);
  210. json_t *json_deep_copy(const json_t *value);
  211. /* decoding */
  212. #define JSON_REJECT_DUPLICATES 0x1
  213. #define JSON_DISABLE_EOF_CHECK 0x2
  214. #define JSON_DECODE_ANY 0x4
  215. #define JSON_DECODE_INT_AS_REAL 0x8
  216. #define JSON_ALLOW_NUL 0x10
  217. typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
  218. json_t *json_loads(const char *input, size_t flags, json_error_t *error);
  219. json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
  220. json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
  221. json_t *json_loadfd(int input, size_t flags, json_error_t *error);
  222. json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
  223. json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
  224. /* encoding */
  225. #define JSON_MAX_INDENT 0x1F
  226. #define JSON_INDENT(n) ((n) & JSON_MAX_INDENT)
  227. #define JSON_COMPACT 0x20
  228. #define JSON_ENSURE_ASCII 0x40
  229. #define JSON_SORT_KEYS 0x80
  230. #define JSON_PRESERVE_ORDER 0x100
  231. #define JSON_ENCODE_ANY 0x200
  232. #define JSON_ESCAPE_SLASH 0x400
  233. #define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
  234. #define JSON_EMBED 0x10000
  235. typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
  236. char *json_dumps(const json_t *json, size_t flags);
  237. size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags);
  238. int json_dumpf(const json_t *json, FILE *output, size_t flags);
  239. int json_dumpfd(const json_t *json, int output, size_t flags);
  240. int json_dump_file(const json_t *json, const char *path, size_t flags);
  241. int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
  242. /* custom memory allocation */
  243. typedef void *(*json_malloc_t)(size_t);
  244. typedef void (*json_free_t)(void *);
  245. void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
  246. void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn);
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif