json.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. /*! \file
  19. *
  20. * \brief JSON abstraction layer.
  21. *
  22. * This is a very thin wrapper around the Jansson API. For more details on it,
  23. * see its docs at http://www.digip.org/jansson/doc/2.4/apiref.html.
  24. *
  25. * \author David M. Lee, II <dlee@digium.com>
  26. */
  27. /*** MODULEINFO
  28. <depend>jansson</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/json.h"
  34. #include "asterisk/localtime.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/astobj2.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/callerid.h"
  40. #include <jansson.h>
  41. #include <time.h>
  42. /*! \brief Magic number, for safety checks. */
  43. #define JSON_MAGIC 0x1541992
  44. /*! \brief Internal structure for allocated memory blocks */
  45. struct json_mem {
  46. /*! Magic number, for safety checks */
  47. uint32_t magic;
  48. /*! Mutext for locking this memory block */
  49. ast_mutex_t mutex;
  50. /*! Linked list pointer for the free list */
  51. AST_LIST_ENTRY(json_mem) list;
  52. /*! Data section of the allocation; void pointer for proper alignment */
  53. void *data[];
  54. };
  55. /*! \brief Free a \ref json_mem block. */
  56. static void json_mem_free(struct json_mem *mem)
  57. {
  58. mem->magic = 0;
  59. ast_mutex_destroy(&mem->mutex);
  60. ast_free(mem);
  61. }
  62. /*!
  63. * \brief Get the \ref json_mem block for a pointer allocated via
  64. * ast_json_malloc().
  65. *
  66. * This function properly handles Jansson singletons (null, true, false), and
  67. * \c NULL.
  68. *
  69. * \param p Pointer, usually to a \c json_t or \ref ast_json.
  70. * \return \ref json_mem object with extra allocation info.
  71. */
  72. static inline struct json_mem *to_json_mem(void *p)
  73. {
  74. struct json_mem *mem;
  75. /* Avoid ref'ing the singleton values */
  76. if (p == NULL || p == json_null() || p == json_true() ||
  77. p == json_false()) {
  78. return NULL;
  79. }
  80. mem = (struct json_mem *)((char *) (p) - sizeof(*mem));
  81. ast_assert(mem->magic == JSON_MAGIC);
  82. return mem;
  83. }
  84. /*!
  85. * \brief Lock an \ref ast_json instance.
  86. *
  87. * If \a json is an immutable singleton (null, true, false), this function
  88. * safely ignores it and returns \c NULL. Otherwise, \a json must have been
  89. * allocates using ast_json_malloc().
  90. *
  91. * \param json JSON instance to lock.
  92. * \return \ref Corresponding \ref json_mem block.
  93. * \return \c NULL if \a json was not allocated.
  94. */
  95. static struct json_mem *json_mem_lock(struct ast_json *json)
  96. {
  97. struct json_mem *mem = to_json_mem(json);
  98. if (!mem) {
  99. return NULL;
  100. }
  101. ast_mutex_lock(&mem->mutex);
  102. return mem;
  103. }
  104. /*!
  105. * \brief Unlock a \ref json_mem instance.
  106. *
  107. * \param mem \ref json_mem, usually returned from json_mem_lock().
  108. */
  109. static void json_mem_unlock(struct json_mem *mem)
  110. {
  111. if (!mem) {
  112. return;
  113. }
  114. ast_mutex_unlock(&mem->mutex);
  115. }
  116. /*!
  117. * \brief Scoped lock for a \ref ast_json instance.
  118. *
  119. * \param json JSON instance to lock.
  120. */
  121. #define SCOPED_JSON_LOCK(json) \
  122. RAII_VAR(struct json_mem *, __mem_ ## __LINE__, \
  123. json_mem_lock(json), json_mem_unlock)
  124. void *ast_json_malloc(size_t size)
  125. {
  126. struct json_mem *mem = ast_malloc(size + sizeof(*mem));
  127. if (!mem) {
  128. return NULL;
  129. }
  130. mem->magic = JSON_MAGIC;
  131. ast_mutex_init(&mem->mutex);
  132. return mem->data;
  133. }
  134. AST_THREADSTORAGE(json_free_list_ts);
  135. /*!
  136. * \brief Struct for a linked list of \ref json_mem.
  137. */
  138. AST_LIST_HEAD_NOLOCK(json_mem_list, json_mem);
  139. /*!
  140. * \brief Thread local list of \ref json_mem blocks to free at the end of an
  141. * unref.
  142. */
  143. static struct json_mem_list *json_free_list(void)
  144. {
  145. return ast_threadstorage_get(&json_free_list_ts,
  146. sizeof(struct json_mem_list));
  147. }
  148. void ast_json_free(void *p)
  149. {
  150. struct json_mem *mem;
  151. struct json_mem_list *free_list;
  152. mem = to_json_mem(p);
  153. if (!mem) {
  154. return;
  155. }
  156. /* Since the unref is holding a lock in mem, we can't free it
  157. * immediately. Store it off on a thread local list to be freed by
  158. * ast_json_unref().
  159. */
  160. free_list = json_free_list();
  161. if (!free_list) {
  162. ast_log(LOG_ERROR, "Error allocating free list\n");
  163. ast_assert(0);
  164. /* It's not ideal to free the memory immediately, but that's the
  165. * best we can do if the threadlocal allocation fails */
  166. json_mem_free(mem);
  167. return;
  168. }
  169. AST_LIST_INSERT_HEAD(free_list, mem, list);
  170. }
  171. void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*))
  172. {
  173. json_set_alloc_funcs(malloc_fn, free_fn);
  174. }
  175. void ast_json_reset_alloc_funcs(void)
  176. {
  177. json_set_alloc_funcs(ast_json_malloc, ast_json_free);
  178. }
  179. struct ast_json *ast_json_ref(struct ast_json *json)
  180. {
  181. /* Jansson refcounting is non-atomic; lock it. */
  182. SCOPED_JSON_LOCK(json);
  183. json_incref((json_t *)json);
  184. return json;
  185. }
  186. void ast_json_unref(struct ast_json *json)
  187. {
  188. struct json_mem_list *free_list;
  189. struct json_mem *mem;
  190. if (!json) {
  191. return;
  192. }
  193. /* Jansson refcounting is non-atomic; lock it. */
  194. {
  195. SCOPED_JSON_LOCK(json);
  196. json_decref((json_t *) json);
  197. }
  198. /* Now free any objects that were ast_json_free()'s while the lock was
  199. * held */
  200. free_list = json_free_list();
  201. if (!free_list) {
  202. return;
  203. }
  204. while ((mem = AST_LIST_REMOVE_HEAD(free_list, list))) {
  205. json_mem_free(mem);
  206. }
  207. }
  208. enum ast_json_type ast_json_typeof(const struct ast_json *json)
  209. {
  210. int r = json_typeof((json_t*)json);
  211. switch(r) {
  212. case JSON_OBJECT: return AST_JSON_OBJECT;
  213. case JSON_ARRAY: return AST_JSON_ARRAY;
  214. case JSON_STRING: return AST_JSON_STRING;
  215. case JSON_INTEGER: return AST_JSON_INTEGER;
  216. case JSON_REAL: return AST_JSON_REAL;
  217. case JSON_TRUE: return AST_JSON_TRUE;
  218. case JSON_FALSE: return AST_JSON_FALSE;
  219. case JSON_NULL: return AST_JSON_NULL;
  220. }
  221. ast_assert(0); /* Unexpect return from json_typeof */
  222. return r;
  223. }
  224. const char *ast_json_typename(enum ast_json_type type)
  225. {
  226. switch (type) {
  227. case AST_JSON_OBJECT: return "object";
  228. case AST_JSON_ARRAY: return "array";
  229. case AST_JSON_STRING: return "string";
  230. case AST_JSON_INTEGER: return "integer";
  231. case AST_JSON_REAL: return "real";
  232. case AST_JSON_TRUE: return "boolean";
  233. case AST_JSON_FALSE: return "boolean";
  234. case AST_JSON_NULL: return "null";
  235. }
  236. ast_assert(0);
  237. return "?";
  238. }
  239. struct ast_json *ast_json_true(void)
  240. {
  241. return (struct ast_json *)json_true();
  242. }
  243. struct ast_json *ast_json_false(void)
  244. {
  245. return (struct ast_json *)json_false();
  246. }
  247. struct ast_json *ast_json_boolean(int value)
  248. {
  249. #if JANSSON_VERSION_HEX >= 0x020400
  250. return (struct ast_json *)json_boolean(value);
  251. #else
  252. return value ? ast_json_true() : ast_json_false();
  253. #endif
  254. }
  255. struct ast_json *ast_json_null(void)
  256. {
  257. return (struct ast_json *)json_null();
  258. }
  259. int ast_json_is_true(const struct ast_json *json)
  260. {
  261. return json_is_true((const json_t *)json);
  262. }
  263. int ast_json_is_false(const struct ast_json *json)
  264. {
  265. return json_is_false((const json_t *)json);
  266. }
  267. int ast_json_is_null(const struct ast_json *json)
  268. {
  269. return json_is_null((const json_t *)json);
  270. }
  271. struct ast_json *ast_json_string_create(const char *value)
  272. {
  273. return (struct ast_json *)json_string(value);
  274. }
  275. const char *ast_json_string_get(const struct ast_json *string)
  276. {
  277. return json_string_value((json_t *)string);
  278. }
  279. int ast_json_string_set(struct ast_json *string, const char *value)
  280. {
  281. return json_string_set((json_t *)string, value);
  282. }
  283. struct ast_json *ast_json_stringf(const char *format, ...)
  284. {
  285. struct ast_json *ret;
  286. va_list args;
  287. va_start(args, format);
  288. ret = ast_json_vstringf(format, args);
  289. va_end(args);
  290. return ret;
  291. }
  292. struct ast_json *ast_json_vstringf(const char *format, va_list args)
  293. {
  294. char *str = NULL;
  295. json_t *ret = NULL;
  296. if (format) {
  297. int err = ast_vasprintf(&str, format, args);
  298. if (err > 0) {
  299. ret = json_string(str);
  300. ast_free(str);
  301. }
  302. }
  303. return (struct ast_json *)ret;
  304. }
  305. struct ast_json *ast_json_integer_create(intmax_t value)
  306. {
  307. return (struct ast_json *)json_integer(value);
  308. }
  309. intmax_t ast_json_integer_get(const struct ast_json *integer)
  310. {
  311. return json_integer_value((json_t *)integer);
  312. }
  313. int ast_json_integer_set(struct ast_json *integer, intmax_t value)
  314. {
  315. return json_integer_set((json_t *)integer, value);
  316. }
  317. struct ast_json *ast_json_real_create(double value)
  318. {
  319. return (struct ast_json *)json_real(value);
  320. }
  321. double ast_json_real_get(const struct ast_json *real)
  322. {
  323. return json_real_value((json_t *)real);
  324. }
  325. int ast_json_real_set(struct ast_json *real, double value)
  326. {
  327. return json_real_set((json_t *)real, value);
  328. }
  329. int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
  330. {
  331. return json_equal((json_t *)lhs, (json_t *)rhs);
  332. }
  333. struct ast_json *ast_json_array_create(void)
  334. {
  335. return (struct ast_json *)json_array();
  336. }
  337. size_t ast_json_array_size(const struct ast_json *array)
  338. {
  339. return json_array_size((json_t *)array);
  340. }
  341. struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index)
  342. {
  343. return (struct ast_json *)json_array_get((json_t *)array, index);
  344. }
  345. int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
  346. {
  347. return json_array_set_new((json_t *)array, index, (json_t *)value);
  348. }
  349. int ast_json_array_append(struct ast_json *array, struct ast_json *value)
  350. {
  351. return json_array_append_new((json_t *)array, (json_t *)value);
  352. }
  353. int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
  354. {
  355. return json_array_insert_new((json_t *)array, index, (json_t *)value);
  356. }
  357. int ast_json_array_remove(struct ast_json *array, size_t index)
  358. {
  359. return json_array_remove((json_t *)array, index);
  360. }
  361. int ast_json_array_clear(struct ast_json *array)
  362. {
  363. return json_array_clear((json_t *)array);
  364. }
  365. int ast_json_array_extend(struct ast_json *array, struct ast_json *tail)
  366. {
  367. return json_array_extend((json_t *)array, (json_t *)tail);
  368. }
  369. struct ast_json *ast_json_object_create(void)
  370. {
  371. return (struct ast_json *)json_object();
  372. }
  373. size_t ast_json_object_size(struct ast_json *object)
  374. {
  375. return json_object_size((json_t *)object);
  376. }
  377. struct ast_json *ast_json_object_get(struct ast_json *object, const char *key)
  378. {
  379. if (!key) {
  380. return NULL;
  381. }
  382. return (struct ast_json *)json_object_get((json_t *)object, key);
  383. }
  384. int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
  385. {
  386. return json_object_set_new((json_t *)object, key, (json_t *)value);
  387. }
  388. int ast_json_object_del(struct ast_json *object, const char *key)
  389. {
  390. return json_object_del((json_t *)object, key);
  391. }
  392. int ast_json_object_clear(struct ast_json *object)
  393. {
  394. return json_object_clear((json_t *)object);
  395. }
  396. int ast_json_object_update(struct ast_json *object, struct ast_json *other)
  397. {
  398. return json_object_update((json_t *)object, (json_t *)other);
  399. }
  400. int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
  401. {
  402. #if JANSSON_VERSION_HEX >= 0x020300
  403. return json_object_update_existing((json_t *)object, (json_t *)other);
  404. #else
  405. struct ast_json_iter *iter = ast_json_object_iter(other);
  406. int ret = 0;
  407. if (object == NULL || other == NULL) {
  408. return -1;
  409. }
  410. while (iter != NULL && ret == 0) {
  411. const char *key = ast_json_object_iter_key(iter);
  412. if (ast_json_object_get(object, key) != NULL) {
  413. struct ast_json *value = ast_json_object_iter_value(iter);
  414. if (!value || ast_json_object_set(object, key, ast_json_ref(value))) {
  415. ret = -1;
  416. }
  417. }
  418. iter = ast_json_object_iter_next(other, iter);
  419. }
  420. return ret;
  421. #endif
  422. }
  423. int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
  424. {
  425. #if JANSSON_VERSION_HEX >= 0x020300
  426. return json_object_update_missing((json_t *)object, (json_t *)other);
  427. #else
  428. struct ast_json_iter *iter = ast_json_object_iter(other);
  429. int ret = 0;
  430. if (object == NULL || other == NULL) {
  431. return -1;
  432. }
  433. while (iter != NULL && ret == 0) {
  434. const char *key = ast_json_object_iter_key(iter);
  435. if (ast_json_object_get(object, key) == NULL) {
  436. struct ast_json *value = ast_json_object_iter_value(iter);
  437. if (!value || ast_json_object_set(object, key, ast_json_ref(value))) {
  438. ret = -1;
  439. }
  440. }
  441. iter = ast_json_object_iter_next(other, iter);
  442. }
  443. return ret;
  444. #endif
  445. }
  446. struct ast_json_iter *ast_json_object_iter(struct ast_json *object)
  447. {
  448. return json_object_iter((json_t *)object);
  449. }
  450. struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key)
  451. {
  452. return json_object_iter_at((json_t *)object, key);
  453. }
  454. struct ast_json_iter *ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter)
  455. {
  456. return json_object_iter_next((json_t *)object, iter);
  457. }
  458. const char *ast_json_object_iter_key(struct ast_json_iter *iter)
  459. {
  460. return json_object_iter_key(iter);
  461. }
  462. struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter)
  463. {
  464. return (struct ast_json *)json_object_iter_value(iter);
  465. }
  466. int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
  467. {
  468. return json_object_iter_set_new((json_t *)object, iter, (json_t *)value);
  469. }
  470. /*!
  471. * \brief Default flags for JSON encoding.
  472. */
  473. static size_t dump_flags(enum ast_json_encoding_format format)
  474. {
  475. return format == AST_JSON_PRETTY ?
  476. JSON_INDENT(2) | JSON_PRESERVE_ORDER : JSON_COMPACT;
  477. }
  478. char *ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
  479. {
  480. /* Jansson's json_dump*, even though it's a read operation, isn't
  481. * thread safe for concurrent reads. Locking is necessary.
  482. * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */
  483. SCOPED_JSON_LOCK(root);
  484. return json_dumps((json_t *)root, dump_flags(format));
  485. }
  486. static int write_to_ast_str(const char *buffer, size_t size, void *data)
  487. {
  488. struct ast_str **dst = data;
  489. size_t str_size = ast_str_size(*dst);
  490. size_t remaining = str_size - ast_str_strlen(*dst);
  491. int ret;
  492. /* While ast_str_append will grow the ast_str, it won't report
  493. * allocation errors. Fortunately, it's not that hard.
  494. */
  495. /* Remaining needs to be big enough for buffer, plus null char */
  496. while (remaining < size + 1) {
  497. /* doubling the size of the buffer gives us 'amortized
  498. * constant' time.
  499. * See http://stackoverflow.com/a/249695/115478 for info.
  500. */
  501. str_size *= 2;
  502. remaining = str_size - ast_str_strlen(*dst);
  503. }
  504. ret = ast_str_make_space(dst, str_size);
  505. if (ret == -1) {
  506. /* Could not alloc; fail */
  507. return -1;
  508. }
  509. ast_str_append_substr(dst, -1, buffer, size);
  510. return 0;
  511. }
  512. int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format)
  513. {
  514. /* Jansson's json_dump*, even though it's a read operation, isn't
  515. * thread safe for concurrent reads. Locking is necessary.
  516. * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */
  517. SCOPED_JSON_LOCK(root);
  518. return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format));
  519. }
  520. int ast_json_dump_file_format(struct ast_json *root, FILE *output, enum ast_json_encoding_format format)
  521. {
  522. /* Jansson's json_dump*, even though it's a read operation, isn't
  523. * thread safe for concurrent reads. Locking is necessary.
  524. * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */
  525. SCOPED_JSON_LOCK(root);
  526. if (!root || !output) {
  527. return -1;
  528. }
  529. return json_dumpf((json_t *)root, output, dump_flags(format));
  530. }
  531. int ast_json_dump_new_file_format(struct ast_json *root, const char *path, enum ast_json_encoding_format format)
  532. {
  533. /* Jansson's json_dump*, even though it's a read operation, isn't
  534. * thread safe for concurrent reads. Locking is necessary.
  535. * See http://www.digip.org/jansson/doc/2.4/portability.html#thread-safety. */
  536. SCOPED_JSON_LOCK(root);
  537. if (!root || !path) {
  538. return -1;
  539. }
  540. return json_dump_file((json_t *)root, path, dump_flags(format));
  541. }
  542. /*!
  543. * \brief Copy Jansson error struct to ours.
  544. */
  545. static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
  546. {
  547. if (error && jansson_error) {
  548. error->line = jansson_error->line;
  549. error->column = jansson_error->column;
  550. error->position = jansson_error->position;
  551. ast_copy_string(error->text, jansson_error->text, sizeof(error->text));
  552. ast_copy_string(error->source, jansson_error->source, sizeof(error->source));
  553. }
  554. }
  555. static void parse_error(struct ast_json_error *error, const char *text, const char *source)
  556. {
  557. if (error != NULL) {
  558. error->line = 0;
  559. error->column = 0;
  560. error->position = 0;
  561. strncpy(error->text, text, sizeof(error->text));
  562. strncpy(error->source, source, sizeof(error->text));
  563. }
  564. }
  565. struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error)
  566. {
  567. json_error_t jansson_error = {};
  568. struct ast_json *r = NULL;
  569. if (input != NULL) {
  570. r = (struct ast_json *)json_loads(input, 0, &jansson_error);
  571. copy_error(error, &jansson_error);
  572. } else {
  573. parse_error(error, "NULL input string", "<null>");
  574. }
  575. return r;
  576. }
  577. struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
  578. {
  579. return ast_json_load_string(ast_str_buffer(input), error);
  580. }
  581. struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
  582. {
  583. json_error_t jansson_error = {};
  584. struct ast_json *r = (struct ast_json *)json_loadb(buffer, buflen, 0, &jansson_error);
  585. copy_error(error, &jansson_error);
  586. return r;
  587. }
  588. struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *error)
  589. {
  590. json_error_t jansson_error = {};
  591. struct ast_json *r = NULL;
  592. if (input != NULL) {
  593. r = (struct ast_json *)json_loadf(input, 0, &jansson_error);
  594. copy_error(error, &jansson_error);
  595. } else {
  596. parse_error(error, "NULL input file", "<null>");
  597. }
  598. return r;
  599. }
  600. struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *error)
  601. {
  602. json_error_t jansson_error = {};
  603. struct ast_json *r = (struct ast_json *)json_load_file(path, 0, &jansson_error);
  604. copy_error(error, &jansson_error);
  605. return r;
  606. }
  607. struct ast_json *ast_json_pack(char const *format, ...)
  608. {
  609. struct ast_json *ret;
  610. va_list args;
  611. va_start(args, format);
  612. ret = ast_json_vpack(format, args);
  613. va_end(args);
  614. return ret;
  615. }
  616. struct ast_json *ast_json_vpack(char const *format, va_list ap)
  617. {
  618. json_error_t error;
  619. struct ast_json *r = NULL;
  620. if (format) {
  621. r = (struct ast_json *)json_vpack_ex(&error, 0, format, ap);
  622. if (!r && !ast_strlen_zero(error.text)) {
  623. ast_log(LOG_ERROR,
  624. "Error building JSON from '%s': %s.\n",
  625. format, error.text);
  626. }
  627. }
  628. return r;
  629. }
  630. struct ast_json *ast_json_copy(const struct ast_json *value)
  631. {
  632. return (struct ast_json *)json_copy((json_t *)value);
  633. }
  634. struct ast_json *ast_json_deep_copy(const struct ast_json *value)
  635. {
  636. return (struct ast_json *)json_deep_copy((json_t *)value);
  637. }
  638. struct ast_json *ast_json_name_number(const char *name, const char *number)
  639. {
  640. return ast_json_pack("{s: s, s: s}",
  641. "name", name,
  642. "number", number);
  643. }
  644. struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
  645. {
  646. return ast_json_pack("{s: o, s: o, s: o}",
  647. "context", context ? ast_json_string_create(context) : ast_json_null(),
  648. "exten", exten ? ast_json_string_create(exten) : ast_json_null(),
  649. "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
  650. }
  651. struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone)
  652. {
  653. char buf[AST_ISO8601_LEN];
  654. struct ast_tm tm = {};
  655. ast_localtime(&tv, &tm, zone);
  656. ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
  657. return ast_json_string_create(buf);
  658. }
  659. struct ast_json *ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type)
  660. {
  661. struct ast_str *string = ast_str_alloca(64);
  662. if (!string) {
  663. return NULL;
  664. }
  665. ast_str_set(&string, 0, (ast_sockaddr_is_ipv4(addr) ||
  666. ast_sockaddr_is_ipv4_mapped(addr)) ? "IPV4/" : "IPV6/");
  667. if (transport_type) {
  668. char *transport_string = NULL;
  669. /* NOTE: None will be applied if multiple transport types are specified in transport_type */
  670. switch(transport_type) {
  671. case AST_TRANSPORT_UDP:
  672. transport_string = "UDP";
  673. break;
  674. case AST_TRANSPORT_TCP:
  675. transport_string = "TCP";
  676. break;
  677. case AST_TRANSPORT_TLS:
  678. transport_string = "TLS";
  679. break;
  680. case AST_TRANSPORT_WS:
  681. transport_string = "WS";
  682. break;
  683. case AST_TRANSPORT_WSS:
  684. transport_string = "WSS";
  685. break;
  686. }
  687. if (transport_string) {
  688. ast_str_append(&string, 0, "%s/", transport_string);
  689. }
  690. }
  691. ast_str_append(&string, 0, "%s", ast_sockaddr_stringify_addr(addr));
  692. ast_str_append(&string, 0, "/%s", ast_sockaddr_stringify_port(addr));
  693. return ast_json_string_create(ast_str_buffer(string));
  694. }
  695. void ast_json_init(void)
  696. {
  697. /* Setup to use Asterisk custom allocators */
  698. ast_json_reset_alloc_funcs();
  699. }
  700. static void json_payload_destructor(void *obj)
  701. {
  702. struct ast_json_payload *payload = obj;
  703. ast_json_unref(payload->json);
  704. }
  705. struct ast_json_payload *ast_json_payload_create(struct ast_json *json)
  706. {
  707. struct ast_json_payload *payload;
  708. if (!(payload = ao2_alloc(sizeof(*payload), json_payload_destructor))) {
  709. return NULL;
  710. }
  711. ast_json_ref(json);
  712. payload->json = json;
  713. return payload;
  714. }
  715. static struct ast_json *json_party_number(struct ast_party_number *number)
  716. {
  717. if (!number->valid) {
  718. return NULL;
  719. }
  720. return ast_json_pack("{s: s, s: i, s: i, s: s}",
  721. "number", number->str,
  722. "plan", number->plan,
  723. "presentation", number->presentation,
  724. "presentation_txt", ast_describe_caller_presentation(number->presentation));
  725. }
  726. static struct ast_json *json_party_name(struct ast_party_name *name)
  727. {
  728. if (!name->valid) {
  729. return NULL;
  730. }
  731. return ast_json_pack("{s: s, s: s, s: i, s: s}",
  732. "name", name->str,
  733. "character_set", ast_party_name_charset_describe(name->char_set),
  734. "presentation", name->presentation,
  735. "presentation_txt", ast_describe_caller_presentation(name->presentation));
  736. }
  737. static struct ast_json *json_party_subaddress(struct ast_party_subaddress *subaddress)
  738. {
  739. if (!subaddress->valid) {
  740. return NULL;
  741. }
  742. return ast_json_pack("{s: s, s: i, s: b}",
  743. "subaddress", subaddress->str,
  744. "type", subaddress->type,
  745. "odd", subaddress->odd_even_indicator);
  746. }
  747. struct ast_json *ast_json_party_id(struct ast_party_id *party)
  748. {
  749. RAII_VAR(struct ast_json *, json_party_id, NULL, ast_json_unref);
  750. int pres;
  751. /* Combined party presentation */
  752. pres = ast_party_id_presentation(party);
  753. json_party_id = ast_json_pack("{s: i, s: s}",
  754. "presentation", pres,
  755. "presentation_txt", ast_describe_caller_presentation(pres));
  756. if (!json_party_id) {
  757. return NULL;
  758. }
  759. /* Party number */
  760. if (party->number.valid && ast_json_object_set(json_party_id, "number", json_party_number(&party->number))) {
  761. return NULL;
  762. }
  763. /* Party name */
  764. if (party->name.valid && ast_json_object_set(json_party_id, "name", json_party_name(&party->name))) {
  765. return NULL;
  766. }
  767. /* Party subaddress */
  768. if (party->subaddress.valid && ast_json_object_set(json_party_id, "subaddress", json_party_subaddress(&party->subaddress))) {
  769. return NULL;
  770. }
  771. return ast_json_ref(json_party_id);
  772. }