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