jansson.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef __cplusplus
  11. #define JSON_INLINE inline
  12. #else
  13. #define JSON_INLINE inline
  14. extern "C" {
  15. #endif
  16. /* types */
  17. typedef enum {
  18. JSON_OBJECT,
  19. JSON_ARRAY,
  20. JSON_STRING,
  21. JSON_INTEGER,
  22. JSON_REAL,
  23. JSON_TRUE,
  24. JSON_FALSE,
  25. JSON_NULL
  26. } json_type;
  27. typedef struct {
  28. json_type type;
  29. unsigned long refcount;
  30. } json_t;
  31. #define json_typeof(json) ((json)->type)
  32. #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
  33. #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
  34. #define json_is_string(json) (json && json_typeof(json) == JSON_STRING)
  35. #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER)
  36. #define json_is_real(json) (json && json_typeof(json) == JSON_REAL)
  37. #define json_is_number(json) (json_is_integer(json) || json_is_real(json))
  38. #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE)
  39. #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE)
  40. #define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
  41. #define json_is_null(json) (json && json_typeof(json) == JSON_NULL)
  42. /* construction, destruction, reference counting */
  43. json_t *json_object(void);
  44. json_t *json_array(void);
  45. json_t *json_string(const char *value);
  46. json_t *json_string_nocheck(const char *value);
  47. json_t *json_integer(int value);
  48. json_t *json_real(double value);
  49. json_t *json_true(void);
  50. json_t *json_false(void);
  51. json_t *json_null(void);
  52. static JSON_INLINE
  53. json_t *json_incref(json_t *json)
  54. {
  55. if(json && json->refcount != (unsigned int)-1)
  56. ++json->refcount;
  57. return json;
  58. }
  59. /* do not call json_delete directly */
  60. void json_delete(json_t *json);
  61. static JSON_INLINE
  62. void json_decref(json_t *json)
  63. {
  64. if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0)
  65. json_delete(json);
  66. }
  67. /* getters, setters, manipulation */
  68. unsigned int json_object_size(const json_t *object);
  69. json_t *json_object_get(const json_t *object, const char *key);
  70. int json_object_set_new(json_t *object, const char *key, json_t *value);
  71. int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
  72. int json_object_del(json_t *object, const char *key);
  73. int json_object_clear(json_t *object);
  74. int json_object_update(json_t *object, json_t *other);
  75. void *json_object_iter(json_t *object);
  76. void *json_object_iter_at(json_t *object, const char *key);
  77. void *json_object_iter_next(json_t *object, void *iter);
  78. const char *json_object_iter_key(void *iter);
  79. json_t *json_object_iter_value(void *iter);
  80. int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
  81. static JSON_INLINE
  82. int json_object_set(json_t *object, const char *key, json_t *value)
  83. {
  84. return json_object_set_new(object, key, json_incref(value));
  85. }
  86. static JSON_INLINE
  87. int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
  88. {
  89. return json_object_set_new_nocheck(object, key, json_incref(value));
  90. }
  91. static inline
  92. int json_object_iter_set(json_t *object, void *iter, json_t *value)
  93. {
  94. return json_object_iter_set_new(object, iter, json_incref(value));
  95. }
  96. unsigned int json_array_size(const json_t *array);
  97. json_t *json_array_get(const json_t *array, unsigned int index);
  98. int json_array_set_new(json_t *array, unsigned int index, json_t *value);
  99. int json_array_append_new(json_t *array, json_t *value);
  100. int json_array_insert_new(json_t *array, unsigned int index, json_t *value);
  101. int json_array_remove(json_t *array, unsigned int index);
  102. int json_array_clear(json_t *array);
  103. int json_array_extend(json_t *array, json_t *other);
  104. static JSON_INLINE
  105. int json_array_set(json_t *array, unsigned int index, json_t *value)
  106. {
  107. return json_array_set_new(array, index, json_incref(value));
  108. }
  109. static JSON_INLINE
  110. int json_array_append(json_t *array, json_t *value)
  111. {
  112. return json_array_append_new(array, json_incref(value));
  113. }
  114. static JSON_INLINE
  115. int json_array_insert(json_t *array, unsigned int index, json_t *value)
  116. {
  117. return json_array_insert_new(array, index, json_incref(value));
  118. }
  119. const char *json_string_value(const json_t *string);
  120. int json_integer_value(const json_t *integer);
  121. double json_real_value(const json_t *real);
  122. double json_number_value(const json_t *json);
  123. int json_string_set(json_t *string, const char *value);
  124. int json_string_set_nocheck(json_t *string, const char *value);
  125. int json_integer_set(json_t *integer, int value);
  126. int json_real_set(json_t *real, double value);
  127. /* equality */
  128. int json_equal(json_t *value1, json_t *value2);
  129. /* copying */
  130. json_t *json_copy(json_t *value);
  131. json_t *json_deep_copy(json_t *value);
  132. /* loading, printing */
  133. #define JSON_ERROR_TEXT_LENGTH 160
  134. typedef struct {
  135. char text[JSON_ERROR_TEXT_LENGTH];
  136. int line;
  137. } json_error_t;
  138. json_t *json_loads(const char *input, json_error_t *error);
  139. json_t *json_loadf(FILE *input, json_error_t *error);
  140. json_t *json_load_file(const char *path, json_error_t *error);
  141. #define JSON_INDENT(n) (n & 0xFF)
  142. #define JSON_COMPACT 0x100
  143. #define JSON_ENSURE_ASCII 0x200
  144. #define JSON_SORT_KEYS 0x400
  145. #define JSON_PRESERVE_ORDER 0x800
  146. char *json_dumps(const json_t *json, unsigned long flags);
  147. int json_dumpf(const json_t *json, FILE *output, unsigned long flags);
  148. int json_dump_file(const json_t *json, const char *path, unsigned long flags);
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif