json.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #ifndef _ASTERISK_JSON_H
  19. #define _ASTERISK_JSON_H
  20. #include "asterisk/netsock2.h"
  21. /*! \file
  22. *
  23. * \brief Asterisk JSON abstraction layer.
  24. * \since 12.0.0
  25. *
  26. * This is a very thin wrapper around the Jansson API. For more details on it,
  27. * see its docs at http://www.digip.org/jansson/doc/2.4/apiref.html.
  28. *
  29. * Rather than provide the multiple ways of doing things that the Jansson API
  30. * does, the Asterisk wrapper is always reference-stealing, and always NULL
  31. * safe.
  32. *
  33. * And by always, I mean that the reference is stolen even if the function
  34. * fails. This avoids lots of conditional logic, and also avoids having to track
  35. * zillions of local variables when building complex JSON objects. You can
  36. * instead chain \c ast_json_* calls together safely and only worry about
  37. * cleaning up the root object.
  38. *
  39. * In the cases where you have a need to introduce intermediate objects, just
  40. * wrap them with json_ref() when passing them to other \c ast_json_*()
  41. * functions.
  42. *
  43. * \par Thread Safety
  44. *
  45. * Jansson (as of 2.4) provides fairly weak thread safety guarantees. The
  46. * Asterisk wrapper improves upon that slightly. The remaining refcounting
  47. * problems are issues when slicing/sharing/mixing instances between JSON
  48. * objects and arrays, which we avoid.
  49. *
  50. * The \c ast_json_dump_* functions are thread safe for multiple concurrent
  51. * dumps of the same object, so long as the concurrent dumps start from the same
  52. * \c root object. But if an object is shared by other JSON objects/arrays, then
  53. * concurrent dumps of the outer objects/arrays are not thread safe. This can be
  54. * avoided by using ast_json_deep_copy() when sharing JSON instances between
  55. * objects.
  56. *
  57. * The ast_json_ref() and ast_json_unref() functions are thread safe. Since the
  58. * Asterisk wrapper exclusively uses the reference stealing API, Jansson won't
  59. * be performing many refcount modifications behind our backs. There are a few
  60. * exceptions.
  61. *
  62. * The first is the transitive json_decref() that occurs when \ref
  63. * AST_JSON_OBJECT and \ref AST_JSON_ARRAY instances are deleted. This can be
  64. * avoided by using ast_json_deep_copy() when sharing JSON instances between
  65. * objects.
  66. *
  67. * The second is when using the reference borrowing specifier in
  68. * ast_json_pack() (capital \c O). This can be avoided by using the reference
  69. * stealing specifier (lowercase \c o) and wrapping the JSON object parameter
  70. * with ast_json_ref() for an explicit ref-bump.
  71. *
  72. * \par Example code
  73. *
  74. * \code
  75. * // Example of how to use the Asterisk JSON API
  76. * static struct ast_json *foo(void) {
  77. * RAII_VAR(struct ast_json *, array, NULL, ast_json_unref);
  78. * RAII_VAR(struct ast_json *, obj, NULL, ast_json_unref);
  79. * int i, res;
  80. *
  81. * array = ast_json_array_create();
  82. * if (!array) { return NULL; }
  83. *
  84. * for (i = 0; i < 10; ++i) {
  85. * // NULL safety and object stealing means calls can
  86. * // be chained together directly.
  87. * res = ast_json_array_append(array,
  88. * ast_json_integer_create(i));
  89. * if (res != 0) { return NULL; }
  90. * }
  91. *
  92. * obj = ast_json_object_create();
  93. * if (!obj) { return NULL; }
  94. *
  95. * // If you already have an object reference, ast_json_ref()
  96. * // can be used inline to bump the ref before passing it along
  97. * // to a ref-stealing call
  98. * res = ast_json_object_set(obj, "foo", ast_json_ref(array));
  99. * if (!res) { return NULL; }
  100. *
  101. * return obj;
  102. * }
  103. * \endcode
  104. *
  105. * \author David M. Lee, II <dlee@digium.com>
  106. */
  107. /*!@{*/
  108. /*!
  109. * \brief Primarily used to cast when packing to an "I" type.
  110. */
  111. typedef AST_JSON_INT_T ast_json_int_t;
  112. /*!
  113. * \brief Initialize the JSON library.
  114. */
  115. void ast_json_init(void);
  116. /*!
  117. * \brief Set custom allocators instead of the standard ast_malloc() and ast_free().
  118. * \since 12.0.0
  119. *
  120. * This is used by the unit tests to do JSON specific memory leak detection. Since it
  121. * affects all users of the JSON library, shouldn't normally be used.
  122. *
  123. * \param malloc_fn Custom allocation function.
  124. * \param free_fn Matching free function.
  125. */
  126. void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*));
  127. /*!
  128. * \brief Change alloc funcs back to the resource module defaults.
  129. * \since 12.0.0
  130. *
  131. * If you use ast_json_set_alloc_funcs() to temporarily change the allocator functions
  132. * (i.e., from in a unit test), this function sets them back to ast_malloc() and
  133. * ast_free().
  134. */
  135. void ast_json_reset_alloc_funcs(void);
  136. /*!
  137. * \brief Asterisk's custom JSON allocator. Exposed for use by unit tests.
  138. * \since 12.0.0
  139. * \internal
  140. */
  141. void *ast_json_malloc(size_t size);
  142. /*!
  143. * \brief Asterisk's custom JSON allocator. Exposed for use by unit tests.
  144. * \since 12.0.0
  145. * \internal
  146. */
  147. void ast_json_free(void *p);
  148. /*!
  149. * \struct ast_json
  150. * \brief Abstract JSON element (object, array, string, int, ...).
  151. * \since 12.0.0
  152. */
  153. struct ast_json;
  154. /*!
  155. * \brief Increase refcount on \a value.
  156. * \since 12.0.0
  157. *
  158. * \param value JSON value to reference.
  159. * \return The given \a value.
  160. */
  161. struct ast_json *ast_json_ref(struct ast_json *value);
  162. /*!
  163. * \brief Decrease refcount on \a value. If refcount reaches zero, \a value is freed.
  164. * \since 12.0.0
  165. *
  166. * \note It is safe to pass \c NULL to this function.
  167. */
  168. void ast_json_unref(struct ast_json *value);
  169. /*!@}*/
  170. /*!@{*/
  171. /*!
  172. * \brief Valid types of a JSON element.
  173. * \since 12.0.0
  174. */
  175. enum ast_json_type
  176. {
  177. AST_JSON_OBJECT,
  178. AST_JSON_ARRAY,
  179. AST_JSON_STRING,
  180. AST_JSON_INTEGER,
  181. AST_JSON_REAL,
  182. AST_JSON_TRUE,
  183. AST_JSON_FALSE,
  184. AST_JSON_NULL,
  185. };
  186. /*!
  187. * \brief Get the type of \a value.
  188. * \since 12.0.0
  189. * \param value Value to query.
  190. * \return Type of \a value.
  191. */
  192. enum ast_json_type ast_json_typeof(const struct ast_json *value);
  193. /*!
  194. * \brief Get the string name for the given type.
  195. * \since 12.0.0
  196. * \param type Type to convert to string.
  197. * \return Simple string for the type name (object, array, string, etc.)
  198. * \return \c "?" for invalid types.
  199. */
  200. const char *ast_json_typename(enum ast_json_type type);
  201. /*!@}*/
  202. /*!@{*/
  203. /*!
  204. * \brief Check the string of the given length for UTF-8 format.
  205. * \since 13.12.0
  206. *
  207. * \param str String to check.
  208. * \param len Length of string to check.
  209. *
  210. * \retval 0 if not UTF-8 encoded or str is NULL.
  211. * \retval 1 if UTF-8 encoded.
  212. */
  213. int ast_json_utf8_check_len(const char *str, size_t len);
  214. /*!
  215. * \brief Check the nul terminated string for UTF-8 format.
  216. * \since 13.12.0
  217. *
  218. * \param str String to check.
  219. *
  220. * \retval 0 if not UTF-8 encoded or str is NULL.
  221. * \retval 1 if UTF-8 encoded.
  222. */
  223. int ast_json_utf8_check(const char *str);
  224. /*!
  225. * \brief Check str for UTF-8 and replace with an empty string if fails the check.
  226. *
  227. * \note The convenience macro is normally used with ast_json_pack()
  228. * or a function wrapper that calls ast_json_vpack().
  229. */
  230. #define AST_JSON_UTF8_VALIDATE(str) (ast_json_utf8_check(str) ? (str) : "")
  231. /*!@}*/
  232. /*!@{*/
  233. /*!
  234. * \brief Get the JSON true value.
  235. * \since 12.0.0
  236. *
  237. * The returned value is a singleton, and does not need to be
  238. * ast_json_unref()'ed.
  239. *
  240. * \return JSON true.
  241. */
  242. struct ast_json *ast_json_true(void);
  243. /*!
  244. * \brief Get the JSON false value.
  245. * \since 12.0.0
  246. *
  247. * The returned value is a singleton, and does not need to be
  248. * ast_json_unref()'ed.
  249. *
  250. * \return JSON false.
  251. */
  252. struct ast_json *ast_json_false(void);
  253. /*!
  254. * \brief Get the JSON boolean corresponding to \a value.
  255. * \since 12.0.0
  256. * \return JSON true if value is true (non-zero).
  257. * \return JSON false if value is false (zero).
  258. */
  259. struct ast_json *ast_json_boolean(int value);
  260. /*!
  261. * \brief Get the JSON null value.
  262. * \since 12.0.0
  263. *
  264. * The returned value is a singleton, and does not need to be
  265. * ast_json_unref()'ed.
  266. *
  267. * \return JSON null.
  268. */
  269. struct ast_json *ast_json_null(void);
  270. /*!
  271. * \brief Check if \a value is JSON true.
  272. * \since 12.0.0
  273. * \return True (non-zero) if \a value == \ref ast_json_true().
  274. * \return False (zero) otherwise..
  275. */
  276. int ast_json_is_true(const struct ast_json *value);
  277. /*!
  278. * \brief Check if \a value is JSON false.
  279. * \since 12.0.0
  280. * \return True (non-zero) if \a value == \ref ast_json_false().
  281. * \return False (zero) otherwise.
  282. */
  283. int ast_json_is_false(const struct ast_json *value);
  284. /*!
  285. * \brief Check if \a value is JSON null.
  286. * \since 12.0.0
  287. * \return True (non-zero) if \a value == \ref ast_json_false().
  288. * \return False (zero) otherwise.
  289. */
  290. int ast_json_is_null(const struct ast_json *value);
  291. /*!@}*/
  292. /*!@{*/
  293. /*!
  294. * \brief Construct a JSON string from \a value.
  295. * \since 12.0.0
  296. *
  297. * The given \a value must be a valid ASCII or UTF-8 encoded string.
  298. *
  299. * \param value Value of new JSON string.
  300. * \return Newly constructed string element.
  301. * \return \c NULL on error.
  302. */
  303. struct ast_json *ast_json_string_create(const char *value);
  304. /*!
  305. * \brief Get the value of a JSON string.
  306. * \since 12.0.0
  307. * \param string JSON string.
  308. * \return Value of the string.
  309. * \return \c NULL on error.
  310. */
  311. const char *ast_json_string_get(const struct ast_json *string);
  312. /*!
  313. * \brief Change the value of a JSON string.
  314. * \since 12.0.0
  315. *
  316. * The given \a value must be a valid ASCII or UTF-8 encoded string.
  317. *
  318. * \param string JSON string to modify.
  319. * \param value New value to store in \a string.
  320. * \return 0 on success.
  321. * \return -1 on error.
  322. */
  323. int ast_json_string_set(struct ast_json *string, const char *value);
  324. /*!
  325. * \brief Create a JSON string, printf style.
  326. * \since 12.0.0
  327. *
  328. * The formatted value must be a valid ASCII or UTF-8 encoded string.
  329. *
  330. * \param format \c printf style format string.
  331. * \return Newly allocated string.
  332. * \return \c NULL on error.
  333. */
  334. struct ast_json *ast_json_stringf(const char *format, ...) __attribute__((format(printf, 1, 2)));
  335. /*!
  336. * \brief Create a JSON string, vprintf style.
  337. * \since 12.0.0
  338. *
  339. * The formatted value must be a valid ASCII or UTF-8 encoded string.
  340. *
  341. * \param format \c printf style format string.
  342. * \return Newly allocated string.
  343. * \return \c NULL on error.
  344. */
  345. struct ast_json *ast_json_vstringf(const char *format, va_list args) __attribute__((format(printf, 1, 0)));
  346. /*!@}*/
  347. /*!@{*/
  348. /*!
  349. * \brief Create a JSON integer.
  350. * \since 12.0.0
  351. * \param value Value of the new JSON integer.
  352. * \return Newly allocated integer.
  353. * \return \c NULL on error.
  354. */
  355. struct ast_json *ast_json_integer_create(intmax_t value);
  356. /*!
  357. * \brief Get the value from a JSON integer.
  358. * \since 12.0.0
  359. * \param integer JSON integer.
  360. * \return Value of a JSON integer.
  361. * \return 0 if \a integer is not a JSON integer.
  362. */
  363. intmax_t ast_json_integer_get(const struct ast_json *integer);
  364. /*!
  365. * \brief Set the value of a JSON integer.
  366. * \since 12.0.0
  367. * \param integer JSON integer to modify.
  368. * \param value New value for \a integer.
  369. * \return 0 on success.
  370. * \return -1 on error.
  371. */
  372. int ast_json_integer_set(struct ast_json *integer, intmax_t value);
  373. /*!
  374. * \brief Create a JSON real number.
  375. * \since 12.0.0
  376. * \param value Value of the new JSON real number.
  377. * \return Newly allocated real number.
  378. * \return \c NULL on error.
  379. */
  380. struct ast_json *ast_json_real_create(double value);
  381. /*!
  382. * \brief Get the value from a JSON real number.
  383. * \since 12.0.0
  384. * \param real JSON real number.
  385. * \return Value of a JSON real number.
  386. * \return 0 if \a real is not a JSON real number.
  387. */
  388. double ast_json_real_get(const struct ast_json *real);
  389. /*!
  390. * \brief Set the value of a JSON real number.
  391. * \since 12.0.0
  392. * \param integer JSON real number to modify.
  393. * \param value New value for \a real.
  394. * \return 0 on success.
  395. * \return -1 on error.
  396. */
  397. int ast_json_real_set(struct ast_json *real, double value);
  398. /*!@}*/
  399. /*!@{*/
  400. /*!
  401. * \brief Create a empty JSON array.
  402. * \since 12.0.0
  403. * \return Newly allocated array.
  404. * \return \c NULL on error.
  405. */
  406. struct ast_json *ast_json_array_create(void);
  407. /*!
  408. * \brief Get the size of a JSON array.
  409. * \since 12.0.0
  410. * \param array JSON array.
  411. * \return Size of \a array.
  412. * \return 0 if array is not a JSON array.
  413. */
  414. size_t ast_json_array_size(const struct ast_json *array);
  415. /*!
  416. * \brief Get an element from an array.
  417. * \since 12.0.0
  418. *
  419. * The returned element is a borrowed reference; use ast_json_ref() to safely keep a
  420. * pointer to it.
  421. *
  422. * \param array JSON array.
  423. * \param index Zero-based index into \a array.
  424. * \return The specified element.
  425. * \return \c NULL if \a array not an array.
  426. * \return \c NULL if \a index is out of bounds.
  427. */
  428. struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index);
  429. /*!
  430. * \brief Change an element in an array.
  431. * \since 12.0.0
  432. *
  433. * \note The \a array steals the \a value reference even if it returns error;
  434. * use ast_json_ref() to safely keep a pointer to it.
  435. *
  436. * \param array JSON array to modify.
  437. * \param index Zero-based index into array.
  438. * \param value New JSON value to store in \a array at \a index.
  439. * \return 0 on success.
  440. * \return -1 on error.
  441. */
  442. int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value);
  443. /*!
  444. * \brief Append to an array.
  445. * \since 12.0.0
  446. *
  447. * \note The \a array steals the \a value reference even if it returns error;
  448. * use ast_json_ref() to safely keep a pointer to it.
  449. *
  450. * \param array JSON array to modify.
  451. * \param value New JSON value to store at the end of \a array.
  452. * \return 0 on success.
  453. * \return -1 on error.
  454. */
  455. int ast_json_array_append(struct ast_json *array, struct ast_json *value);
  456. /*!
  457. * \brief Insert into an array.
  458. * \since 12.0.0
  459. *
  460. * \note The \a array steals the \a value reference even if it returns error;
  461. * use ast_json_ref() to safely keep a pointer to it.
  462. *
  463. * \param array JSON array to modify.
  464. * \param index Zero-based index into array.
  465. * \param value New JSON value to store in \a array at \a index.
  466. * \return 0 on success.
  467. * \return -1 on error.
  468. */
  469. int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value);
  470. /*!
  471. * \brief Remove an element from an array.
  472. * \since 12.0.0
  473. * \param array JSON array to modify.
  474. * \param index Zero-based index into array.
  475. * \return 0 on success.
  476. * \return -1 on error.
  477. */
  478. int ast_json_array_remove(struct ast_json *array, size_t index);
  479. /*!
  480. * \brief Remove all elements from an array.
  481. * \since 12.0.0
  482. * \param array JSON array to clear.
  483. * \return 0 on success.
  484. * \return -1 on error.
  485. */
  486. int ast_json_array_clear(struct ast_json *array);
  487. /*!
  488. * \brief Append all elements from \a tail to \a array.
  489. * \since 12.0.0
  490. *
  491. * The \a tail argument is not changed, so ast_json_unref() it when you are done with it.
  492. *
  493. * \param array JSON array to modify.
  494. * \param tail JSON array with contents to append to \a array.
  495. * \return 0 on success.
  496. * \return -1 on error.
  497. */
  498. int ast_json_array_extend(struct ast_json *array, struct ast_json *tail);
  499. /*!@}*/
  500. /*!@{*/
  501. /*!
  502. * \brief Create a new JSON object.
  503. * \since 12.0.0
  504. * \return Newly allocated object.
  505. * \return \c NULL on error.
  506. */
  507. struct ast_json *ast_json_object_create(void);
  508. /*!
  509. * \brief Get size of JSON object.
  510. * \since 12.0.0
  511. * \param object JSON object.
  512. * \return Size of \a object.
  513. * \return Zero of \a object is not a JSON object.
  514. */
  515. size_t ast_json_object_size(struct ast_json *object);
  516. /*!
  517. * \brief Get a field from a JSON object.
  518. * \since 12.0.0
  519. *
  520. * The returned element is a borrowed reference; use ast_json_ref() to safely keep a
  521. * pointer to it.
  522. *
  523. * \param object JSON object.
  524. * \param key Key of field to look up.
  525. * \return Value with given \a key.
  526. * \return \c NULL on error.
  527. */
  528. struct ast_json *ast_json_object_get(struct ast_json *object, const char *key);
  529. /*!
  530. * \brief Set a field in a JSON object.
  531. * \since 12.0.0
  532. *
  533. * \note The object steals the \a value reference even if it returns error;
  534. * use ast_json_ref() to safely keep a pointer to it.
  535. *
  536. * \param object JSON object to modify.
  537. * \param key Key of field to set.
  538. * \param value JSON value to set for field.
  539. * \return 0 on success.
  540. * \return -1 on error.
  541. */
  542. int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value);
  543. /*!
  544. * \brief Delete a field from a JSON object.
  545. * \since 12.0.0
  546. *
  547. * \param object JSON object to modify.
  548. * \param key Key of field to delete.
  549. * \return 0 on success, or -1 if key does not exist.
  550. */
  551. int ast_json_object_del(struct ast_json *object, const char *key);
  552. /*!
  553. * \brief Delete all elements from a JSON object.
  554. * \since 12.0.0
  555. * \param object JSON object to clear.
  556. * \return 0 on success.
  557. * \return -1 on error.
  558. */
  559. int ast_json_object_clear(struct ast_json *object);
  560. /*!
  561. * \brief Update \a object with all of the fields of \a other.
  562. * \since 12.0.0
  563. *
  564. * All of the fields of \a other are copied into \a object, overwriting existing keys.
  565. * The \a other object is not changed, so ast_json_unref() it when you are done with it.
  566. *
  567. * \param object JSON object to modify.
  568. * \param other JSON object to copy into \a object.
  569. * \return 0 on success.
  570. * \return -1 on error.
  571. */
  572. int ast_json_object_update(struct ast_json *object, struct ast_json *other);
  573. /*!
  574. * \brief Update existing fields in \a object with the fields of \a other.
  575. * \since 12.0.0
  576. *
  577. * Like ast_json_object_update(), but only existing fields are updated. No new fields
  578. * will get added. The \a other object is not changed, so ast_json_unref() it when you
  579. * are done with it.
  580. *
  581. * \param object JSON object to modify.
  582. * \param other JSON object to copy into \a object.
  583. * \return 0 on success.
  584. * \return -1 on error.
  585. */
  586. int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other);
  587. /*!
  588. * \brief Add new fields to \a object with the fields of \a other.
  589. * \since 12.0.0
  590. *
  591. * Like ast_json_object_update(), but only missing fields are added. No existing fields
  592. * will be modified. The \a other object is not changed, so ast_json_unref() it when you
  593. * are done with it.
  594. *
  595. * \param object JSON object to modify.
  596. * \param other JSON object to copy into \a object.
  597. * \return 0 on success.
  598. * \return -1 on error.
  599. */
  600. int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other);
  601. /*!
  602. * \struct ast_json_iter
  603. * \brief Iterator for JSON object key/values.
  604. * \since 12.0.0
  605. *
  606. * Note that iteration order is not specified, and may change as fields are added to
  607. * and removed from the object.
  608. */
  609. struct ast_json_iter;
  610. /*!
  611. * \brief Get an iterator pointing to the first field in a JSON object.
  612. * \since 12.0.0
  613. *
  614. * The order of the fields in an object are not specified. However, iterating forward
  615. * from this iterator will cover all fields in \a object. Adding or removing fields from
  616. * \a object may invalidate its iterators.
  617. *
  618. * \param object JSON object.
  619. * \return Iterator to the first field in \a object.
  620. * \return \c NULL \a object is empty.
  621. * \return \c NULL on error.
  622. */
  623. struct ast_json_iter *ast_json_object_iter(struct ast_json *object);
  624. /*!
  625. * \brief Get an iterator pointing to a specified \a key in \a object.
  626. * \since 12.0.0
  627. *
  628. * Iterating forward from this iterator may not to cover all elements in \a object.
  629. *
  630. * \param object JSON object to iterate.
  631. * \param key Key of field to lookup.
  632. * \return Iterator pointing to the field with the given \a key.
  633. * \return \c NULL if \a key does not exist.
  634. * \return \c NULL on error.
  635. */
  636. struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key);
  637. /*!
  638. * \brief Get the next iterator.
  639. * \since 12.0.0
  640. * \param object JSON object \a iter was obtained from.
  641. * \param iter JSON object iterator.
  642. * \return Iterator to next field in \a object.
  643. * \return \c NULL if \a iter was the last field.
  644. */
  645. struct ast_json_iter *ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter);
  646. /*!
  647. * \brief Get the key from an iterator.
  648. * \since 12.0.0
  649. * \param iter JSON object iterator.
  650. * \return Key of the field \a iter points to.
  651. */
  652. const char *ast_json_object_iter_key(struct ast_json_iter *iter);
  653. /*!
  654. * \brief Get the value from an iterator.
  655. * \since 12.0.0
  656. *
  657. * The returned element is a borrowed reference; use ast_json_ref() to safely
  658. * keep a pointer to it.
  659. *
  660. * \param iter JSON object iterator.
  661. * \return Value of the field \a iter points to.
  662. */
  663. struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter);
  664. /*!
  665. * \brief Set the value of the field pointed to by an iterator.
  666. * \since 12.0.0
  667. *
  668. * \note The object steals the \a value reference even if it returns error;
  669. * use ast_json_ref() to safely keep a pointer to it.
  670. *
  671. * \param object JSON object \a iter was obtained from.
  672. * \param iter JSON object iterator.
  673. * \param value JSON value to store in \iter's field.
  674. * \return 0 on success.
  675. * \return -1 on error.
  676. */
  677. int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value);
  678. /*!@}*/
  679. /*!@{*/
  680. /*!
  681. * \brief Encoding format type.
  682. * \since 12.0.0
  683. */
  684. enum ast_json_encoding_format
  685. {
  686. /*! Compact format, low human readability */
  687. AST_JSON_COMPACT,
  688. /*! Formatted for human readability */
  689. AST_JSON_PRETTY,
  690. };
  691. /*!
  692. * \brief Encode a JSON value to a compact string.
  693. * \since 12.0.0
  694. *
  695. * Returned string must be freed by calling ast_json_free().
  696. *
  697. * \param root JSON value.
  698. * \return String encoding of \a root.
  699. * \return \c NULL on error.
  700. */
  701. #define ast_json_dump_string(root) ast_json_dump_string_format(root, AST_JSON_COMPACT)
  702. /*!
  703. * \brief Encode a JSON value to a string.
  704. * \since 12.0.0
  705. *
  706. * Returned string must be freed by calling ast_json_free().
  707. *
  708. * \param root JSON value.
  709. * \param format encoding format type.
  710. * \return String encoding of \a root.
  711. * \return \c NULL on error.
  712. */
  713. char *ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format);
  714. #define ast_json_dump_str(root, dst) ast_json_dump_str_format(root, dst, AST_JSON_COMPACT)
  715. /*!
  716. * \brief Encode a JSON value to an \ref ast_str.
  717. * \since 12.0.0
  718. *
  719. * If \a dst is too small, it will be grown as needed.
  720. *
  721. * \param root JSON value.
  722. * \param dst \ref ast_str to store JSON encoding.
  723. * \param format encoding format type.
  724. * \return 0 on success.
  725. * \return -1 on error. The contents of \a dst are undefined.
  726. */
  727. int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format);
  728. #define ast_json_dump_file(root, output) ast_json_dump_file_format(root, output, AST_JSON_COMPACT)
  729. /*!
  730. * \brief Encode a JSON value to a \c FILE.
  731. * \since 12.0.0
  732. *
  733. * \param root JSON value.
  734. * \param output File to write JSON encoding to.
  735. * \param format encoding format type.
  736. * \return 0 on success.
  737. * \return -1 on error. The contents of \a output are undefined.
  738. */
  739. int ast_json_dump_file_format(struct ast_json *root, FILE *output, enum ast_json_encoding_format format);
  740. #define ast_json_dump_new_file(root, path) ast_json_dump_new_file_format(root, path, AST_JSON_COMPACT)
  741. /*!
  742. * \brief Encode a JSON value to a file at the given location.
  743. * \since 12.0.0
  744. *
  745. * \param root JSON value.
  746. * \param path Path to file to write JSON encoding to.
  747. * \param format encoding format type.
  748. * \return 0 on success.
  749. * \return -1 on error. The contents of \a output are undefined.
  750. */
  751. int ast_json_dump_new_file_format(struct ast_json *root, const char *path, enum ast_json_encoding_format format);
  752. #define AST_JSON_ERROR_TEXT_LENGTH 160
  753. #define AST_JSON_ERROR_SOURCE_LENGTH 80
  754. /*!
  755. * \brief JSON parsing error information.
  756. * \since 12.0.0
  757. */
  758. struct ast_json_error {
  759. /*! Line number error occured on */
  760. int line;
  761. /*! Character (not byte, can be different for UTF-8) column on which the error occurred. */
  762. int column;
  763. /*! Position in bytes from start of input */
  764. int position;
  765. /*! Error message */
  766. char text[AST_JSON_ERROR_TEXT_LENGTH];
  767. /*! Source of the error (filename or <string>) */
  768. char source[AST_JSON_ERROR_TEXT_LENGTH];
  769. };
  770. /*!
  771. * \brief Parse null terminated string into a JSON object or array.
  772. * \since 12.0.0
  773. * \param input String to parse.
  774. * \param[out] error Filled with information on error.
  775. * \return Parsed JSON element.
  776. * \return \c NULL on error.
  777. */
  778. struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error);
  779. /*!
  780. * \brief Parse \ref ast_str into a JSON object or array.
  781. * \since 12.0.0
  782. * \param input \ref ast_str to parse.
  783. * \param[out] error Filled with information on error.
  784. * \return Parsed JSON element.
  785. * \return \c NULL on error.
  786. */
  787. struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error);
  788. /*!
  789. * \brief Parse buffer with known length into a JSON object or array.
  790. * \since 12.0.0
  791. * \param buffer Buffer to parse.
  792. * \param buflen Length of \a buffer.
  793. * \param[out] error Filled with information on error.
  794. * \return Parsed JSON element.
  795. * \return \c NULL on error.
  796. */
  797. struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error);
  798. /*!
  799. * \brief Parse a \c FILE into JSON object or array.
  800. * \since 12.0.0
  801. * \param input \c FILE to parse.
  802. * \param[out] error Filled with information on error.
  803. * \return Parsed JSON element.
  804. * \return \c NULL on error.
  805. */
  806. struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *error);
  807. /*!
  808. * \brief Parse file at \a path into JSON object or array.
  809. * \since 12.0.0
  810. * \param path Path of file to parse.
  811. * \param[out] error Filled with information on error.
  812. * \return Parsed JSON element.
  813. * \return \c NULL on error.
  814. */
  815. struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *error);
  816. /*!
  817. * \brief Helper for creating complex JSON values.
  818. * \since 12.0.0
  819. *
  820. * See original Jansson docs at http://www.digip.org/jansson/doc/2.4/apiref.html#apiref-pack
  821. * for more details.
  822. */
  823. struct ast_json *ast_json_pack(char const *format, ...);
  824. /*!
  825. * \brief Helper for creating complex JSON values simply.
  826. * \since 12.0.0
  827. *
  828. * See original Jansson docs at http://www.digip.org/jansson/doc/2.4/apiref.html#apiref-pack
  829. * for more details.
  830. */
  831. struct ast_json *ast_json_vpack(char const *format, va_list ap);
  832. /*!@}*/
  833. /*!@{*/
  834. /*!
  835. * \brief Compare two JSON objects.
  836. * \since 12.0.0
  837. *
  838. * Two JSON objects are equal if they are of the same type, and their contents are equal.
  839. *
  840. * \param lhs Value to compare.
  841. * \param rhs Other value to compare.
  842. * \return True (non-zero) if \a lhs and \a rhs are equal.
  843. * \return False (zero) if they are not.
  844. */
  845. int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs);
  846. /*!
  847. * \brief Copy a JSON value, but not its children.
  848. * \since 12.0.0
  849. *
  850. * If \a value is a JSON object or array, its children are shared with the returned copy.
  851. *
  852. * \param value JSON value to copy.
  853. * \return Shallow copy of \a value.
  854. * \return \c NULL on error.
  855. */
  856. struct ast_json *ast_json_copy(const struct ast_json *value);
  857. /*!
  858. * \brief Copy a JSON value, and its children.
  859. * \since 12.0.0
  860. *
  861. * If \a value is a JSON object or array, they are also copied.
  862. *
  863. * \param value JSON value to copy.
  864. * \return Deep copy of \a value.
  865. * \return \c NULL on error.
  866. */
  867. struct ast_json *ast_json_deep_copy(const struct ast_json *value);
  868. /*!@}*/
  869. /*!@{*/
  870. /*!
  871. * \brief Common JSON rendering functions for common 'objects'.
  872. */
  873. /*!
  874. * \brief Simple name/number pair.
  875. * \param name Name
  876. * \param number Number
  877. * \return NULL if error (non-UTF8 characters, NULL inputs, etc.)
  878. * \return JSON object with name and number fields
  879. */
  880. struct ast_json *ast_json_name_number(const char *name, const char *number);
  881. /*!
  882. * \brief Construct a timeval as JSON.
  883. *
  884. * JSON does not define a standard date format (boo), but the de facto standard
  885. * is to use ISO 8601 formatted string. We build a millisecond resolution string
  886. * from the \c timeval
  887. *
  888. * \param tv \c timeval to encode.
  889. * \param zone Text string of a standard system zoneinfo file. If NULL, the system localtime will be used.
  890. * \return JSON string with ISO 8601 formatted date/time.
  891. * \return \c NULL on error.
  892. */
  893. struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone);
  894. /*!
  895. * \brief Construct an IP address as JSON
  896. *
  897. * XXX some comments describing the need for this here
  898. *
  899. * \param addr ast_sockaddr to encode
  900. * \param transport_type ast_transport to include in the address string if any. Should just be one.
  901. * \return JSON string containing the IP address with optional transport information
  902. * \return \c NULL on error.
  903. */
  904. struct ast_json *ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type);
  905. /*!
  906. * \brief Construct a context/exten/priority as JSON.
  907. *
  908. * If a \c NULL is passed for \c context or \c exten, or -1 for \c priority,
  909. * the fields is set to ast_json_null().
  910. *
  911. * \param context Context name.
  912. * \param exten Extension.
  913. * \param priority Dialplan priority.
  914. * \return JSON object with \c context, \c exten and \c priority fields
  915. */
  916. struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority);
  917. struct ast_json_payload {
  918. struct ast_json *json;
  919. };
  920. /*!
  921. * \brief Create an ao2 object to pass json blobs as data payloads for stasis
  922. * \since 12.0.0
  923. *
  924. * \param json the ast_json blob we are loading
  925. *
  926. * \retval NULL if we fail to alloc it
  927. * \retval pointer to the ast_json_payload created
  928. */
  929. struct ast_json_payload *ast_json_payload_create(struct ast_json *json);
  930. struct ast_party_id;
  931. /*!
  932. * \brief Construct an ast_party_id as JSON.
  933. * \since 12.0.0
  934. *
  935. * \param party The party ID to represent as JSON.
  936. *
  937. * \return JSON object with \c name, \c number and \c subaddress objects
  938. * for those that are valid in the party ID
  939. */
  940. struct ast_json *ast_json_party_id(struct ast_party_id *party);
  941. enum ast_json_to_ast_vars_code {
  942. /*! \brief Conversion successful */
  943. AST_JSON_TO_AST_VARS_CODE_SUCCESS,
  944. /*!
  945. * \brief Conversion failed because invalid value type supplied.
  946. * \note Only string values allowed.
  947. */
  948. AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE,
  949. /*! \brief Conversion failed because of allocation failure. (Out Of Memory) */
  950. AST_JSON_TO_AST_VARS_CODE_OOM,
  951. };
  952. /*!
  953. * \brief Convert a \c ast_json list of key/value pair tuples into a \c ast_variable list
  954. * \since 12.5.0
  955. *
  956. * \param json_variables The JSON blob containing the variable
  957. * \param variables An out reference to the variables to populate.
  958. *
  959. * \code
  960. * struct ast_json *json_variables = ast_json_pack("[ { s: s } ]", "foo", "bar");
  961. * struct ast_variable *variables = NULL;
  962. * int res;
  963. *
  964. * res = ast_json_to_ast_variables(json_variables, &variables);
  965. * \endcode
  966. *
  967. * \return Conversion enum ast_json_to_ast_vars_code status
  968. */
  969. enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables);
  970. /*!@}*/
  971. #endif /* _ASTERISK_JSON_H */