Asterisk - The Open Source Telephony Project  21.4.1
Data Structures
json.h File Reference

Asterisk JSON abstraction layer. More...

#include "asterisk/netsock2.h"

Go to the source code of this file.

Data Structures

struct  ast_json_error
 JSON parsing error information. More...
 
struct  ast_json_payload
 

Functions

struct ast_jsonast_json_true (void)
 Get the JSON true value. More...
 
struct ast_jsonast_json_false (void)
 Get the JSON false value. More...
 
struct ast_jsonast_json_boolean (int value)
 Get the JSON boolean corresponding to value. More...
 
struct ast_jsonast_json_null (void)
 Get the JSON null value. More...
 
int ast_json_is_array (const struct ast_json *value)
 Check if value is JSON array. More...
 
int ast_json_is_object (const struct ast_json *value)
 Check if value is JSON object. More...
 
int ast_json_is_true (const struct ast_json *value)
 Check if value is JSON true. More...
 
int ast_json_is_false (const struct ast_json *value)
 Check if value is JSON false. More...
 
int ast_json_is_null (const struct ast_json *value)
 Check if value is JSON null. More...
 
struct ast_jsonast_json_string_create (const char *value)
 Construct a JSON string from value. More...
 
const char * ast_json_string_get (const struct ast_json *string)
 Get the value of a JSON string. More...
 
int ast_json_string_set (struct ast_json *string, const char *value)
 Change the value of a JSON string. More...
 
struct ast_jsonast_json_stringf (const char *format,...)
 Create a JSON string, printf style. More...
 
struct ast_jsonast_json_vstringf (const char *format, va_list args)
 Create a JSON string, vprintf style. More...
 
struct ast_jsonast_json_integer_create (intmax_t value)
 Create a JSON integer. More...
 
intmax_t ast_json_integer_get (const struct ast_json *integer)
 Get the value from a JSON integer. More...
 
int ast_json_integer_set (struct ast_json *integer, intmax_t value)
 Set the value of a JSON integer. More...
 
struct ast_jsonast_json_real_create (double value)
 Create a JSON real number. More...
 
double ast_json_real_get (const struct ast_json *real)
 Get the value from a JSON real number. More...
 
int ast_json_real_set (struct ast_json *real, double value)
 Set the value of a JSON real number. More...
 
struct ast_jsonast_json_array_create (void)
 Create a empty JSON array. More...
 
size_t ast_json_array_size (const struct ast_json *array)
 Get the size of a JSON array. More...
 
struct ast_jsonast_json_array_get (const struct ast_json *array, size_t index)
 Get an element from an array. More...
 
int ast_json_array_set (struct ast_json *array, size_t index, struct ast_json *value)
 Change an element in an array. More...
 
int ast_json_array_append (struct ast_json *array, struct ast_json *value)
 Append to an array. More...
 
int ast_json_array_insert (struct ast_json *array, size_t index, struct ast_json *value)
 Insert into an array. More...
 
int ast_json_array_remove (struct ast_json *array, size_t index)
 Remove an element from an array. More...
 
int ast_json_array_clear (struct ast_json *array)
 Remove all elements from an array. More...
 
int ast_json_array_extend (struct ast_json *array, struct ast_json *tail)
 Append all elements from tail to array. More...
 
int ast_json_equal (const struct ast_json *lhs, const struct ast_json *rhs)
 Compare two JSON objects. More...
 
struct ast_jsonast_json_copy (const struct ast_json *value)
 Copy a JSON value, but not its children. More...
 
struct ast_jsonast_json_deep_copy (const struct ast_json *value)
 Copy a JSON value, and its children. More...
 
typedef AST_JSON_INT_T ast_json_int_t
 Primarily used to cast when packing to an "I" type.
 
int ast_json_init (void)
 Initialize the JSON library.
 
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(). More...
 
void ast_json_reset_alloc_funcs (void)
 Change alloc funcs back to the resource module defaults. More...
 
void * ast_json_malloc (size_t size)
 Asterisk's custom JSON allocator. Exposed for use by unit tests. More...
 
void ast_json_free (void *p)
 Asterisk's custom JSON allocator. Exposed for use by unit tests. More...
 
struct ast_jsonast_json_ref (struct ast_json *value)
 Increase refcount on value. More...
 
void ast_json_unref (struct ast_json *value)
 Decrease refcount on value. If refcount reaches zero, value is freed. More...
 
enum  ast_json_type {
  AST_JSON_OBJECT, AST_JSON_ARRAY, AST_JSON_STRING, AST_JSON_INTEGER,
  AST_JSON_REAL, AST_JSON_TRUE, AST_JSON_FALSE, AST_JSON_NULL
}
 Valid types of a JSON element. More...
 
enum ast_json_type ast_json_typeof (const struct ast_json *value)
 Get the type of value. More...
 
const char * ast_json_typename (enum ast_json_type type)
 Get the string name for the given type. More...
 
#define AST_JSON_UTF8_VALIDATE(str)   (ast_json_utf8_check(str) ? (str) : "")
 Check str for UTF-8 and replace with an empty string if fails the check. More...
 
int ast_json_utf8_check_len (const char *str, size_t len)
 Check the string of the given length for UTF-8 format. More...
 
int ast_json_utf8_check (const char *str)
 Check the nul terminated string for UTF-8 format. More...
 
#define ast_json_object_string_get(object, key)   ast_json_string_get(ast_json_object_get(object, key))
 Get a string field from a JSON object. More...
 
#define ast_json_object_integer_get(object, key)   ast_json_integer_get(ast_json_object_get(object, key))
 Get an integer field from a JSON object. More...
 
#define ast_json_object_real_get(object, key)   ast_json_real_get(ast_json_object_get(object, key))
 Get a double field from a JSON object. More...
 
struct ast_jsonast_json_object_create (void)
 Create a new JSON object. More...
 
struct ast_jsonast_json_object_create_vars (const struct ast_variable *variables, const char *excludes)
 Create a new JSON object using the given variables. More...
 
size_t ast_json_object_size (struct ast_json *object)
 Get size of JSON object. More...
 
struct ast_jsonast_json_object_get (struct ast_json *object, const char *key)
 Get a field from a JSON object. More...
 
int ast_json_object_set (struct ast_json *object, const char *key, struct ast_json *value)
 Set a field in a JSON object. More...
 
int ast_json_object_del (struct ast_json *object, const char *key)
 Delete a field from a JSON object. More...
 
int ast_json_object_clear (struct ast_json *object)
 Delete all elements from a JSON object. More...
 
int ast_json_object_update (struct ast_json *object, struct ast_json *other)
 Update object with all of the fields of other. More...
 
int ast_json_object_update_existing (struct ast_json *object, struct ast_json *other)
 Update existing fields in object with the fields of other. More...
 
int ast_json_object_update_missing (struct ast_json *object, struct ast_json *other)
 Add new fields to object with the fields of other. More...
 
struct ast_json_iterast_json_object_iter (struct ast_json *object)
 Get an iterator pointing to the first field in a JSON object. More...
 
struct ast_json_iterast_json_object_iter_at (struct ast_json *object, const char *key)
 Get an iterator pointing to a specified key in object. More...
 
struct ast_json_iterast_json_object_iter_next (struct ast_json *object, struct ast_json_iter *iter)
 Get the next iterator. More...
 
const char * ast_json_object_iter_key (struct ast_json_iter *iter)
 Get the key from an iterator. More...
 
struct ast_jsonast_json_object_iter_value (struct ast_json_iter *iter)
 Get the value from an iterator. More...
 
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. More...
 
#define ast_json_dump_string(root)   ast_json_dump_string_format(root, AST_JSON_COMPACT)
 Encode a JSON value to a compact string. More...
 
#define ast_json_dump_string_sorted(root)   ast_json_dump_string_format(root, AST_JSON_SORTED)
 Encode a JSON value to a string, with its keys sorted. More...
 
#define ast_json_dump_str(root, dst)   ast_json_dump_str_format(root, dst, AST_JSON_COMPACT)
 
#define ast_json_dump_file(root, output)   ast_json_dump_file_format(root, output, AST_JSON_COMPACT)
 
#define ast_json_dump_new_file(root, path)   ast_json_dump_new_file_format(root, path, AST_JSON_COMPACT)
 
#define AST_JSON_ERROR_TEXT_LENGTH   160
 
#define AST_JSON_ERROR_SOURCE_LENGTH   80
 
enum  ast_json_encoding_format { AST_JSON_COMPACT, AST_JSON_PRETTY, AST_JSON_SORTED }
 Encoding format type. More...
 
char * ast_json_dump_string_format (struct ast_json *root, enum ast_json_encoding_format format)
 Encode a JSON value to a string. More...
 
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. More...
 
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. More...
 
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. More...
 
struct ast_jsonast_json_load_string (const char *input, struct ast_json_error *error)
 Parse null terminated string into a JSON object or array. More...
 
struct ast_jsonast_json_load_str (const struct ast_str *input, struct ast_json_error *error)
 Parse ast_str into a JSON object or array. More...
 
struct ast_jsonast_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. More...
 
struct ast_jsonast_json_load_file (FILE *input, struct ast_json_error *error)
 Parse a FILE into JSON object or array. More...
 
struct ast_jsonast_json_load_new_file (const char *path, struct ast_json_error *error)
 Parse file at path into JSON object or array. More...
 
struct ast_jsonast_json_pack (char const *format,...)
 Helper for creating complex JSON values. More...
 
struct ast_jsonast_json_vpack (char const *format, va_list ap)
 Helper for creating complex JSON values simply. More...
 
enum  ast_json_to_ast_vars_code { AST_JSON_TO_AST_VARS_CODE_SUCCESS, AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE, AST_JSON_TO_AST_VARS_CODE_OOM }
 
