Move repeated #defines into tr_json_util.h and add documentation
[trust_router.git] / include / tr_json_util.h
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * 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 the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 /* Utilities for working with JSON/jansson */
36
37 #ifndef TRUST_ROUTER_TR_JSON_UTIL_H
38 #define TRUST_ROUTER_TR_JSON_UTIL_H
39
40 /**
41  * @def OBJECT_SET_OR_FAIL(job, key, val)
42  * Add a key/value pair to an object or jump to the cleanup label
43  * if the value is null.
44  *
45  * @param jobj JSON object instance to receive the key/value pair
46  * @param key key to set
47  * @param val value to set
48  */
49 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
50 do {                                           \
51   if (val)                                     \
52     json_object_set_new((jobj),(key),(val));   \
53   else                                         \
54     goto cleanup;                              \
55 } while (0)
56
57 /**
58  * @def OBJECT_SET_OR_SKIP(job, key, val)
59  * Add a key/value pair to an object only if it is non-null.
60  *
61  * @param jobj JSON object instance to receive the key/value pair
62  * @param key key to set
63  * @param val value to set
64  */
65 #define OBJECT_SET_OR_SKIP(jobj, key, val)     \
66 do {                                           \
67   if (val)                                     \
68     json_object_set_new((jobj),(key),(val));   \
69 } while (0)
70
71
72 /**
73  * @def ARRAY_APPEND_OR_FAIL(job, key, val)
74  * Append a value to an array or jump to the cleanup label
75  * if the value is null.
76  *
77  * @param jobj JSON array instance to receive the value
78  * @param val value to set
79  */
80 #define ARRAY_APPEND_OR_FAIL(jary, val)        \
81 do {                                           \
82   if (val)                                     \
83     json_array_append_new((jary),(val));       \
84   else                                         \
85     goto cleanup;                              \
86 } while (0)
87
88
89 #endif //TRUST_ROUTER_TR_JSON_UTIL_H