1 /* json_support.h -- Helper functions for jansson and JSON
2  *
3  * Copyright (c) 1994-2019 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  *
42  */
43 
44 
45 #ifndef JSON_SUPPORT_H
46 #define JSON_SUPPORT_H
47 
48 #include <config.h>
49 #include <jansson.h>
50 
51 #include "util.h"
52 
53 #define JNOTNULL(item)          ((item) ? (json_is_null(item) == 0) : 0)
54 
55 /* jansson replacement functions for those missing in older versions */
56 
57 #ifndef json_boolean
58 #define json_boolean(val)       ((val) ? json_true() : json_false())
59 #endif /* json_boolean */
60 
61 #ifndef json_boolean_value
62 #define json_boolean_value(val) ((val) == json_true() ? 1 : 0)
63 #endif /* json_boolean_value */
64 
65 #ifndef json_object_foreach
66 #define json_object_foreach(obj, key, val)                      \
67      void *_iter_;                                              \
68      for (_iter_ = json_object_iter(obj);                       \
69           _iter_                                                \
70               && (key = json_object_iter_key(_iter_))           \
71               && (val = json_object_iter_value(_iter_));        \
72           _iter_ = json_object_iter_next(obj, _iter_))
73 #endif /* json_object_foreach */
74 
75 #ifndef json_object_foreach_safe
76 #define json_object_foreach_safe(object, n, key, value)     \
77     for(key = json_object_iter_key(json_object_iter(object)), \
78             n = json_object_iter_next(object, json_object_key_to_iter(key)); \
79         key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
80         key = json_object_iter_key(n), \
81 n = json_object_iter_next(object, json_object_key_to_iter(key)))
82 #endif /* json_object_foreach_safe */
83 
84 #ifndef json_array_foreach
85 #define json_array_foreach(array, index, value)                 \
86     for (index = 0;                                             \
87          index < json_array_size(array)                         \
88              && (value = json_array_get(array, index));         \
89          index++)
90 #endif /* json_array_foreach */
91 
92 EXPORTED int json_is_utcdate(json_t *json);
93 
94 EXPORTED int json_array_find(json_t *array, const char *needle);
95 
96 #ifdef NEED_JANSSON_JSON_DUMPB
97 /* https://jansson.readthedocs.io/en/2.11/apiref.html#c.json_dumpb */
98 EXPORTED size_t json_dumpb(const json_t *json,
99                            char *buffer, size_t size, size_t flags);
100 #endif
101 
102 #endif /* JSON_SUPPORT_H */
103