2 * Copyright (c) 2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <mon_internal.h>
40 /* Helper for encoding. Adds a newly allocated JSON object to
41 * jobj. If the allocation or setting fails, returns NULL after
43 #define object_set_or_free_and_return(jobj, tmp_jval, key, jval) \
45 (tmp_jval) = (jval); \
46 if ( (tmp_jval) == NULL) { \
50 if (json_object_set_new((jobj), (key), (tmp_jval)) == -1) { \
51 json_decref(tmp_jval); \
58 * Encode a monitoring response as a JSON object
60 * Caller must ensure json_decref() is used to free the return value.
62 * @param resp response to encode
63 * @return response as a newly allocated JSON object
65 json_t *mon_resp_encode(MON_RESP *resp)
67 json_t *resp_json = NULL;
70 /* Get a JSON object */
71 resp_json = json_object();
72 if (resp_json == NULL)
75 /* Add properties, cleaning up and returning NULL on failure */
76 object_set_or_free_and_return(resp_json, jval, "code", json_integer(resp->code));
77 object_set_or_free_and_return(resp_json, jval, "message", tr_name_to_json_string(resp->message));
79 /* If we have a payload, add it */
81 object_set_or_free_and_return(resp_json, jval, "payload", resp->payload);