struct ast_jsonast_json_name_number (const char *name, const char *number)
 Common JSON rendering functions for common 'objects'. More...
 
struct ast_jsonast_json_timeval (const struct timeval tv, const char *zone)
 Construct a timeval as JSON. More...
 
struct ast_jsonast_json_ipaddr (const struct ast_sockaddr *addr, enum ast_transport transport_type)
 Construct an IP address as JSON. More...
 
struct ast_jsonast_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. More...
 
struct ast_jsonast_json_dialplan_cep (const char *context, const char *exten, int priority)
 Construct a context/exten/priority as JSON. More...
 
struct ast_json_payloadast_json_payload_create (struct ast_json *json)
 Create an ao2 object to pass json blobs as data payloads for stasis. More...
 
struct ast_jsonast_json_party_id (struct ast_party_id *party)
 Construct an ast_party_id as JSON. More...
 
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. More...
 
struct ast_jsonast_json_channel_vars (struct varshead *channelvars)
 Construct a JSON object from a ast_var_t list. More...
 

Detailed Description

Asterisk JSON abstraction layer.

Since
12.0.0

This is a very thin wrapper around the Jansson API. For more details on it, see its docs at http://www.digip.org/jansson/doc/2.11/apiref.html.

Rather than provide the multiple ways of doing things that the Jansson API does, the Asterisk wrapper is always reference-stealing, and always NULL safe.

And by always, I mean that the reference is stolen even if the function fails. This avoids lots of conditional logic, and also avoids having to track zillions of local variables when building complex JSON objects. You can instead chain ast_json_* calls together safely and only worry about cleaning up the root object.

In the cases where you have a need to introduce intermediate objects, just wrap them with json_ref() when passing them to other ast_json_*() functions.

Example code
// Example of how to use the Asterisk JSON API
static struct ast_json *foo(void) {
RAII_VAR(struct ast_json *, array, NULL, ast_json_unref);
RAII_VAR(struct ast_json *, obj, NULL, ast_json_unref);
int i, res;
if (!array) { return NULL; }
for (i = 0; i < 10; ++i) {
// NULL safety and object stealing means calls can
// be chained together directly.
res = ast_json_array_append(array,
if (res != 0) { return NULL; }
}
if (!obj) { return NULL; }
// If you already have an object reference, ast_json_ref()
// can be used inline to bump the ref before passing it along
// to a ref-stealing call
res = ast_json_object_set(obj, "foo", ast_json_ref(array));
if (!res) { return NULL; }
return obj;
}
Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file json.h.

Macro Definition Documentation

#define ast_json_dump_string (   root)    ast_json_dump_string_format(root, AST_JSON_COMPACT)

Encode a JSON value to a compact string.

Since
12.0.0

Returned string must be freed by calling ast_json_free().

Parameters
rootJSON value.
Returns
String encoding of root.
Return values
NULLon error.

Definition at line 810 of file json.h.

Referenced by ast_cel_create_event_with_time().

#define ast_json_dump_string_sorted (   root)    ast_json_dump_string_format(root, AST_JSON_SORTED)

Encode a JSON value to a string, with its keys sorted.

Returned string must be freed by calling ast_json_free().

Parameters
rootJSON value.
Returns
String encoding of root.
Return values
NULLon error.

Definition at line 834 of file json.h.

#define ast_json_object_integer_get (   object,
  key 
)    ast_json_integer_get(ast_json_object_get(object, key))

Get an integer field from a JSON object.

Parameters
objectJSON object.
keyKey of integer field to look up.
Returns
Value of a JSON integer.
Return values
0if integer is not a JSON integer.

Definition at line 609 of file json.h.

#define ast_json_object_real_get (   object,
  key 
)    ast_json_real_get(ast_json_object_get(object, key))

Get a double field from a JSON object.

Parameters
objectJSON object.
keyKey of double field to look up.
Returns
Value of a JSON double.
Return values
0if real is not a JSON real number.

Definition at line 618 of file json.h.

#define ast_json_object_string_get (   object,
  key 
)    ast_json_string_get(ast_json_object_get(object, key))

Get a string field from a JSON object.

Since
16.3.0
Parameters
objectJSON object.
keyKey of string field to look up.
Returns
String value of given key.
Return values
NULLon error, or key value is not a string.

Definition at line 600 of file json.h.

#define AST_JSON_UTF8_VALIDATE (   str)    (ast_json_utf8_check(str) ? (str) : "")

Check str for UTF-8 and replace with an empty string if fails the check.

Note
The convenience macro is normally used with ast_json_pack() or a function wrapper that calls ast_json_vpack().

Definition at line 224 of file json.h.

Referenced by ast_json_name_number(), report_fax_status(), report_receive_fax_status(), report_send_fax_status(), and set_channel_variables().

Enumeration Type Documentation

Encoding format type.

Since
12.0.0
Enumerator
AST_JSON_COMPACT 

Compact format, low human readability

AST_JSON_PRETTY 

Formatted for human readability

AST_JSON_SORTED 

Keys sorted alphabetically

Definition at line 790 of file json.h.

791 {
792  /*! Compact format, low human readability */
794  /*! Formatted for human readability */
796  /*! Keys sorted alphabetically */
798 };
Enumerator
AST_JSON_TO_AST_VARS_CODE_SUCCESS 

Conversion successful.

AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE 

Conversion failed because invalid value type supplied.

Note
Only string values allowed.
AST_JSON_TO_AST_VARS_CODE_OOM 

Conversion failed because of allocation failure. (Out Of Memory)

Definition at line 1109 of file json.h.

1109  {
1110  /*! \brief Conversion successful */
1112  /*!
1113  * \brief Conversion failed because invalid value type supplied.
1114  * \note Only string values allowed.
1115  */
1117  /*! \brief Conversion failed because of allocation failure. (Out Of Memory) */
1119 };
Conversion failed because invalid value type supplied.
Definition: json.h:1116
Conversion successful.
Definition: json.h:1111
Conversion failed because of allocation failure. (Out Of Memory)
Definition: json.h:1118

Valid types of a JSON element.

Since
12.0.0

Definition at line 162 of file json.h.

163 {
164  AST_JSON_OBJECT,
165  AST_JSON_ARRAY,
166  AST_JSON_STRING,
167  AST_JSON_INTEGER,
168  AST_JSON_REAL,
169  AST_JSON_TRUE,
170  AST_JSON_FALSE,
171  AST_JSON_NULL,
172 };

Function Documentation

int ast_json_array_append ( struct ast_json array,
struct ast_json value 
)

Append to an array.

Since
12.0.0
Note
The array steals the value reference even if it returns error; use ast_json_ref() to safely keep a pointer to it.
Parameters
arrayJSON array to modify.
valueNew JSON value to store at the end of array.
Return values
0on success.
-1on error.

Definition at line 378 of file json.c.

Referenced by add_format_information_cb(), app_to_json(), append_sound_cb(), ast_ari_bridges_list(), ast_ari_channels_list(), ast_ari_endpoints_list(), ast_ari_endpoints_list_by_tech(), ast_ari_recordings_list_stored(), ast_bucket_json(), ast_endpoint_snapshot_to_json(), conf_send_event_to_participants(), process_log_list(), process_module_list(), report_receive_fax_status(), stasis_app_device_states_to_json(), stasis_app_exec(), and stasis_app_mailboxes_to_json().

379 {
380  return json_array_append_new((json_t *)array, (json_t *)value);
381 }
int ast_json_array_clear ( struct ast_json array)

Remove all elements from an array.

Since
12.0.0
Parameters
arrayJSON array to clear.
Return values
0on success.
-1on error.

Definition at line 390 of file json.c.

391 {
392  return json_array_clear((json_t *)array);
393 }
struct ast_json* ast_json_array_create ( void  )
int ast_json_array_extend ( struct ast_json array,
struct ast_json tail 
)

Append all elements from tail to array.

Since
12.0.0

The tail argument is not changed, so ast_json_unref() it when you are done with it.

Parameters
arrayJSON array to modify.
tailJSON array with contents to append to array.
Return values
0on success.
-1on error.

Definition at line 394 of file json.c.

395 {
396  return json_array_extend((json_t *)array, (json_t *)tail);
397 }
struct ast_json* ast_json_array_get ( const struct ast_json array,
size_t  index 
)

Get an element from an array.

Since
12.0.0

The returned element is a borrowed reference; use ast_json_ref() to safely keep a pointer to it.

Parameters
arrayJSON array.
indexZero-based index into array.
Returns
The specified element.
Return values
NULLif array not an array. if index is out of bounds.

Definition at line 370 of file json.c.

Referenced by ast_ari_applications_subscribe_parse_body(), ast_ari_applications_unsubscribe_parse_body(), ast_ari_asterisk_get_info_parse_body(), ast_ari_asterisk_update_object(), ast_ari_bridges_add_channel_parse_body(), ast_ari_bridges_play_parse_body(), ast_ari_bridges_play_with_id_parse_body(), ast_ari_bridges_remove_channel_parse_body(), ast_ari_channels_play_parse_body(), ast_ari_channels_play_with_id_parse_body(), ast_ari_events_user_event_parse_body(), and ast_ari_validate_list().

371 {
372  return (struct ast_json *)json_array_get((json_t *)array, index);
373 }
Abstract JSON element (object, array, string, int, ...).
int ast_json_array_insert ( struct ast_json array,
size_t  index,
struct ast_json value 
)

Insert into an array.

Since
12.0.0
Note
The array steals the value reference even if it returns error; use ast_json_ref() to safely keep a pointer to it.
Parameters
arrayJSON array to modify.
indexZero-based index into array.
valueNew JSON value to store in array at index.
Return values
0on success.
-1on error.

Definition at line 382 of file json.c.

