/jansson/src/jansson.h

http://github.com/nicolasff/webdis · C Header · 222 lines · 157 code · 47 blank · 18 comment · 26 complexity · 35f8bd1f472f8706a18faabfa68f7db3 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009, 2010 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 <jansson_config.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* version */
  16. #define JANSSON_MAJOR_VERSION 1
  17. #define JANSSON_MINOR_VERSION 3
  18. #define JANSSON_MICRO_VERSION 0
  19. /* Micro version is omitted if it's 0 */
  20. #define JANSSON_VERSION "1.3"
  21. /* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
  22. for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
  23. #define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
  24. (JANSSON_MINOR_VERSION << 8) | \
  25. (JANSSON_MICRO_VERSION << 0)))
  26. /* types */
  27. typedef enum {
  28. JSON_OBJECT,
  29. JSON_ARRAY,
  30. JSON_STRING,
  31. JSON_INTEGER,
  32. JSON_REAL,
  33. JSON_TRUE,
  34. JSON_FALSE,
  35. JSON_NULL
  36. } json_type;
  37. typedef struct {
  38. json_type type;
  39. size_t refcount;
  40. } json_t;
  41. #if JSON_INTEGER_IS_LONG_LONG
  42. #define JSON_INTEGER_FORMAT "lld"
  43. typedef long long json_int_t;
  44. #else
  45. #define JSON_INTEGER_FORMAT "ld"
  46. typedef long json_int_t;
  47. #endif /* JSON_INTEGER_IS_LONG_LONG */
  48. #define json_typeof(json) ((json)->type)
  49. #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
  50. #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
  51. #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
  52. #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
  53. #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
  54. #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
  55. #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
  56. #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
  57. #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
  58. #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
  59. /* construction, destruction, reference counting */
  60. json_t *json_object(void);
  61. json_t *json_array(void);
  62. json_t *json_string(const char *value);
  63. json_t *json_string_nocheck(const char *value);
  64. json_t *json_integer(json_int_t value);
  65. json_t *json_real(double value);
  66. json_t *json_true(void);
  67. json_t *json_false(void);
  68. json_t *json_null(void);
  69. static JSON_INLINE
  70. json_t *json_incref(json_t *json)
  71. {
  72. if(json && json->refcount != (size_t)-1)
  73. ++json->refcount;
  74. return json;
  75. }
  76. /* do not call json_delete directly */
  77. void json_delete(json_t *json);
  78. static JSON_INLINE
  79. void json_decref(json_t *json)
  80. {
  81. if(json && json->refcount != (size_t)-1 && --json->refcount == 0)
  82. json_delete(json);
  83. }
  84. /* error reporting */
  85. #define JSON_ERROR_TEXT_LENGTH 160
  86. #define JSON_ERROR_SOURCE_LENGTH 80
  87. typedef struct {
  88. char text[JSON_ERROR_TEXT_LENGTH];
  89. int line;
  90. int column;
  91. char source[JSON_ERROR_SOURCE_LENGTH];
  92. } json_error_t;
  93. /* getters, setters, manipulation */
  94. size_t json_object_size(const json_t *object);
  95. json_t *json_object_get(const json_t *object, const char *key);
  96. int json_object_set_new(json_t *object, const char *key, json_t *value);
  97. int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
  98. int json_object_del(json_t *object, const char *key);
  99. int json_object_clear(json_t *object);
  100. int json_object_update(json_t *object, json_t *other);
  101. void *json_object_iter(json_t *object);
  102. void *json_object_iter_at(json_t *object, const char *key);
  103. void *json_object_iter_next(json_t *object, void *iter);
  104. const char *json_object_iter_key(void *iter);
  105. json_t *json_object_iter_value(void *iter);
  106. int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
  107. static JSON_INLINE
  108. int json_object_set(json_t *object, const char *key, json_t *value)
  109. {
  110. return json_object_set_new(object, key, json_incref(value));
  111. }
  112. static JSON_INLINE
  113. int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
  114. {
  115. return json_object_set_new_nocheck(object, key, json_incref(value));
  116. }
  117. static JSON_INLINE
  118. int json_object_iter_set(json_t *object, void *iter, json_t *value)
  119. {
  120. return json_object_iter_set_new(object, iter, json_incref(value));
  121. }
  122. size_t json_array_size(const json_t *array);
  123. json_t *json_array_get(const json_t *array, size_t index);
  124. int json_array_set_new(json_t *array, size_t index, json_t *value);
  125. int json_array_append_new(json_t *array, json_t *value);
  126. int json_array_insert_new(json_t *array, size_t index, json_t *value);
  127. int json_array_remove(json_t *array, size_t index);
  128. int json_array_clear(json_t *array);
  129. int json_array_extend(json_t *array, json_t *other);
  130. static JSON_INLINE
  131. int json_array_set(json_t *array, size_t index, json_t *value)
  132. {
  133. return json_array_set_new(array, index, json_incref(value));
  134. }
  135. static JSON_INLINE
  136. int json_array_append(json_t *array, json_t *value)
  137. {
  138. return json_array_append_new(array, json_incref(value));
  139. }
  140. static JSON_INLINE
  141. int json_array_insert(json_t *array, size_t index, json_t *value)
  142. {
  143. return json_array_insert_new(array, index, json_incref(value));
  144. }
  145. const char *json_string_value(const json_t *string);
  146. json_int_t json_integer_value(const json_t *integer);
  147. double json_real_value(const json_t *real);
  148. double json_number_value(const json_t *json);
  149. int json_string_set(json_t *string, const char *value);
  150. int json_string_set_nocheck(json_t *string, const char *value);
  151. int json_integer_set(json_t *integer, json_int_t value);
  152. int json_real_set(json_t *real, double value);
  153. json_t *json_pack(json_error_t *error, const char *fmt, ...);
  154. int json_unpack(json_t *root, json_error_t *error, const char *fmt, ...);
  155. /* equality */
  156. int json_equal(json_t *value1, json_t *value2);
  157. /* copying */
  158. json_t *json_copy(json_t *value);
  159. json_t *json_deep_copy(json_t *value);
  160. /* loading, printing */
  161. json_t *json_loads(const char *input, size_t flags, json_error_t *error);
  162. json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
  163. json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
  164. #define JSON_INDENT(n) (n & 0x1F)
  165. #define JSON_COMPACT 0x20
  166. #define JSON_ENSURE_ASCII 0x40
  167. #define JSON_SORT_KEYS 0x80
  168. #define JSON_PRESERVE_ORDER 0x100
  169. char *json_dumps(const json_t *json, size_t flags);
  170. int json_dumpf(const json_t *json, FILE *output, size_t flags);
  171. int json_dump_file(const json_t *json, const char *path, size_t flags);
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif