Debian Codegraph @

main/json.c

raw symbols SCIP
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 /*! \file
20 *
21 * \brief JSON abstraction layer.
22 *
23 * This is a very thin wrapper around the Jansson API. For more details on it,
24 * see its docs at http://www.digip.org/jansson/doc/2.11/apiref.html.
25 *
26 * \author David M. Lee, II <[email protected]>
27 */
28
29 /*** MODULEINFO
30 <depend>jansson</depend>
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/json.h"
37 #include "asterisk/localtime.h"
38 #include "asterisk/module.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/callerid.h"
43
44 #include <jansson.h>
45 #include <time.h>
46
47 void *ast_json_malloc(size_t size)
48 {
49 return ast_malloc(size);
50 }
51
52 void ast_json_free(void *p)
53 {
54 ast_free(p);
55 }
56
57 void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*))
58 {
59 json_set_alloc_funcs(malloc_fn, free_fn);
60 }
61
62 void ast_json_reset_alloc_funcs(void)
63 {
64 json_set_alloc_funcs(ast_json_malloc, ast_json_free);
65 }
66
67 struct ast_json *ast_json_ref(struct ast_json *json)
68 {
69 json_incref((json_t *)json);
70 return json;
71 }
72
73 void ast_json_unref(struct ast_json *json)
74 {
75 json_decref((json_t *) json);
76 }
77
78 enum ast_json_type ast_json_typeof(const struct ast_json *json)
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); /* Unexpected return from json_typeof */
92 return r;
93 }
94
95 const char *ast_json_typename(enum ast_json_type type)
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 }
110
111 /* Ported from libjansson utf.c:utf8_check_first() */
112 static size_t json_utf8_check_first(char byte)
113 {
114 unsigned char ch = (unsigned char) byte;
115
116 if (ch < 0x80) {
117 return 1;
118 }
119
120 if (0x80 <= ch && ch <= 0xBF) {
121 /* second, third or fourth byte of a multi-byte
122 sequence, i.e. a "continuation byte" */
123 return 0;
124 } else if (ch == 0xC0 || ch == 0xC1) {
125 /* overlong encoding of an ASCII byte */
126 return 0;
127 } else if (0xC2 <= ch && ch <= 0xDF) {
128 /* 2-byte sequence */
129 return 2;
130 } else if (0xE0 <= ch && ch <= 0xEF) {
131 /* 3-byte sequence */
132 return 3;
133 } else if (0xF0 <= ch && ch <= 0xF4) {
134 /* 4-byte sequence */
135 return 4;
136 } else { /* ch >= 0xF5 */
137 /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
138 UTF-8 */
139 return 0;
140 }
141 }
142
143 /* Ported from libjansson utf.c:utf8_check_full() */
144 static size_t json_utf8_check_full(const char *str, size_t len)
145 {
146 size_t pos;
147 int32_t value;
148 unsigned char ch = (unsigned char) str[0];
149
150 if (len == 2) {
151 value = ch & 0x1F;
152 } else if (len == 3) {
153 value = ch & 0xF;
154 } else if (len == 4) {
155 value = ch & 0x7;
156 } else {
157 return 0;
158 }
159
160 for (pos = 1; pos < len; ++pos) {
161 ch = (unsigned char) str[pos];
162 if (ch < 0x80 || ch > 0xBF) {
163 /* not a continuation byte */
164 return 0;
165 }
166
167 value = (value << 6) + (ch & 0x3F);
168 }
169
170 if (value > 0x10FFFF) {
171 /* not in Unicode range */
172 return 0;
173 } else if (0xD800 <= value && value <= 0xDFFF) {
174 /* invalid code point (UTF-16 surrogate halves) */
175 return 0;
176 } else if ((len == 2 && value < 0x80)
177 || (len == 3 && value < 0x800)
178 || (len == 4 && value < 0x10000)) {
179 /* overlong encoding */
180 return 0;
181 }
182
183 return 1;
184 }
185
186 int ast_json_utf8_check_len(const char *str, size_t len)
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 }
227
228 int ast_json_utf8_check(const char *str)
229 {
230 return str ? ast_json_utf8_check_len(str, strlen(str)) : 0;
231 }
232
233 struct ast_json *ast_json_true(void)
234 {
235 return (struct ast_json *)json_true();
236 }
237
238 struct ast_json *ast_json_false(void)
239 {
240 return (struct ast_json *)json_false();
241 }
242
243 struct ast_json *ast_json_boolean(int value)
244 {
245 return (struct ast_json *)json_boolean(value);
246 }
247
248 struct ast_json *ast_json_null(void)
249 {
250 return (struct ast_json *)json_null();
251 }
252
253 int ast_json_is_array(const struct ast_json *json)
254 {
255 return json_is_array((const json_t *)json);
256 }
257
258 int ast_json_is_object(const struct ast_json *json)
259 {
260 return json_is_object((const json_t *)json);
261 }
262
263 int ast_json_is_true(const struct ast_json *json)
264 {
265 return json_is_true((const json_t *)json);
266 }
267
268 int ast_json_is_false(const struct ast_json *json)
269 {
270 return json_is_false((const json_t *)json);
271 }
272
273 int ast_json_is_null(const struct ast_json *json)
274 {
275 return json_is_null((const json_t *)json);
276 }
277
278 struct ast_json *ast_json_string_create(const char *value)
279 {
280 return (struct ast_json *)json_string(value);
281 }
282
283 const char *ast_json_string_get(const struct ast_json *string)
284 {
285 return json_string_value((json_t *)string);
286 }
287
288 int ast_json_string_set(struct ast_json *string, const char *value)
289 {
290 return json_string_set((json_t *)string, value);
291 }
292
293 struct ast_json *ast_json_stringf(const char *format, ...)
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 }
302
303 struct ast_json *ast_json_vstringf(const char *format, va_list args)
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 }
326
327 struct ast_json *ast_json_integer_create(intmax_t value)
328 {
329 return (struct ast_json *)json_integer(value);
330 }
331
332 intmax_t ast_json_integer_get(const struct ast_json *integer)
333 {
334 return json_integer_value((json_t *)integer);
335 }
336
337 int ast_json_integer_set(struct ast_json *integer, intmax_t value)
338 {
339 return json_integer_set((json_t *)integer, value);
340 }
341
342 struct ast_json *ast_json_real_create(double value)
343 {
344 return (struct ast_json *)json_real(value);
345 }
346
347 double ast_json_real_get(const struct ast_json *real)
348 {
349 return json_real_value((json_t *)real);
350 }
351
352 int ast_json_real_set(struct ast_json *real, double value)
353 {
354 return json_real_set((json_t *)real, value);
355 }
356
357 int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
358 {
359 return json_equal((json_t *)lhs, (json_t *)rhs);
360 }
361
362 struct ast_json *ast_json_array_create(void)
363 {
364 return (struct ast_json *)json_array();
365 }
366 size_t ast_json_array_size(const struct ast_json *array)
367 {
368 return json_array_size((json_t *)array);
369 }
370 struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index)
371 {
372 return (struct ast_json *)json_array_get((json_t *)array, index);
373 }
374 int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
375 {
376 return json_array_set_new((json_t *)array, index, (json_t *)value);
377 }
378 int ast_json_array_append(struct ast_json *array, struct ast_json *value)
379 {
380 return json_array_append_new((json_t *)array, (json_t *)value);
381 }
382 int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
383 {
384 return json_array_insert_new((json_t *)array, index, (json_t *)value);
385 }
386 int ast_json_array_remove(struct ast_json *array, size_t index)
387 {
388 return json_array_remove((json_t *)array, index);
389 }
390 int ast_json_array_clear(struct ast_json *array)
391 {
392 return json_array_clear((json_t *)array);
393 }
394 int ast_json_array_extend(struct ast_json *array, struct ast_json *tail)
395 {
396 return json_array_extend((json_t *)array, (json_t *)tail);
397 }
398
399 struct ast_json *ast_json_object_create(void)
400 {
401 return (struct ast_json *)json_object();
402 }
403 size_t ast_json_object_size(struct ast_json *object)
404 {
405 return json_object_size((json_t *)object);
406 }
407 struct ast_json *ast_json_object_get(struct ast_json *object, const char *key)
408 {
409 if (!key) {
410 return NULL;
411 }
412 return (struct ast_json *)json_object_get((json_t *)object, key);
413 }
414 int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
415 {
416 return json_object_set_new((json_t *)object, key, (json_t *)value);
417 }
418 int ast_json_object_del(struct ast_json *object, const char *key)
419 {
420 return json_object_del((json_t *)object, key);
421 }
422 int ast_json_object_clear(struct ast_json *object)
423 {
424 return json_object_clear((json_t *)object);
425 }
426 int ast_json_object_update(struct ast_json *object, struct ast_json *other)
427 {
428 return json_object_update((json_t *)object, (json_t *)other);
429 }
430 int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
431 {
432 return json_object_update_existing((json_t *)object, (json_t *)other);
433 }
434 int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
435 {
436 return json_object_update_missing((json_t *)object, (json_t *)other);
437 }
438
439 struct ast_json_iter *ast_json_object_iter(struct ast_json *object)
440 {
441 return json_object_iter((json_t *)object);
442 }
443 struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key)
444 {
445 return json_object_iter_at((json_t *)object, key);
446 }
447 struct ast_json_iter *ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter)
448 {
449 return json_object_iter_next((json_t *)object, iter);
450 }
451 const char *ast_json_object_iter_key(struct ast_json_iter *iter)
452 {
453 return json_object_iter_key(iter);
454 }
455 struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter)
456 {
457 return (struct ast_json *)json_object_iter_value(iter);
458 }
459 int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
460 {
461 return json_object_iter_set_new((json_t *)object, iter, (json_t *)value);
462 }
463
464 /*!
465 * \brief Default flags for JSON encoding.
466 */
467 static size_t dump_flags(enum ast_json_encoding_format format)
468 {
469 size_t jansson_dump_flags;
470
471 if (format & AST_JSON_PRETTY) {
472 jansson_dump_flags = JSON_INDENT(2);
473 } else {
474 jansson_dump_flags = JSON_COMPACT;
475 }
476
477 if (format & AST_JSON_SORTED) {
478 jansson_dump_flags |= JSON_SORT_KEYS;
479 }
480
481 return jansson_dump_flags;
482 }
483
484 char *ast_json_dump_string_format(struct ast_json *root, enum ast_json_encoding_format format)
485 {
486 return json_dumps((json_t *)root, dump_flags(format));
487 }
488
489 static int write_to_ast_str(const char *buffer, size_t size, void *data)
490 {
491 struct ast_str **dst = data;
492 size_t str_size = ast_str_size(*dst);
493 size_t remaining = str_size - ast_str_strlen(*dst);
494 int ret;
495
496 /* While ast_str_append will grow the ast_str, it won't report
497 * allocation errors. Fortunately, it's not that hard.
498 */
499
500 /* Remaining needs to be big enough for buffer, plus null char */
501 while (remaining < size + 1) {
502 /* doubling the size of the buffer gives us 'amortized
503 * constant' time.
504 * See http://stackoverflow.com/a/249695/115478 for info.
505 */
506 str_size *= 2;
507 remaining = str_size - ast_str_strlen(*dst);
508 }
509
510 ret = ast_str_make_space(dst, str_size);
511 if (ret == -1) {
512 /* Could not alloc; fail */
513 return -1;
514 }
515
516 ast_str_append_substr(dst, -1, buffer, size);
517 return 0;
518 }
519
520 int ast_json_dump_str_format(struct ast_json *root, struct ast_str **dst, enum ast_json_encoding_format format)
521 {
522 return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags(format));
523 }
524
525
526 int ast_json_dump_file_format(struct ast_json *root, FILE *output, enum ast_json_encoding_format format)
527 {
528 if (!root || !output) {
529 return -1;
530 }
531 return json_dumpf((json_t *)root, output, dump_flags(format));
532 }
533 int ast_json_dump_new_file_format(struct ast_json *root, const char *path, enum ast_json_encoding_format format)
534 {
535 if (!root || !path) {
536 return -1;
537 }
538 return json_dump_file((json_t *)root, path, dump_flags(format));
539 }
540
541 /*!
542 * \brief Copy Jansson error struct to ours.
543 */
544 static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
545 {
546 if (error && jansson_error) {
547 error->line = jansson_error->line;
548 error->column = jansson_error->column;
549 error->position = jansson_error->position;
550 ast_copy_string(error->text, jansson_error->text, sizeof(error->text));
551 ast_copy_string(error->source, jansson_error->source, sizeof(error->source));
552 }
553
554 }
555
556 static void parse_error(struct ast_json_error *error, const char *text, const char *source)
557 {
558 if (error != NULL) {
559 error->line = 0;
560 error->column = 0;
561 error->position = 0;
562 strncpy(error->text, text, sizeof(error->text));
563 strncpy(error->source, source, sizeof(error->text));
564 }
565 }
566
567 struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error)
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 }
579
580 struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
581 {
582 return ast_json_load_string(ast_str_buffer(input), error);
583 }
584
585 struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
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 }
592 struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *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 }
604 struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *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 }
611
612 struct ast_json *ast_json_pack(char const *format, ...)
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 }
621 struct ast_json *ast_json_vpack(char const *format, va_list ap)
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);
631 ast_log_backtrace();
632 }
633 }
634 return r;
635 }
636
637 struct ast_json *ast_json_copy(const struct ast_json *value)
638 {
639 return (struct ast_json *)json_copy((json_t *)value);
640 }
641 struct ast_json *ast_json_deep_copy(const struct ast_json *value)
642 {
643 return (struct ast_json *)json_deep_copy((json_t *)value);
644 }
645
646 struct ast_json *ast_json_name_number(const char *name, const char *number)
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 }
652
653 struct ast_json *ast_json_dialplan_cep_app(
654 const char *context, const char *exten, int priority, const char *app_name, const char *app_data)
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 }
664
665 struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
666 {
667 return ast_json_dialplan_cep_app(context, exten, priority, "", "");
668 }
669
670 struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone)
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 }
681
682 struct ast_json *ast_json_ipaddr(const struct ast_sockaddr *addr, enum ast_transport transport_type)
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 }
725
726 int ast_json_init(void)
727 {
728 json_t *version_check;
729
730 /* Setup to use Asterisk custom allocators */
731 ast_json_reset_alloc_funcs();
732
733 /* We depend on functionality of jansson-2.11 but don't actually use
734 * any symbols. If we link at runtime to less than 2.11 this json_pack
735 * will return NULL. */
736 version_check = json_pack("{s: o?, s: o*}",
737 "JSON", NULL,
738 "Bourne", NULL);
739 if (!version_check) {
740 ast_log(LOG_ERROR, "There was a problem finding jansson 2.11 runtime libraries.\n"
741 "Please rebuild Asterisk using ./configure --with-jansson-bundled.\n");
742 return -1;
743 }
744
745 json_decref(version_check);
746
747 return 0;
748 }
749
750 static void json_payload_destructor(void *obj)
751 {
752 struct ast_json_payload *payload = obj;
753 ast_json_unref(payload->json);
754 }
755
756 struct ast_json_payload *ast_json_payload_create(struct ast_json *json)
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 }
769
770 static struct ast_json *json_party_number(struct ast_party_number *number)
771 {
772 if (!number->valid) {
773 return NULL;
774 }
775 return ast_json_pack("{s: s, s: i, s: i, s: s}",
776 "number", AST_JSON_UTF8_VALIDATE(number->str),
777 "plan", number->plan,
778 "presentation", number->presentation,
779 "presentation_txt", ast_describe_caller_presentation(number->presentation));
780 }
781
782 static struct ast_json *json_party_name(struct ast_party_name *name)
783 {
784 if (!name->valid) {
785 return NULL;
786 }
787 return ast_json_pack("{s: s, s: s, s: i, s: s}",
788 "name", AST_JSON_UTF8_VALIDATE(name->str),
789 "character_set", ast_party_name_charset_describe(name->char_set),
790 "presentation", name->presentation,
791 "presentation_txt", ast_describe_caller_presentation(name->presentation));
792 }
793
794 static struct ast_json *json_party_subaddress(struct ast_party_subaddress *subaddress)
795 {
796 if (!subaddress->valid) {
797 return NULL;
798 }
799 return ast_json_pack("{s: s, s: i, s: b}",
800 "subaddress", AST_JSON_UTF8_VALIDATE(subaddress->str),
801 "type", subaddress->type,
802 "odd", subaddress->odd_even_indicator);
803 }
804
805 struct ast_json *ast_json_party_id(struct ast_party_id *party)
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 }
817
818 enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
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;
841 return AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE;
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;
855 return AST_JSON_TO_AST_VARS_CODE_OOM;
856 }
857
858 tail = ast_variable_list_append_hint(variables, tail, new_var);
859 }
860
861 return AST_JSON_TO_AST_VARS_CODE_SUCCESS;
862 }
863
864 enum ast_json_nvp_ast_vars_code ast_json_nvp_array_to_ast_variables(
865 struct ast_json *json_variables, struct ast_variable **variables)
866 {
867 struct ast_variable *tail = NULL;
868 int i = 0;
869 size_t len = json_variables ? ast_json_array_size(json_variables) : 0;
870
871 if (len == 0) {
872 return AST_JSON_NVP_AST_VARS_CODE_NO_INPUT;
873 }
874
875 for (i = 0; i < len; i++) {
876 struct ast_variable *new_var;
877 struct ast_json *json_value;
878 struct ast_json *json_key;
879 const char *key;
880 const char *value;
881
882 json_value = ast_json_array_get(json_variables, i);
883 if (!json_value || ast_json_is_null(json_value) || ast_json_typeof(json_value) != AST_JSON_OBJECT) {
884 /* Error: Only objects allowed */
885 return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
886 }
887
888 json_key = ast_json_object_get(json_value, "name");
889 if (!json_key || ast_json_is_null(json_key) || ast_json_typeof(json_key) != AST_JSON_STRING) {
890 /* Error: Only strings allowed */
891 return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
892 }
893 key = ast_json_string_get(json_key);
894
895 json_key = ast_json_object_get(json_value, "value");
896 if (!json_key || ast_json_is_null(json_key) || ast_json_typeof(json_key) != AST_JSON_STRING) {
897 /* Error: Only strings allowed */
898 return AST_JSON_NVP_AST_VARS_CODE_INVALID_TYPE;
899 }
900 value = ast_json_string_get(json_key);
901
902 new_var = ast_variable_new(key, value, "");
903 if (!new_var) {
904 /* Error: OOM */
905 return AST_JSON_NVP_AST_VARS_CODE_OOM;
906 }
907
908 tail = ast_variable_list_append_hint(variables, tail, new_var);
909 }
910
911 return AST_JSON_NVP_AST_VARS_CODE_SUCCESS;
912 }
913
914 struct ast_json *ast_variables_to_json_nvp_array(struct ast_variable *variables)
915 {
916 struct ast_variable *v = NULL;
917 struct ast_json *json_variables = ast_json_array_create();
918
919 if (!variables || !json_variables) {
920 return NULL;
921 }
922
923 for (v = variables; v; v = v->next) {
924 struct ast_json *obj = ast_json_pack("{s: s, s: s}",
925 "name", v->name,
926 "value", v->value);
927 if (!obj) {
928 ast_json_unref(json_variables);
929 return NULL;
930 }
931 if (ast_json_array_append(json_variables, obj)) {
932 ast_json_unref(json_variables);
933 ast_json_unref(obj);
934 return NULL;
935 }
936 }
937
938 return json_variables;
939 }
940
941 struct ast_json *ast_json_channel_vars(struct varshead *channelvars)
942 {
943 struct ast_json *ret;
944 struct ast_var_t *var;
945
946 ret = ast_json_object_create();
947 AST_LIST_TRAVERSE(channelvars, var, entries) {
948 ast_json_object_set(ret, var->name, ast_json_string_create(var->value));
949 }
950
951 return ret;
952 }
953
954 struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)
955 {
956 const struct ast_variable *i;
957 struct ast_json *obj;
958
959 obj = ast_json_object_create();
960 if (!obj) {
961 return NULL;
962 }
963
964 for (i = variables; i; i = i->next) {
965 if (!excludes || !ast_in_delimited_string(i->name, excludes, ',')) {
966 ast_json_object_set(obj, i->name, ast_json_string_create(i->value));
967 }
968 }
969
970 return obj;
971 }