383 {
384  return json_array_insert_new((json_t *)array, index, (json_t *)value);
385 }
int ast_json_array_remove ( struct ast_json array,
size_t  index 
)

Remove an element from an array.

Since
12.0.0
Parameters
arrayJSON array to modify.
indexZero-based index into array.
Return values
0on success.
-1on error.

Definition at line 386 of file json.c.

387 {
388  return json_array_remove((json_t *)array, index);
389 }
int ast_json_array_set ( struct ast_json array,
size_t  index,
struct ast_json value 
)

Change an element in an array.

Since
12.0.0
Note
The array steals the value reference even if it returns error; use ast_json_ref() to safely keep a pointer to it.
Parameters
arrayJSON array to modify.
indexZero-based index into array.
valueNew JSON value to store in array at index.
Return values
0on success.
-1on error.

Definition at line 374 of file json.c.

375 {
376  return json_array_set_new((json_t *)array, index, (json_t *)value);
377 }
size_t ast_json_array_size ( const struct ast_json array)
struct ast_json* ast_json_boolean ( int  value)

Get the JSON boolean corresponding to value.

Since
12.0.0
Returns
JSON true if value is true (non-zero).
JSON false if value is false (zero).

Definition at line 243 of file json.c.

244 {
245  return (struct ast_json *)json_boolean(value);
246 }
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_channel_vars ( struct varshead channelvars)

Construct a JSON object from a ast_var_t list.

Since
14.2.0
Parameters
channelvarsThe list of ast_var_t to represent as JSON
Returns
JSON object with variable names as keys and variable values as values

Definition at line 864 of file json.c.

References ast_json_object_create(), ast_json_object_set(), ast_json_string_create(), and AST_LIST_TRAVERSE.

Referenced by ast_channel_snapshot_to_json().

865 {
866  struct ast_json *ret;
867  struct ast_var_t *var;
868 
869  ret = ast_json_object_create();
870  AST_LIST_TRAVERSE(channelvars, var, entries) {
871  ast_json_object_set(ret, var->name, ast_json_string_create(var->value));
872  }
873 
874  return ret;
875 }
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
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:414
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
struct ast_json* ast_json_copy ( const struct ast_json value)

Copy a JSON value, but not its children.

Since
12.0.0

If value is a JSON object or array, its children are shared with the returned copy.

Parameters
valueJSON value to copy.
Returns
Shallow copy of value.
Return values
NULLon error.

Definition at line 637 of file json.c.

638 {
639  return (struct ast_json *)json_copy((json_t *)value);
640 }
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_deep_copy ( const struct ast_json value)

Copy a JSON value, and its children.

Since
12.0.0

If value is a JSON object or array, they are also copied.

Parameters
valueJSON value to copy.
Returns
Deep copy of value.
Return values
NULLon error.

Definition at line 641 of file json.c.

642 {
643  return (struct ast_json *)json_deep_copy((json_t *)value);
644 }
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_dialplan_cep ( const char *  context,
const char *  exten,
int  priority 
)

Construct a context/exten/priority as JSON.

If a NULL is passed for context or exten, or -1 for priority, the fields is set to ast_json_null().

Parameters
contextContext name.
extenExtension.
priorityDialplan priority.
Returns
JSON object with context, exten and priority fields

Definition at line 665 of file json.c.

References ast_json_dialplan_cep_app().

666 {
667  return ast_json_dialplan_cep_app(context, exten, priority, "", "");
668 }
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:653
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.

If a NULL is passed for context or exten or app_name or app_data, or -1 for priority, the fields is set to ast_json_null().

Parameters
contextContext name.
extenExtension.
priorityDialplan priority.
app_nameApplication name.
app_dataApplication argument.
Returns
JSON object with context, exten and priority app_name app_data fields

Definition at line 653 of file json.c.

References ast_json_integer_create(), ast_json_null(), and ast_json_pack().

Referenced by ast_channel_snapshot_to_json(), and ast_json_dialplan_cep().

655 {
656  return ast_json_pack("{s: s?, s: s?, s: o, s: s?, s: s?}",
657  "context", context,
658  "exten", exten,
659  "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null(),
660  "app_name", app_name,
661  "app_data", app_data
662  );
663 }
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
struct ast_json * ast_json_integer_create(intmax_t value)
Create a JSON integer.
Definition: json.c:327
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.

Since
12.0.0
Parameters
rootJSON value.
outputFile to write JSON encoding to.
formatencoding format type.
Return values
0on success.
-1on error. The contents of output are undefined.

Definition at line 526 of file json.c.

References dump_flags().

527 {
528  if (!root || !output) {
529  return -1;
530  }
531  return json_dumpf((json_t *)root, output, dump_flags(format));
532 }
static size_t dump_flags(enum ast_json_encoding_format format)
Default flags for JSON encoding.
Definition: json.c:467
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.

Since
12.0.0
Parameters
rootJSON value.
pathPath to file to write JSON encoding to.
formatencoding format type.
Return values
0on success.
-1on error. The contents of output are undefined.

Definition at line 533 of file json.c.

References dump_flags().

534 {
535  if (!root || !path) {
536  return -1;
537  }
538  return json_dump_file((json_t *)root, path, dump_flags(format));
539 }
static size_t dump_flags(enum ast_json_encoding_format format)
Default flags for JSON encoding.
Definition: json.c:467
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.

Since
12.0.0

If dst is too small, it will be grown as needed.

Parameters
rootJSON value.
dstast_str to store JSON encoding.
formatencoding format type.
Return values
0on success.
-1on error. The contents of dst are undefined.

Definition at line 520 of file json.c.

References dump_flags().

521 {
522  return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format));
523 }
static size_t dump_flags(enum ast_json_encoding_format format)
Default flags for JSON encoding.
Definition: json.c:467
char* ast_json_dump_string_format ( struct ast_json root,
enum ast_json_encoding_format  format 
)

Encode a JSON value to a string.

Since
12.0.0

Returned string must be freed by calling ast_json_free().

Parameters
rootJSON value.
formatencoding format type.
Returns
String encoding of root.
Return values
NULLon error.

Definition at line 484 of file json.c.

References dump_flags().

Referenced by ast_ari_websocket_session_write(), and stasis_app_message_handler().

485 {
486  return json_dumps((json_t *)root, dump_flags(format));
487 }
static size_t dump_flags(enum ast_json_encoding_format format)
Default flags for JSON encoding.
Definition: json.c:467
int ast_json_equal ( const struct ast_json lhs,
const struct ast_json rhs 
)

Compare two JSON objects.

Since
12.0.0

Two JSON objects are equal if they are of the same type, and their contents are equal.

Parameters
lhsValue to compare.
rhsOther value to compare.
Return values
True(non-zero) if lhs and rhs are equal.
False(zero) if they are not.

Definition at line 357 of file json.c.

358 {
359  return json_equal((json_t *)lhs, (json_t *)rhs);
360 }
struct ast_json* ast_json_false ( void  )

Get the JSON false value.

Since
12.0.0

The returned value is a singleton, and does not need to be ast_json_unref()'ed.

Returns
JSON false.

Definition at line 238 of file json.c.

239 {
240  return (struct ast_json *)json_false();
241 }
Abstract JSON element (object, array, string, int, ...).
void ast_json_free ( void *  p)

Asterisk's custom JSON allocator. Exposed for use by unit tests.

Since
12.0.0

Definition at line 52 of file json.c.

Referenced by ast_ari_websocket_session_write(), ast_cel_create_event_with_time(), ast_json_reset_alloc_funcs(), and stasis_app_message_handler().

53 {
54  ast_free(p);
55 }
struct ast_json* ast_json_integer_create ( intmax_t  value)

Create a JSON integer.

Since
12.0.0
Parameters
valueValue of the new JSON integer.
Returns
Newly allocated integer.
Return values
NULLon error.

Definition at line 327 of file json.c.

Referenced by ast_ari_asterisk_get_info(), ast_endpoint_snapshot_to_json(), ast_json_dialplan_cep_app(), ast_rtp_convert_stats_json(), identify_module(), and stasis_app_recording_to_json().

328 {
329  return (struct ast_json *)json_integer(value);
330 }
Abstract JSON element (object, array, string, int, ...).
intmax_t ast_json_integer_get ( const struct ast_json integer)
int ast_json_integer_set ( struct ast_json integer,
intmax_t  value 
)

Set the value of a JSON integer.

Since
12.0.0
Parameters
integerJSON integer to modify.
valueNew value for integer.
Return values
0on success.
-1on error.

Definition at line 337 of file json.c.

338 {
339  return json_integer_set((json_t *)integer, value);
340 }
struct ast_json* ast_json_ipaddr ( const struct ast_sockaddr addr,
enum ast_transport  transport_type 
)

Construct an IP address as JSON.

XXX some comments describing the need for this here

Parameters
addrast_sockaddr to encode
transport_typeast_transport to include in the address string if any. Should just be one.
Returns
JSON string containing the IP address with optional transport information
Return values
NULLon error.

Definition at line 682 of file json.c.

References ast_json_string_create(), ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv4_mapped(), ast_sockaddr_stringify_addr(), ast_sockaddr_stringify_port(), ast_str_append(), ast_str_buffer(), and ast_str_set().

683 {
684  struct ast_str *string = ast_str_alloca(64);
685 
686  if (!string) {
687  return NULL;
688  }
689 
690  ast_str_set(&string, 0, (ast_sockaddr_is_ipv4(addr) ||
691  ast_sockaddr_is_ipv4_mapped(addr)) ? "IPV4/" : "IPV6/");
692 
693  if (transport_type) {
694  char *transport_string = NULL;
695 
696  /* NOTE: None will be applied if multiple transport types are specified in transport_type */
697  switch(transport_type) {
698  case AST_TRANSPORT_UDP:
699  transport_string = "UDP";
700  break;
701  case AST_TRANSPORT_TCP:
702  transport_string = "TCP";
703  break;
704  case AST_TRANSPORT_TLS:
705  transport_string = "TLS";
706  break;
707  case AST_TRANSPORT_WS:
708  transport_string = "WS";
709  break;
710  case AST_TRANSPORT_WSS:
711  transport_string = "WSS";
712  break;
713  }
714 
715  if (transport_string) {
716  ast_str_append(&string, 0, "%s/", transport_string);
717  }
718  }
719 
720  ast_str_append(&string, 0, "%s", ast_sockaddr_stringify_addr(addr));
721  ast_str_append(&string, 0, "/%s", ast_sockaddr_stringify_port(addr));
722 
723  return ast_json_string_create(ast_str_buffer(string));
724 }
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:286
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
static char * ast_sockaddr_stringify_port(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return a port only.
Definition: netsock2.h:358
Support for dynamic strings.
Definition: strings.h:623
int ast_sockaddr_is_ipv4_mapped(const struct ast_sockaddr *addr)
Determine if this is an IPv4-mapped IPv6 address.
Definition: netsock2.c:507
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:497
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
int ast_json_is_array ( const struct ast_json value)

Check if value is JSON array.

Since
12.0.0
Return values
True(non-zero) if value == ast_json_array().
False(zero) otherwise..

Definition at line 253 of file json.c.

254 {
255  return json_is_array((const json_t *)json);
256 }
int ast_json_is_false ( const struct ast_json value)

Check if value is JSON false.

Since
12.0.0
Return values
True(non-zero) if value == ast_json_false().
False(zero) otherwise.

Definition at line 268 of file json.c.

269 {
270  return json_is_false((const json_t *)json);
271 }
int ast_json_is_null ( const struct ast_json value)

Check if value is JSON null.

Since
12.0.0
Return values
True(non-zero) if value == ast_json_false().
False(zero) otherwise.

Definition at line 273 of file json.c.

Referenced by ast_manager_str_from_json_object().

274 {
275  return json_is_null((const json_t *)json);
276 }
int ast_json_is_object ( const struct ast_json value)

Check if value is JSON object.

Since
12.0.0
Return values
True(non-zero) if value == ast_json_object().
False(zero) otherwise..

Definition at line 258 of file json.c.

259 {
260  return json_is_object((const json_t *)json);
261 }
int ast_json_is_true ( const struct ast_json value)

Check if value is JSON true.

Since
12.0.0
Return values
True(non-zero) if value == ast_json_true().
False(zero) otherwise..

Definition at line 263 of file json.c.

Referenced by ast_ari_bridges_add_channel_parse_body(), ast_ari_bridges_record_parse_body(), ast_ari_channels_record_parse_body(), ast_ari_endpoints_refer_parse_body(), and ast_ari_endpoints_refer_to_endpoint_parse_body().

264 {
265  return json_is_true((const json_t *)json);
266 }
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.

Since
12.0.0
Parameters
bufferBuffer to parse.
buflenLength of buffer.
[out]errorFilled with information on error.
Returns
Parsed JSON element.
Return values
NULLon error.

Definition at line 585 of file json.c.

References copy_error().

Referenced by ast_ari_websocket_session_read(), and ast_http_get_json().

586 {
587  json_error_t jansson_error = {};
588  struct ast_json *r = (struct ast_json *)json_loadb(buffer, buflen, 0, &jansson_error);
589  copy_error(error, &jansson_error);
590  return r;
591 }
static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
Copy Jansson error struct to ours.
Definition: json.c:544
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_load_file ( FILE *  input,
struct ast_json_error error 
)

Parse a FILE into JSON object or array.

Since
12.0.0
Parameters
inputFILE to parse.
[out]errorFilled with information on error.
Returns
Parsed JSON element.
Return values
NULLon error.

Definition at line 592 of file json.c.

References copy_error().

593 {
594  json_error_t jansson_error = {};
595  struct ast_json *r = NULL;
596  if (input != NULL) {
597  r = (struct ast_json *)json_loadf(input, 0, &jansson_error);
598  copy_error(error, &jansson_error);
599  } else {
600  parse_error(error, "NULL input file", "<null>");
601  }
602  return r;
603 }
static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
Copy Jansson error struct to ours.
Definition: json.c:544
Abstract JSON element (object, array, string, int, ...).
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.

Since
12.0.0
Parameters
pathPath of file to parse.
[out]errorFilled with information on error.
Returns
Parsed JSON element.
Return values
NULLon error.

Definition at line 604 of file json.c.

References copy_error().

605 {
606  json_error_t jansson_error = {};
607  struct ast_json *r = (struct ast_json *)json_load_file(path, 0, &jansson_error);
608  copy_error(error, &jansson_error);
609  return r;
610 }
static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
Copy Jansson error struct to ours.
Definition: json.c:544
Abstract JSON element (object, array, string, int, ...).
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.

Since
12.0.0
Parameters
inputast_str to parse.
[out]errorFilled with information on error.
Returns
Parsed JSON element.
Return values
NULLon error.

Definition at line 580 of file json.c.

References ast_json_load_string(), and ast_str_buffer().

581 {
582  return ast_json_load_string(ast_str_buffer(input), error);
583 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
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:567
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.

Since
12.0.0
Parameters
inputString to parse.
[out]errorFilled with information on error.
Returns
Parsed JSON element.
Return values
NULLon error.

Definition at line 567 of file json.c.

References copy_error().

Referenced by ast_json_load_str(), and sorcery_astdb_retrieve_fields_common().

568 {
569  json_error_t jansson_error = {};
570  struct ast_json *r = NULL;
571  if (input != NULL) {
572  r = (struct ast_json *)json_loads(input, 0, &jansson_error);
573  copy_error(error, &jansson_error);
574  } else {
575  parse_error(error, "NULL input string", "<null>");
576  }
577  return r;
578 }
static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
Copy Jansson error struct to ours.
Definition: json.c:544
Abstract JSON element (object, array, string, int, ...).
void* ast_json_malloc ( size_t  size)

Asterisk's custom JSON allocator. Exposed for use by unit tests.

Since
12.0.0

Definition at line 47 of file json.c.

References ast_malloc.

Referenced by ast_json_reset_alloc_funcs(), and json_debug_malloc().

48 {
49  return ast_malloc(size);
50 }
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
struct ast_json* ast_json_name_number ( const char *  name,
const char *  number 
)

Common JSON rendering functions for common 'objects'.

Simple name/number pair.

Parameters
nameName
numberNumber
Returns
JSON object with name and number fields
Return values
NULLon error (non-UTF8 characters, NULL inputs, etc.)

Definition at line 646 of file json.c.

References ast_json_pack(), and AST_JSON_UTF8_VALIDATE.

Referenced by ast_channel_snapshot_to_json().

647 {
648  return ast_json_pack("{s: s, s: s}",
649  "name", AST_JSON_UTF8_VALIDATE(name),
650  "number", AST_JSON_UTF8_VALIDATE(number));
651 }
Number structure.
Definition: app_followme.c:154
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
#define AST_JSON_UTF8_VALIDATE(str)
Check str for UTF-8 and replace with an empty string if fails the check.
Definition: json.h:224
struct ast_json* ast_json_null ( void  )

Get the JSON null value.

Since
12.0.0

The returned value is a singleton, and does not need to be ast_json_unref()'ed.

Returns
JSON null.

Definition at line 248 of file json.c.

Referenced by ast_ari_recordings_get_stored_file(), ast_ari_response_accepted(), ast_ari_response_no_content(), ast_channel_publish_blob(), ast_channel_publish_cached_blob(), ast_endpoint_blob_create(), ast_json_dialplan_cep_app(), local_optimization_finished_cb(), and local_optimization_started_cb().

249 {
250  return (struct ast_json *)json_null();
251 }
Abstract JSON element (object, array, string, int, ...).
int ast_json_object_clear ( struct ast_json object)

Delete all elements from a JSON object.

Since
12.0.0
Parameters
objectJSON object to clear.
Return values
0on success.
-1on error.

Definition at line 422 of file json.c.

423 {
424  return json_object_clear((json_t *)object);
425 }
struct ast_json* ast_json_object_create ( void  )

Create a new JSON object.

Since
12.0.0
Returns
Newly allocated object.
Return values
NULLon error.

Definition at line 399 of file json.c.

Referenced by ast_ari_asterisk_get_info(), ast_ari_asterisk_get_module(), ast_bucket_file_json(), ast_json_channel_vars(), ast_json_object_create_vars(), ast_rtp_convert_stats_json(), ast_sorcery_objectset_json_create(), and stun_monitor_request().

400 {
401  return (struct ast_json *)json_object();
402 }
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_object_create_vars ( const struct ast_variable variables,
const char *  excludes 
)

Create a new JSON object using the given variables.

Parameters
variablesA list of Asterisk variables
excludesComma separated string of variable names to exclude (optional)
Returns
Newly allocated object.
Return values
NULLon error.

Definition at line 877 of file json.c.

References ast_in_delimited_string(), ast_json_object_create(), ast_json_object_set(), ast_json_string_create(), ast_variable::name, ast_variable::next, and ast_variable::value.

878 {
879  const struct ast_variable *i;
880  struct ast_json *obj;
881 
882  obj = ast_json_object_create();
883  if (!obj) {
884  return NULL;
885  }
886 
887  for (i = variables; i; i = i->next) {
888  if (!excludes || !ast_in_delimited_string(i->name, excludes, ',')) {
890  }
891  }
892 
893  return obj;
894 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
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:414
int ast_in_delimited_string(const char *needle, const char *haystack, char delim)
Check if there is an exact match for 'needle' between delimiters in 'haystack'.
Definition: strings.c:433
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
int ast_json_object_del ( struct ast_json object,
const char *  key 
)

Delete a field from a JSON object.

Since
12.0.0
Parameters
objectJSON object to modify.
keyKey of field to delete.
Return values
0on success, or -1 if key does not exist.

Definition at line 418 of file json.c.

419 {
420  return json_object_del((json_t *)object, key);
421 }
struct ast_json* ast_json_object_get ( struct ast_json object,
const char *  key 
)

Get a field from a JSON object.

Since
12.0.0

The returned element is a borrowed reference; use ast_json_ref() to safely keep a pointer to it.

Parameters
objectJSON object.
keyKey of field to look up.
Returns
Value with given key.
Return values
NULLon error.

Definition at line 407 of file json.c.

Referenced by app_send(), app_to_json(), ast_ari_applications_subscribe_parse_body(), ast_ari_applications_unsubscribe_parse_body(), ast_ari_asterisk_add_log_parse_body(), ast_ari_asterisk_get_global_var_parse_body(), ast_ari_asterisk_get_info_parse_body(), ast_ari_asterisk_set_global_var_parse_body(), ast_ari_asterisk_update_object(), ast_ari_bridges_add_channel_parse_body(), ast_ari_bridges_create_parse_body(), ast_ari_bridges_create_with_id_parse_body(), ast_ari_bridges_play_parse_body(), ast_ari_bridges_play_with_id_parse_body(), ast_ari_bridges_record_parse_body(), ast_ari_bridges_remove_channel_parse_body(), ast_ari_bridges_start_moh_parse_body(), ast_ari_channels_continue_in_dialplan_parse_body(), ast_ari_channels_create(), ast_ari_channels_create_parse_body(), ast_ari_channels_dial_parse_body(), ast_ari_channels_external_media(), ast_ari_channels_external_media_parse_body(), ast_ari_channels_get_channel_var_parse_body(), ast_ari_channels_hangup_parse_body(), ast_ari_channels_move_parse_body(), ast_ari_channels_mute_parse_body(), ast_ari_channels_originate(), ast_ari_channels_originate_parse_body(), ast_ari_channels_originate_with_id(), ast_ari_channels_originate_with_id_parse_body(), ast_ari_channels_play_parse_body(), ast_ari_channels_play_with_id_parse_body(), ast_ari_channels_record_parse_body(), ast_ari_channels_redirect_parse_body(), ast_ari_channels_send_dtmf_parse_body(), ast_ari_channels_set_channel_var_parse_body(), ast_ari_channels_snoop_channel_parse_body(), ast_ari_channels_snoop_channel_with_id_parse_body(), ast_ari_channels_start_moh_parse_body(), ast_ari_channels_unmute_parse_body(), ast_ari_device_states_update_parse_body(), ast_ari_endpoints_refer_parse_body(), ast_ari_endpoints_refer_to_endpoint_parse_body(), ast_ari_endpoints_send_message(), ast_ari_endpoints_send_message_parse_body(), ast_ari_endpoints_send_message_to_endpoint(), ast_ari_endpoints_send_message_to_endpoint_parse_body(), ast_ari_events_user_event(), ast_ari_events_user_event_parse_body(), ast_ari_mailboxes_update_parse_body(), ast_ari_playbacks_control_parse_body(), ast_ari_recordings_copy_stored_parse_body(), ast_ari_sounds_list_parse_body(), ast_ari_validate_event(), ast_ari_validate_message(), ast_endpoint_snapshot_to_json(), channel_dial_cb(), conf_send_event_to_participants(), create_sound_blob(), handle_dial_message(), mwi_app_event_cb(), stasis_app_exec(), and stasis_app_message_handler().

408 {
409  if (!key) {
410  return NULL;
411  }
412  return (struct ast_json *)json_object_get((json_t *)object, key);
413 }
Abstract JSON element (object, array, string, int, ...).
struct ast_json_iter* ast_json_object_iter ( struct ast_json object)

Get an iterator pointing to the first field in a JSON object.

Since
12.0.0

The order of the fields in an object are not specified. However, iterating forward from this iterator will cover all fields in object. Adding or removing fields from object may invalidate its iterators.

Parameters
objectJSON object.
Returns
Iterator to the first field in object.
Return values
NULLobject is empty. on error.

Definition at line 439 of file json.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_asterisk_info(), ast_ari_validate_asterisk_ping(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_build_info(), ast_ari_validate_caller_id(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_config_info(), ast_ari_validate_config_tuple(), ast_ari_validate_contact_info(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_dialed(), ast_ari_validate_dialplan_cep(), ast_ari_validate_endpoint(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_format_lang_pair(), ast_ari_validate_live_recording(), ast_ari_validate_log_channel(), ast_ari_validate_mailbox(), ast_ari_validate_message(), ast_ari_validate_missing_params(), ast_ari_validate_module(), ast_ari_validate_peer(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_rtpstat(), ast_ari_validate_set_id(), ast_ari_validate_sound(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_status_info(), ast_ari_validate_stored_recording(), ast_ari_validate_system_info(), ast_ari_validate_text_message(), ast_ari_validate_text_message_received(), ast_ari_validate_variable(), and ast_json_to_ast_variables().

440 {
441  return json_object_iter((json_t *)object);
442 }
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.

Since
12.0.0

Iterating forward from this iterator may not to cover all elements in object.

Parameters
objectJSON object to iterate.
keyKey of field to lookup.
Returns
Iterator pointing to the field with the given key.
Return values
NULLif key does not exist. on error.

Definition at line 443 of file json.c.

444 {
445  return json_object_iter_at((json_t *)object, key);
446 }
const char* ast_json_object_iter_key ( struct ast_json_iter iter)

Get the key from an iterator.

Since
12.0.0
Parameters
iterJSON object iterator.
Returns
Key of the field iter points to.

Definition at line 451 of file json.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_asterisk_info(), ast_ari_validate_asterisk_ping(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_build_info(), ast_ari_validate_caller_id(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_config_info(), ast_ari_validate_config_tuple(), ast_ari_validate_contact_info(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_dialed(), ast_ari_validate_dialplan_cep(), ast_ari_validate_endpoint(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_format_lang_pair(), ast_ari_validate_live_recording(), ast_ari_validate_log_channel(), ast_ari_validate_mailbox(), ast_ari_validate_message(), ast_ari_validate_missing_params(), ast_ari_validate_module(), ast_ari_validate_peer(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_rtpstat(), ast_ari_validate_set_id(), ast_ari_validate_sound(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_status_info(), ast_ari_validate_stored_recording(), ast_ari_validate_system_info(), ast_ari_validate_text_message(), ast_ari_validate_text_message_received(), ast_ari_validate_variable(), and ast_json_to_ast_variables().

452 {
453  return json_object_iter_key(iter);
454 }
struct ast_json_iter* ast_json_object_iter_next ( struct ast_json object,
struct ast_json_iter iter 
)

Get the next iterator.

Since
12.0.0
Parameters
objectJSON object iter was obtained from.
iterJSON object iterator.
Returns
Iterator to next field in object.
Return values
NULLif iter was the last field.

Definition at line 447 of file json.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_asterisk_info(), ast_ari_validate_asterisk_ping(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_build_info(), ast_ari_validate_caller_id(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_config_info(), ast_ari_validate_config_tuple(), ast_ari_validate_contact_info(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_dialed(), ast_ari_validate_dialplan_cep(), ast_ari_validate_endpoint(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_format_lang_pair(), ast_ari_validate_live_recording(), ast_ari_validate_log_channel(), ast_ari_validate_mailbox(), ast_ari_validate_message(), ast_ari_validate_missing_params(), ast_ari_validate_module(), ast_ari_validate_peer(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_rtpstat(), ast_ari_validate_set_id(), ast_ari_validate_sound(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_status_info(), ast_ari_validate_stored_recording(), ast_ari_validate_system_info(), ast_ari_validate_text_message(), ast_ari_validate_text_message_received(), ast_ari_validate_variable(), and ast_json_to_ast_variables().

448 {
449  return json_object_iter_next((json_t *)object, iter);
450 }
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.

Since
12.0.0
Note
The object steals the value reference even if it returns error; use ast_json_ref() to safely keep a pointer to it.
Parameters
objectJSON object iter was obtained from.
iterJSON object iterator.
valueJSON value to store in iter's field.
Return values
0on success.
-1on error.

Definition at line 459 of file json.c.

460 {
461  return json_object_iter_set_new((json_t *)object, iter, (json_t *)value);
462 }
struct ast_json* ast_json_object_iter_value ( struct ast_json_iter iter)

Get the value from an iterator.

Since
12.0.0

The returned element is a borrowed reference; use ast_json_ref() to safely keep a pointer to it.

Parameters
iterJSON object iterator.
Returns
Value of the field iter points to.

Definition at line 455 of file json.c.

Referenced by ast_ari_validate_application(), ast_ari_validate_application_move_failed(), ast_ari_validate_application_replaced(), ast_ari_validate_asterisk_info(), ast_ari_validate_asterisk_ping(), ast_ari_validate_bridge(), ast_ari_validate_bridge_attended_transfer(), ast_ari_validate_bridge_blind_transfer(), ast_ari_validate_bridge_created(), ast_ari_validate_bridge_destroyed(), ast_ari_validate_bridge_merged(), ast_ari_validate_bridge_video_source_changed(), ast_ari_validate_build_info(), ast_ari_validate_caller_id(), ast_ari_validate_channel(), ast_ari_validate_channel_caller_id(), ast_ari_validate_channel_connected_line(), ast_ari_validate_channel_created(), ast_ari_validate_channel_destroyed(), ast_ari_validate_channel_dialplan(), ast_ari_validate_channel_dtmf_received(), ast_ari_validate_channel_entered_bridge(), ast_ari_validate_channel_hangup_request(), ast_ari_validate_channel_hold(), ast_ari_validate_channel_left_bridge(), ast_ari_validate_channel_state_change(), ast_ari_validate_channel_talking_finished(), ast_ari_validate_channel_talking_started(), ast_ari_validate_channel_unhold(), ast_ari_validate_channel_userevent(), ast_ari_validate_channel_varset(), ast_ari_validate_config_info(), ast_ari_validate_config_tuple(), ast_ari_validate_contact_info(), ast_ari_validate_contact_status_change(), ast_ari_validate_device_state(), ast_ari_validate_device_state_changed(), ast_ari_validate_dial(), ast_ari_validate_dialplan_cep(), ast_ari_validate_endpoint(), ast_ari_validate_endpoint_state_change(), ast_ari_validate_event(), ast_ari_validate_format_lang_pair(), ast_ari_validate_live_recording(), ast_ari_validate_log_channel(), ast_ari_validate_mailbox(), ast_ari_validate_message(), ast_ari_validate_missing_params(), ast_ari_validate_module(), ast_ari_validate_peer(), ast_ari_validate_peer_status_change(), ast_ari_validate_playback(), ast_ari_validate_playback_continuing(), ast_ari_validate_playback_finished(), ast_ari_validate_playback_started(), ast_ari_validate_recording_failed(), ast_ari_validate_recording_finished(), ast_ari_validate_recording_started(), ast_ari_validate_rtpstat(), ast_ari_validate_set_id(), ast_ari_validate_sound(), ast_ari_validate_stasis_end(), ast_ari_validate_stasis_start(), ast_ari_validate_status_info(), ast_ari_validate_stored_recording(), ast_ari_validate_system_info(), ast_ari_validate_text_message(), ast_ari_validate_text_message_received(), ast_ari_validate_variable(), and ast_json_to_ast_variables().

456 {
457  return (struct ast_json *)json_object_iter_value(iter);
458 }
Abstract JSON element (object, array, string, int, ...).
int ast_json_object_set ( struct ast_json object,
const char *  key,
struct ast_json value 
)

Set a field in a JSON object.

Since
12.0.0
Note
The object steals the value reference even if it returns error; use ast_json_ref() to safely keep a pointer to it.
Parameters
objectJSON object to modify.
keyKey of field to set.
valueJSON value to set for field.
Return values
0on success.
-1on error.

Definition at line 414 of file json.c.

Referenced by app_send(), ast_ari_asterisk_get_info(), ast_bridge_snapshot_to_json(), ast_bucket_file_json(), ast_bucket_json(), ast_channel_snapshot_to_json(), ast_endpoint_snapshot_to_json(), ast_json_channel_vars(), ast_json_object_create_vars(), ast_rtp_convert_stats_json(), ast_sorcery_objectset_json_create(), identify_module(), stasis_app_event_filter_to_json(), stasis_app_message_handler(), stasis_app_recording_to_json(), and stasis_app_user_event().

415 {
416  return json_object_set_new((json_t *)object, key, (json_t *)value);
417 }
size_t ast_json_object_size ( struct ast_json object)

Get size of JSON object.

Since
12.0.0
Parameters
objectJSON object.
Returns
Size of object.
Return values
Zeroof object is not a JSON object.

Definition at line 403 of file json.c.

404 {
405  return json_object_size((json_t *)object);
406 }
int ast_json_object_update ( struct ast_json object,
struct ast_json other 
)

Update object with all of the fields of other.

Since
12.0.0

All of the fields of other are copied into object, overwriting existing keys. The other object is not changed, so ast_json_unref() it when you are done with it.

Parameters
objectJSON object to modify.
otherJSON object to copy into object.
Return values
0on success.
-1on error.

Definition at line 426 of file json.c.

427 {
428  return json_object_update((json_t *)object, (json_t *)other);
429 }
int ast_json_object_update_existing ( struct ast_json object,
struct ast_json other 
)

Update existing fields in object with the fields of other.

Since
12.0.0

Like ast_json_object_update(), but only existing fields are updated. No new fields will get added. The other object is not changed, so ast_json_unref() it when you are done with it.

Parameters
objectJSON object to modify.
otherJSON object to copy into object.
Return values
0on success.
-1on error.

Definition at line 430 of file json.c.

431 {
432  return json_object_update_existing((json_t *)object, (json_t *)other);
433 }
int ast_json_object_update_missing ( struct ast_json object,
struct ast_json other 
)

Add new fields to object with the fields of other.

Since
12.0.0

Like ast_json_object_update(), but only missing fields are added. No existing fields will be modified. The other object is not changed, so ast_json_unref() it when you are done with it.

Parameters
objectJSON object to modify.
otherJSON object to copy into object.
Return values
0on success.
-1on error.

Definition at line 434 of file json.c.

435 {
436  return json_object_update_missing((json_t *)object, (json_t *)other);
437 }
struct ast_json* ast_json_pack ( char const *  format,
  ... 
)

Helper for creating complex JSON values.

Since
12.0.0

See original Jansson docs at http://www.digip.org/jansson/doc/2.11/apiref.html#apiref-pack for more details.

Definition at line 612 of file json.c.

References ast_json_vpack().

Referenced by add_format_information_cb(), app_send_end_msg(), app_to_json(), app_update(), ast_ari_asterisk_get_global_var(), ast_ari_asterisk_get_info(), ast_ari_asterisk_ping(), ast_ari_channels_get_channel_var(), ast_ari_response_error(), ast_bridge_channel_write_hold(), ast_bridge_publish_enter(), ast_bridge_snapshot_to_json(), ast_cel_publish_event(), ast_cel_publish_user_event(), ast_channel_move(), ast_channel_publish_varset(), ast_channel_snapshot_to_json(), ast_endpoint_snapshot_to_json(), ast_json_dialplan_cep_app(), ast_json_name_number(), ast_json_party_id(), ast_manager_publish_event(), ast_queue_hangup_with_cause(), ast_queue_hold(), ast_rtcp_interpret(), ast_softhangup(), ast_system_publish_registry(), cel_channel_state_change(), confbridge_handle_atxfer(), create_sound_blob(), leave_queue(), local_optimization_finished_cb(), local_optimization_started_cb(), manager_mute_mixmonitor(), process_log_list(), process_module_list(), publish_cluster_discovery_to_stasis_full(), publish_dahdichannel(), publish_load_message_type(), really_quit(), record_abandoned(), report_fax_status(), report_receive_fax_status(), report_send_fax_status(), reset_user_pw(), ring_entry(), rna(), send_agent_complete(), stasis_app_device_state_to_json(), stasis_app_exec(), stasis_app_playback_to_json(), stasis_app_recording_to_json(), stasis_app_user_event(), and update_registry().

613 {
614  struct ast_json *ret;
615  va_list args;
616  va_start(args, format);
617  ret = ast_json_vpack(format, args);
618  va_end(args);
619  return ret;
620 }
struct ast_json * ast_json_vpack(char const *format, va_list ap)
Helper for creating complex JSON values simply.
Definition: json.c:621
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_party_id ( struct ast_party_id party)

Construct an ast_party_id as JSON.

Since
12.0.0
Parameters
partyThe party ID to represent as JSON.
Returns
JSON object with name, number and subaddress objects for those that are valid in the party ID

Definition at line 805 of file json.c.

References ast_describe_caller_presentation(), ast_json_pack(), ast_party_id_presentation(), ast_party_id::name, ast_party_id::number, and ast_party_id::subaddress.

806 {
807  int pres = ast_party_id_presentation(party);
808 
809  /* Combined party presentation */
810  return ast_json_pack("{s: i, s: s, s: o*, s: o*, s: o*}",
811  "presentation", pres,
812  "presentation_txt", ast_describe_caller_presentation(pres),
813  "number", json_party_number(&party->number),
814  "name", json_party_name(&party->name),
815  "subaddress", json_party_subaddress(&party->subaddress));
816 }
const char * ast_describe_caller_presentation(int data)
Convert caller ID pres value to explanatory string.
Definition: callerid.c:1364
struct ast_party_name name
Subscriber name.
Definition: channel.h:340
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_party_subaddress subaddress
Subscriber subaddress.
Definition: channel.h:344
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:342
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.

Since
12.0.0
Parameters
jsonthe ast_json blob we are loading
Returns
pointer to the ast_json_payload created
Return values
NULLif we fail to alloc it

Definition at line 756 of file json.c.

References ast_json_ref().

Referenced by ast_manager_publish_event(), ast_system_publish_registry(), publish_cluster_discovery_to_stasis_full(), publish_load_message_type(), and stun_monitor_request().

757 {
758  struct ast_json_payload *payload;
759 
760  if (!(payload = ao2_alloc(sizeof(*payload), json_payload_destructor))) {
761  return NULL;
762  }
763 
764  ast_json_ref(json);
765  payload->json = json;
766 
767  return payload;
768 }
struct ast_json * ast_json_ref(struct ast_json *json)
Increase refcount on value.
Definition: json.c:67
struct ast_json* ast_json_real_create ( double  value)

Create a JSON real number.

Since
12.0.0
Parameters
valueValue of the new JSON real number.
Returns
Newly allocated real number.
Return values
NULLon error.

Definition at line 342 of file json.c.

Referenced by ast_ari_asterisk_get_info(), and ast_rtp_convert_stats_json().

343 {
344  return (struct ast_json *)json_real(value);
345 }
Abstract JSON element (object, array, string, int, ...).
double ast_json_real_get ( const struct ast_json real)

Get the value from a JSON real number.

Since
12.0.0
Parameters
realJSON real number.
Returns
Value of a JSON real number.
Return values
0if real is not a JSON real number.

Definition at line 347 of file json.c.

348 {
349  return json_real_value((json_t *)real);
350 }
int ast_json_real_set ( struct ast_json real,
double  value 
)

Set the value of a JSON real number.

Since
12.0.0
Parameters
realJSON real number to modify.
valueNew value for real.
Return values
0on success.
-1on error.

Definition at line 352 of file json.c.

353 {
354  return json_real_set((json_t *)real, value);
355 }
struct ast_json* ast_json_ref ( struct ast_json value)
void ast_json_reset_alloc_funcs ( void  )

Change alloc funcs back to the resource module defaults.

Since
12.0.0

If you use ast_json_set_alloc_funcs() to temporarily change the allocator functions (i.e., from in a unit test), this function sets them back to ast_malloc() and ast_free().

Definition at line 62 of file json.c.

References ast_json_free(), and ast_json_malloc().

Referenced by ast_json_init().

63 {
64  json_set_alloc_funcs(ast_json_malloc, ast_json_free);
65 }
void * ast_json_malloc(size_t size)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:47
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
void ast_json_set_alloc_funcs ( void *(*)(size_t)  malloc_fn,
void(*)(void *)  free_fn 
)

Set custom allocators instead of the standard ast_malloc() and ast_free().

Since
12.0.0

This is used by the unit tests to do JSON specific memory leak detection. Since it affects all users of the JSON library, shouldn't normally be used.

Parameters
malloc_fnCustom allocation function.
free_fnMatching free function.

Definition at line 57 of file json.c.

58 {
59  json_set_alloc_funcs(malloc_fn, free_fn);
60 }
struct ast_json* ast_json_string_create ( const char *  value)

Construct a JSON string from value.

Since
12.0.0

The given value must be a valid ASCII or UTF-8 encoded string.

Parameters
valueValue of new JSON string.
Returns
Newly constructed string element.
Return values
NULLon error.

Definition at line 278 of file json.c.

Referenced by app_send(), app_to_json(), ast_bridge_snapshot_to_json(), ast_bucket_file_json(), ast_bucket_json(), ast_channel_snapshot_to_json(), ast_endpoint_snapshot_to_json(), ast_json_channel_vars(), ast_json_ipaddr(), ast_json_object_create_vars(), ast_json_timeval(), ast_rtp_convert_stats_json(), ast_sorcery_objectset_json_create(), identify_module(), report_receive_fax_status(), stasis_app_exec(), stasis_app_message_handler(), and stasis_app_user_event().

279 {
280  return (struct ast_json *)json_string(value);
281 }
Abstract JSON element (object, array, string, int, ...).
const char* ast_json_string_get ( const struct ast_json string)

Get the value of a JSON string.

Since
12.0.0
Parameters
stringJSON string.
Returns
Value of the string.
Return values
NULLon error.

Definition at line 283 of file json.c.

Referenced by app_send(), ast_ari_applications_subscribe_parse_body(), ast_ari_applications_unsubscribe_parse_body(), ast_ari_asterisk_add_log_parse_body(), ast_ari_asterisk_get_global_var_parse_body(), ast_ari_asterisk_get_info_parse_body(), ast_ari_asterisk_set_global_var_parse_body(), ast_ari_asterisk_update_object(), ast_ari_bridges_add_channel_parse_body(), ast_ari_bridges_create_parse_body(), ast_ari_bridges_create_with_id_parse_body(), ast_ari_bridges_play_parse_body(), ast_ari_bridges_play_with_id_parse_body(), ast_ari_bridges_record_parse_body(), ast_ari_bridges_remove_channel_parse_body(), ast_ari_bridges_start_moh_parse_body(), ast_ari_channels_continue_in_dialplan_parse_body(), ast_ari_channels_create_parse_body(), ast_ari_channels_dial_parse_body(), ast_ari_channels_external_media_parse_body(), ast_ari_channels_get_channel_var_parse_body(), ast_ari_channels_hangup_parse_body(), ast_ari_channels_move_parse_body(), ast_ari_channels_mute_parse_body(), ast_ari_channels_originate_parse_body(), ast_ari_channels_originate_with_id_parse_body(), ast_ari_channels_play_parse_body(), ast_ari_channels_play_with_id_parse_body(), ast_ari_channels_record_parse_body(), ast_ari_channels_redirect_parse_body(), ast_ari_channels_send_dtmf_parse_body(), ast_ari_channels_set_channel_var_parse_body(), ast_ari_channels_snoop_channel_parse_body(), ast_ari_channels_snoop_channel_with_id_parse_body(), ast_ari_channels_start_moh_parse_body(), ast_ari_channels_unmute_parse_body(), ast_ari_device_states_update_parse_body(), ast_ari_endpoints_refer_parse_body(), ast_ari_endpoints_refer_to_endpoint_parse_body(), ast_ari_endpoints_send_message_parse_body(), ast_ari_endpoints_send_message_to_endpoint_parse_body(), ast_ari_events_user_event_parse_body(), ast_ari_playbacks_control_parse_body(), ast_ari_recordings_copy_stored_parse_body(), ast_ari_sounds_list_parse_body(), ast_ari_validate_date(), ast_ari_validate_event(), ast_ari_validate_message(), ast_json_to_ast_variables(), channel_dial_cb(), handle_dial_message(), mwi_app_event_cb(), and stasis_app_message_handler().

284 {
285  return json_string_value((json_t *)string);
286 }
int ast_json_string_set ( struct ast_json string,
const char *  value 
)

Change the value of a JSON string.

Since
12.0.0

The given value must be a valid ASCII or UTF-8 encoded string.

Parameters
stringJSON string to modify.
valueNew value to store in string.
Return values
0on success.
-1on error.

Definition at line 288 of file json.c.

Referenced by conf_send_event_to_participants().

289 {
290  return json_string_set((json_t *)string, value);
291 }
struct ast_json* ast_json_stringf ( const char *  format,
  ... 
)

Create a JSON string, printf style.

Since
12.0.0

The formatted value must be a valid ASCII or UTF-8 encoded string.

Parameters
formatprintf style format string.
Returns
Newly allocated string.
Return values
NULLon error.

Definition at line 293 of file json.c.

References ast_json_vstringf().

294 {
295  struct ast_json *ret;
296  va_list args;
297  va_start(args, format);
298  ret = ast_json_vstringf(format, args);
299  va_end(args);
300  return ret;
301 }
struct ast_json * ast_json_vstringf(const char *format, va_list args)
Create a JSON string, vprintf style.
Definition: json.c:303
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_timeval ( const struct timeval  tv,
const char *  zone 
)

Construct a timeval as JSON.

JSON does not define a standard date format (boo), but the de facto standard is to use ISO 8601 formatted string. We build a millisecond resolution string from the timeval

Parameters
tvtimeval to encode.
zoneText string of a standard system zoneinfo file. If NULL, the system localtime will be used.
Returns
JSON string with ISO 8601 formatted date/time.
Return values
NULLon error.

Definition at line 670 of file json.c.

References AST_ISO8601_FORMAT, AST_ISO8601_LEN, ast_json_string_create(), ast_localtime(), and ast_strftime().

Referenced by app_send_end_msg(), app_update(), ast_ari_asterisk_get_info(), ast_ari_asterisk_ping(), ast_bridge_snapshot_to_json(), ast_channel_snapshot_to_json(), and stasis_app_exec().

671 {
672  char buf[AST_ISO8601_LEN];
673  struct ast_tm tm = {};
674 
675  ast_localtime(&tv, &tm, zone);
676 
677  ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
678 
679  return ast_json_string_create(buf);
680 }
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
#define AST_ISO8601_LEN
Max length of an null terminated, millisecond resolution, ISO8601 timestamp string.
Definition: localtime.h:105
#define AST_ISO8601_FORMAT
ast_strftime for ISO8601 formatting timestamps.
Definition: localtime.h:103
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
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.

Since
12.5.0
Parameters
json_variablesThe JSON blob containing the variable
variablesAn out reference to the variables to populate.
1 struct ast_json *json_variables = ast_json_pack("[ { s: s } ]", "foo", "bar");
2 struct ast_variable *variables = NULL;
3 int res;
4 
5 res = ast_json_to_ast_variables(json_variables, &variables);
Returns
Conversion enum ast_json_to_ast_vars_code status

Definition at line 818 of file json.c.

References ast_json_object_iter(), ast_json_object_iter_key(), ast_json_object_iter_next(), ast_json_object_iter_value(), ast_json_string_get(), AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE, AST_JSON_TO_AST_VARS_CODE_OOM, AST_JSON_TO_AST_VARS_CODE_SUCCESS, ast_json_typeof(), ast_variable_list_append_hint(), ast_variables_destroy(), and ast_variable::value.

Referenced by sorcery_astdb_retrieve_fields_common().

819 {
820  struct ast_json_iter *it_json_var;
821  struct ast_variable *tail = NULL;
822 
823  *variables = NULL;
824 
825  for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
826  it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
827  struct ast_variable *new_var;
828  const char *key = ast_json_object_iter_key(it_json_var);
829  const char *value;
830  struct ast_json *json_value;
831 
832  if (ast_strlen_zero(key)) {
833  continue;
834  }
835 
836  json_value = ast_json_object_iter_value(it_json_var);
837  if (ast_json_typeof(json_value) != AST_JSON_STRING) {
838  /* Error: Only strings allowed */
839  ast_variables_destroy(*variables);
840  *variables = NULL;
842  }
843  value = ast_json_string_get(json_value);
844  /* Should never be NULL. Otherwise, how could it be a string type? */
845  ast_assert(value != NULL);
846  if (!value) {
847  /* To be safe. */
848  continue;
849  }
850  new_var = ast_variable_new(key, value, "");
851  if (!new_var) {
852  /* Error: OOM */
853  ast_variables_destroy(*variables);
854  *variables = NULL;
856  }
857 
858  tail = ast_variable_list_append_hint(variables, tail, new_var);
859  }
860 
862 }
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
Definition: json.c:455
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
Iterator for JSON object key/values.
Structure for variables, used for configurations and for channel variables.
Conversion failed because invalid value type supplied.
Definition: json.h:1116
struct ast_variable * ast_variable_list_append_hint(struct ast_variable **head, struct ast_variable *search_hint, struct ast_variable *new_var)
Appends a variable list to the end of another list.
Definition: main/config.c:646
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
const char * ast_json_object_iter_key(struct ast_json_iter *iter)
Get the key from an iterator.
Definition: json.c:451
Conversion successful.
Definition: json.h:1111
enum ast_json_type ast_json_typeof(const struct ast_json *json)
Get the type of value.
Definition: json.c:78
Conversion failed because of allocation failure. (Out Of Memory)
Definition: json.h:1118
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:447
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:439
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_true ( void  )

Get the JSON true value.

Since
12.0.0

The returned value is a singleton, and does not need to be ast_json_unref()'ed.

Returns
JSON true.

Definition at line 233 of file json.c.

234 {
235  return (struct ast_json *)json_true();
236 }
Abstract JSON element (object, array, string, int, ...).
const char* ast_json_typename ( enum ast_json_type  type)

Get the string name for the given type.

Since
12.0.0
Parameters
typeType to convert to string.
Returns
Simple string for the type name (object, array, string, etc.)
Return values
?for invalid types.

Definition at line 95 of file json.c.

Referenced by ast_ari_validate_boolean().

96 {
97  switch (type) {
98  case AST_JSON_OBJECT: return "object";
99  case AST_JSON_ARRAY: return "array";
100  case AST_JSON_STRING: return "string";
101  case AST_JSON_INTEGER: return "integer";
102  case AST_JSON_REAL: return "real";
103  case AST_JSON_TRUE: return "boolean";
104  case AST_JSON_FALSE: return "boolean";
105  case AST_JSON_NULL: return "null";
106  }
107  ast_assert(0);
108  return "?";
109 }
enum ast_json_type ast_json_typeof ( const struct ast_json value)

Get the type of value.

Since
12.0.0
Parameters
valueValue to query.
Returns
Type of value.

Definition at line 78 of file json.c.

Referenced by ast_ari_applications_subscribe_parse_body(), ast_ari_applications_unsubscribe_parse_body(), ast_ari_asterisk_get_info_parse_body(), ast_ari_bridges_add_channel_parse_body(), ast_ari_bridges_play_parse_body(), ast_ari_bridges_play_with_id_parse_body(), ast_ari_bridges_remove_channel_parse_body(), ast_ari_channels_play_parse_body(), ast_ari_channels_play_with_id_parse_body(), ast_ari_events_user_event_parse_body(), ast_ari_validate_boolean(), and ast_json_to_ast_variables().

79 {
80  int r = json_typeof((json_t*)json);
81  switch(r) {
82  case JSON_OBJECT: return AST_JSON_OBJECT;
83  case JSON_ARRAY: return AST_JSON_ARRAY;
84  case JSON_STRING: return AST_JSON_STRING;
85  case JSON_INTEGER: return AST_JSON_INTEGER;
86  case JSON_REAL: return AST_JSON_REAL;
87  case JSON_TRUE: return AST_JSON_TRUE;
88  case JSON_FALSE: return AST_JSON_FALSE;
89  case JSON_NULL: return AST_JSON_NULL;
90  }
91  ast_assert(0); /* Unexpect return from json_typeof */
92  return r;
93 }
void ast_json_unref ( struct ast_json value)

Decrease refcount on value. If refcount reaches zero, value is freed.

Since
12.0.0
Note
It is safe to pass NULL to this function.

Definition at line 73 of file json.c.

Referenced by app_send_end_msg(), app_to_json(), app_update(), ari_bridges_play_found(), ast_aoc_manager_event(), ast_ari_applications_list(), ast_ari_applications_subscribe(), ast_ari_applications_unsubscribe(), ast_ari_asterisk_get_global_var(), ast_ari_asterisk_get_info(), ast_ari_asterisk_get_module(), ast_ari_asterisk_list_log_channels(), ast_ari_bridges_list(), ast_ari_bridges_record(), ast_ari_channels_get_channel_var(), ast_ari_channels_list(), ast_ari_endpoints_list(), ast_ari_endpoints_list_by_tech(), ast_ari_recordings_list_stored(), ast_ari_response_error(), ast_ari_sounds_list(), ast_ari_websocket_events_event_websocket_established(), ast_ari_websocket_session_read(), ast_bridge_channel_write_hold(), ast_bridge_publish_enter(), ast_bucket_file_json(), ast_bucket_json(), ast_cel_publish_event(), ast_cel_publish_user_event(), ast_channel_move(), ast_channel_publish_varset(), ast_endpoint_snapshot_to_json(), ast_manager_publish_event(), ast_queue_hangup_with_cause(), ast_queue_hold(), ast_rtcp_interpret(), ast_rtp_convert_stats_json(), ast_softhangup(), ast_sorcery_objectset_json_create(), ast_system_publish_registry(), cel_channel_state_change(), conf_send_event_to_participants(), confbridge_handle_atxfer(), create_sound_blob(), event_session_shutdown(), event_session_update_websocket(), leave_queue(), local_optimization_finished_cb(), local_optimization_started_cb(), manager_mute_mixmonitor(), publish_cluster_discovery_to_stasis_full(), publish_dahdichannel(), publish_load_message_type(), really_quit(), record_abandoned(), report_fax_status(), report_receive_fax_status(), report_send_fax_status(), reset_user_pw(), ring_entry(), rna(), send_agent_complete(), sorcery_astdb_retrieve_fields_common(), stasis_app_exec(), stasis_app_mailboxes_to_json(), stasis_app_playback_to_json(), stasis_app_recording_to_json(), stasis_app_user_event(), stun_monitor_request(), and update_registry().

74 {
75  json_decref((json_t *) json);
76 }
int ast_json_utf8_check ( const char *  str)

Check the nul terminated string for UTF-8 format.

Since
13.12.0
Parameters
strString to check.
Return values
0if not UTF-8 encoded or str is NULL.
1if UTF-8 encoded.

Definition at line 228 of file json.c.

References ast_json_utf8_check_len().

229 {
230  return str ? ast_json_utf8_check_len(str, strlen(str)) : 0;
231 }
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
int ast_json_utf8_check_len ( const char *  str,
size_t  len 
)

Check the string of the given length for UTF-8 format.

Since
13.12.0
Parameters
strString to check.
lenLength of string to check.
Return values
0if not UTF-8 encoded or str is NULL.
1if UTF-8 encoded.

Definition at line 186 of file json.c.

References ast_debug.

Referenced by ast_json_utf8_check().

187 {
188  size_t pos;
189  size_t count;
190  int res = 1;
191 
192  if (!str) {
193  return 0;
194  }
195 
196  /*
197  * Since the json library does not make the check function
198  * public we recreate/copy the function in our interface
199  * module.
200  *
201  * Loop ported from libjansson utf.c:utf8_check_string()
202  */
203  for (pos = 0; pos < len; pos += count) {
204  count = json_utf8_check_first(str[pos]);
205  if (count == 0) {
206  res = 0;
207  break;
208  } else if (count > 1) {
209  if (count > len - pos) {
210  /* UTF-8 needs more than we have left in the string. */
211  res = 0;
212  break;
213  }
214 
215  if (!json_utf8_check_full(&str[pos], count)) {
216  res = 0;
217  break;
218  }
219  }
220  }
221 
222  if (!res) {
223  ast_debug(1, "String '%.*s' is not UTF-8 for json conversion\n", (int) len, str);
224  }
225  return res;
226 }
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_json* ast_json_vpack ( char const *  format,
va_list  ap 
)

Helper for creating complex JSON values simply.

Since
12.0.0

See original Jansson docs at http://www.digip.org/jansson/doc/2.11/apiref.html#apiref-pack for more details.

Definition at line 621 of file json.c.

References ast_log_backtrace().

Referenced by ast_json_pack().

622 {
623  json_error_t error;
624  struct ast_json *r = NULL;
625  if (format) {
626  r = (struct ast_json *)json_vpack_ex(&error, 0, format, ap);
627  if (!r && !ast_strlen_zero(error.text)) {
628  ast_log(LOG_ERROR,
629  "Error building JSON from '%s': %s.\n",
630  format, error.text);
632  }
633  }
634  return r;
635 }
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition: logger.c:2510
Abstract JSON element (object, array, string, int, ...).
struct ast_json* ast_json_vstringf ( const char *  format,
va_list  args 
)

Create a JSON string, vprintf style.

Since
12.0.0

The formatted value must be a valid ASCII or UTF-8 encoded string.

Parameters
formatprintf style format string.
args
Returns
Newly allocated string.
Return values
NULLon error.

Definition at line 303 of file json.c.

References ast_vasprintf.

Referenced by ast_ari_response_error(), and ast_json_stringf().

304 {
305  json_t *ret = NULL;
306 
307  if (format) {
308  /* json_pack was not introduced until jansson-2.0 so Asterisk could never
309  * be compiled against older versions. The version check can never match
310  * anything older than 2.12. */
311 #if defined(HAVE_JANSSON_BUNDLED) || JANSSON_MAJOR_VERSION > 2 || JANSSON_MINOR_VERSION > 11
312  ret = json_vsprintf(format, args);
313 #else
314  char *str = NULL;
315  int err = ast_vasprintf(&str, format, args);
316 
317  if (err >= 0) {
318  ret = json_string(str);
319  ast_free(str);
320  }
321 #endif
322  }
323 
324  return (struct ast_json *)ret;
325 }
#define ast_vasprintf(ret, fmt, ap)
A wrapper for vasprintf()
Definition: astmm.h:278
Abstract JSON element (object, array, string, int, ...).