json.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __PJLIB_UTIL_JSON_H__
  19. #define __PJLIB_UTIL_JSON_H__
  20. /**
  21. * @file json.h
  22. * @brief PJLIB JSON Implementation
  23. */
  24. #include <pj/types.h>
  25. #include <pj/list.h>
  26. #include <pj/pool.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJ_JSON JSON Writer and Loader
  30. * @ingroup PJ_FILE_FMT
  31. * @{
  32. * This API implements JSON file format according to RFC 4627. It can be used
  33. * to parse, write, and manipulate JSON documents.
  34. */
  35. /**
  36. * Type of JSON value.
  37. */
  38. typedef enum pj_json_val_type
  39. {
  40. PJ_JSON_VAL_NULL, /**< Null value (null) */
  41. PJ_JSON_VAL_BOOL, /**< Boolean value (true, false) */
  42. PJ_JSON_VAL_NUMBER, /**< Numeric (float or fixed point) */
  43. PJ_JSON_VAL_STRING, /**< Literal string value. */
  44. PJ_JSON_VAL_ARRAY, /**< Array */
  45. PJ_JSON_VAL_OBJ /**< Object. */
  46. } pj_json_val_type;
  47. /* Forward declaration for JSON element */
  48. typedef struct pj_json_elem pj_json_elem;
  49. /**
  50. * JSON list to store child elements.
  51. */
  52. typedef struct pj_json_list
  53. {
  54. PJ_DECL_LIST_MEMBER(pj_json_elem);
  55. } pj_json_list;
  56. /**
  57. * This represents JSON element. A JSON element is basically a name/value
  58. * pair, where the name is a string and the value can be one of null, boolean
  59. * (true and false constants), number, string, array (containing zero or more
  60. * elements), or object. An object can be viewed as C struct, that is a
  61. * compound element containing other elements, each having name/value pair.
  62. */
  63. struct pj_json_elem
  64. {
  65. PJ_DECL_LIST_MEMBER(pj_json_elem);
  66. pj_str_t name; /**< ELement name. */
  67. pj_json_val_type type; /**< Element type. */
  68. union
  69. {
  70. pj_bool_t is_true; /**< Boolean value. */
  71. float num; /**< Number value. */
  72. pj_str_t str; /**< String value. */
  73. pj_json_list children; /**< Object and array children */
  74. } value; /**< Element value. */
  75. };
  76. /**
  77. * Structure to be specified to pj_json_parse() to be filled with additional
  78. * info when parsing failed.
  79. */
  80. typedef struct pj_json_err_info
  81. {
  82. unsigned line; /**< Line location of the error */
  83. unsigned col; /**< Column location of the error */
  84. int err_char; /**< The offending character. */
  85. } pj_json_err_info;
  86. /**
  87. * Type of function callback to write JSON document in pj_json_writef().
  88. *
  89. * @param s The string to be written to the document.
  90. * @param size The length of the string
  91. * @param user_data User data that was specified to pj_json_writef()
  92. *
  93. * @return If the callback returns non-PJ_SUCCESS, it will
  94. * stop the pj_json_writef() function and this error
  95. * will be returned to caller.
  96. */
  97. typedef pj_status_t (*pj_json_writer)(const char *s,
  98. unsigned size,
  99. void *user_data);
  100. /**
  101. * Initialize null element.
  102. *
  103. * @param el The element.
  104. * @param name Name to be given to the element, or NULL.
  105. */
  106. PJ_DECL(void) pj_json_elem_null(pj_json_elem *el, pj_str_t *name);
  107. /**
  108. * Initialize boolean element with the specified value.
  109. *
  110. * @param el The element.
  111. * @param name Name to be given to the element, or NULL.
  112. * @param val The value.
  113. */
  114. PJ_DECL(void) pj_json_elem_bool(pj_json_elem *el, pj_str_t *name,
  115. pj_bool_t val);
  116. /**
  117. * Initialize number element with the specified value.
  118. *
  119. * @param el The element.
  120. * @param name Name to be given to the element, or NULL.
  121. * @param val The value.
  122. */
  123. PJ_DECL(void) pj_json_elem_number(pj_json_elem *el, pj_str_t *name,
  124. float val);
  125. /**
  126. * Initialize string element with the specified value.
  127. *
  128. * @param el The element.
  129. * @param name Name to be given to the element, or NULL.
  130. * @param val The value.
  131. */
  132. PJ_DECL(void) pj_json_elem_string(pj_json_elem *el, pj_str_t *name,
  133. pj_str_t *val);
  134. /**
  135. * Initialize element as an empty array
  136. *
  137. * @param el The element.
  138. * @param name Name to be given to the element, or NULL.
  139. */
  140. PJ_DECL(void) pj_json_elem_array(pj_json_elem *el, pj_str_t *name);
  141. /**
  142. * Initialize element as an empty object
  143. *
  144. * @param el The element.
  145. * @param name Name to be given to the element, or NULL.
  146. */
  147. PJ_DECL(void) pj_json_elem_obj(pj_json_elem *el, pj_str_t *name);
  148. /**
  149. * Add an element to an object or array.
  150. *
  151. * @param el The object or array element.
  152. * @param child Element to be added to the object or array.
  153. */
  154. PJ_DECL(void) pj_json_elem_add(pj_json_elem *el, pj_json_elem *child);
  155. /**
  156. * Parse a JSON document in the buffer. The buffer MUST be NULL terminated,
  157. * or if not then it must have enough size to put the NULL character.
  158. *
  159. * @param pool The pool to allocate memory for creating elements.
  160. * @param buffer String buffer containing JSON document.
  161. * @param size Size of the document.
  162. * @param err_info Optional structure to be filled with info when
  163. * parsing failed.
  164. *
  165. * @return The root element from the document.
  166. */
  167. PJ_DECL(pj_json_elem*) pj_json_parse(pj_pool_t *pool,
  168. char *buffer,
  169. unsigned *size,
  170. pj_json_err_info *err_info);
  171. /**
  172. * Write the specified element to the string buffer.
  173. *
  174. * @param elem The element to be written.
  175. * @param buffer Output buffer.
  176. * @param size On input, it must be set to the size of the buffer.
  177. * Upon successful return, this will be set to
  178. * the length of the written string.
  179. *
  180. * @return PJ_SUCCESS on success or the appropriate error.
  181. */
  182. PJ_DECL(pj_status_t) pj_json_write(const pj_json_elem *elem,
  183. char *buffer, unsigned *size);
  184. /**
  185. * Incrementally write the element to arbitrary medium using the specified
  186. * callback to write the document chunks.
  187. *
  188. * @param elem The element to be written.
  189. * @param writer Callback function which will be called to write
  190. * text chunks.
  191. * @param user_data Arbitrary user data which will be given back when
  192. * calling the callback.
  193. *
  194. * @return PJ_SUCCESS on success or the appropriate error.
  195. */
  196. PJ_DECL(pj_status_t) pj_json_writef(const pj_json_elem *elem,
  197. pj_json_writer writer,
  198. void *user_data);
  199. /**
  200. * @}
  201. */
  202. PJ_END_DECL
  203. #endif /* __PJLIB_UTIL_JSON_H__ */