jansson.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2009-2011 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 1
  19. #define JANSSON_MICRO_VERSION 0
  20. /* Micro version is omitted if it's 0 */
  21. #define JANSSON_VERSION "2.1"
  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 {
  39. json_type type;
  40. size_t refcount;
  41. } json_t;
  42. #if JSON_INTEGER_IS_LONG_LONG && (!defined(WIN32))
  43. #define JSON_INTEGER_FORMAT "lld"
  44. typedef long long json_int_t;
  45. #else
  46. #define JSON_INTEGER_FORMAT "ld"
  47. typedef long json_int_t;
  48. #endif /* JSON_INTEGER_IS_LONG_LONG */
  49. #define json_typeof(json) ((json)->type)
  50. #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
  51. #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
  52. #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
  53. #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
  54. #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
  55. #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
  56. #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
  57. #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
  58. #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
  59. #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
  60. /* construction, destruction, reference counting */
  61. json_t *json_object(void);
  62. json_t *json_array(void);
  63. json_t *json_string(const char *value);
  64. json_t *json_string_nocheck(const char *value);
  65. json_t *json_integer(json_int_t value);
  66. json_t *json_real(double value);
  67. json_t *json_true(void);
  68. json_t *json_false(void);
  69. json_t *json_null(void);
  70. static JSON_INLINE
  71. json_t *json_incref(json_t *json)
  72. {
  73. if(json && json->refcount != (size_t)-1)
  74. ++json->refcount;
  75. return json;
  76. }
  77. /* do not call json_delete directly */
  78. void json_delete(json_t *json);
  79. static JSON_INLINE
  80. void json_decref(json_t *json)
  81. {
  82. if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
  83. json_delete(json);
  84. }
  85. /* error reporting */
  86. #define JSON_ERROR_TEXT_LENGTH 160
  87. #define JSON_ERROR_SOURCE_LENGTH 80
  88. typedef struct {
  89. int line;
  90. int column;
  91. int position;
  92. char source[JSON_ERROR_SOURCE_LENGTH];
  93. char text[JSON_ERROR_TEXT_LENGTH];
  94. } json_error_t;
  95. /* getters, setters, manipulation */
  96. size_t json_object_size(const json_t *object);
  97. json_t *json_object_get(const json_t *object, const char *key);
  98. int json_object_set_new(json_t *object, const char *key, json_t *value);
  99. int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
  100. int json_object_del(json_t *object, const char *key);
  101. int json_object_clear(json_t *object);
  102. int json_object_update(json_t *object, json_t *other);
  103. void *json_object_iter(json_t *object);
  104. void *json_object_iter_at(json_t *object, const char *key);
  105. void *json_object_iter_next(json_t *object, void *iter);
  106. const char *json_object_iter_key(void *iter);
  107. json_t *json_object_iter_value(void *iter);
  108. int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
  109. static JSON_INLINE
  110. int json_object_set(json_t *object, const char *key, json_t *value)
  111. {
  112. return json_object_set_new(object, key, json_incref(value));
  113. }
  114. static JSON_INLINE
  115. int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
  116. {
  117. return json_object_set_new_nocheck(object, key, json_incref(value));
  118. }
  119. static JSON_INLINE
  120. int json_object_iter_set(json_t *object, void *iter, json_t *value)
  121. {
  122. return json_object_iter_set_new(object, iter, json_incref(value));
  123. }
  124. size_t json_array_size(const json_t *array);
  125. json_t *json_array_get(const json_t *array, size_t index);
  126. int json_array_set_new(json_t *array, size_t index, json_t *value);
  127. int json_array_append_new(json_t *array, json_t *value);
  128. int json_array_insert_new(json_t *array, size_t index, json_t *value);
  129. int json_array_remove(json_t *array, size_t index);
  130. int json_array_clear(json_t *array);
  131. int json_array_extend(json_t *array, json_t *other);
  132. static JSON_INLINE
  133. int json_array_set(json_t *array, size_t index, json_t *value)
  134. {
  135. return json_array_set_new(array, index, json_incref(value));
  136. }
  137. static JSON_INLINE
  138. int json_array_append(json_t *array, json_t *value)
  139. {
  140. return json_array_append_new(array, json_incref(value));
  141. }
  142. static JSON_INLINE
  143. int json_array_insert(json_t *array, size_t index, json_t *value)
  144. {
  145. return json_array_insert_new(array, index, json_incref(value));
  146. }
  147. const char *json_string_value(const json_t *string);
  148. json_int_t json_integer_value(const json_t *integer);
  149. double json_real_value(const json_t *real);
  150. double json_number_value(const json_t *json);
  151. int json_string_set(json_t *string, const char *value);
  152. int json_string_set_nocheck(json_t *string, const char *value);
  153. int json_integer_set(json_t *integer, json_int_t value);
  154. int json_real_set(json_t *real, double value);
  155. /* pack, unpack */
  156. json_t *json_pack(const char *fmt, ...);
  157. json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
  158. json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
  159. #define JSON_VALIDATE_ONLY 0x1
  160. #define JSON_STRICT 0x2
  161. int json_unpack(json_t *root, const char *fmt, ...);
  162. int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
  163. int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
  164. /* equality */
  165. int json_equal(json_t *value1, json_t *value2);
  166. /* copying */
  167. json_t *json_copy(json_t *value);
  168. json_t *json_deep_copy(json_t *value);
  169. /* decoding */
  170. #define JSON_REJECT_DUPLICATES 0x1
  171. #define JSON_DISABLE_EOF_CHECK 0x2
  172. json_t *json_loads(const char *input, size_t flags, json_error_t *error);
  173. json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
  174. json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
  175. json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
  176. /* encoding */
  177. #define JSON_INDENT(n) (n & 0x1F)
  178. #define JSON_COMPACT 0x20
  179. #define JSON_ENSURE_ASCII 0x40
  180. #define JSON_SORT_KEYS 0x80
  181. #define JSON_PRESERVE_ORDER 0x100
  182. #define JSON_ENCODE_ANY 0x200
  183. char *json_dumps(const json_t *json, size_t flags);
  184. int json_dumpf(const json_t *json, FILE *output, size_t flags);
  185. int json_dump_file(const json_t *json, const char *path, size_t flags);
  186. /* custom memory allocation */
  187. typedef void *(*json_malloc_t)(size_t);
  188. typedef void (*json_free_t)(void *);
  189. void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif