2 * The olsr.org Optimized Link-State Routing daemon (olsrd)
4 * (c) by the OLSR project
6 * See our Git repository to find out who worked on this file
7 * and thus is a copyright holder on it.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
21 * * Neither the name of olsr.org, olsrd nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 * Visit http://www.olsr.org for more information.
40 * If you find this software useful feel free to make a donation
41 * to the project. For more information see the website or contact
42 * the copyright holders.
46 #include "json_helpers.h"
56 static const char * empty = "";
58 /* JSON support functions */
60 void abuf_json_reset_entry_number_and_depth(struct json_session *session, bool pretty) {
63 memset(session, 0, sizeof(*session));
64 session->pretty = pretty;
67 static void abuf_json_new_indent(struct json_session *session, struct autobuf *abuf) {
71 if (session->currentjsondepth) {
72 int i = session->currentjsondepth;
74 if (session->pretty) {
75 abuf_puts(abuf, "\n");
83 void abuf_json_insert_comma(struct json_session *session, struct autobuf *abuf) {
87 if (session->entrynumber[session->currentjsondepth])
88 abuf_appendf(abuf, ",");
91 void abuf_json_mark_output(struct json_session *session, bool open, struct autobuf *abuf) {
96 assert(!session->currentjsondepth);
97 abuf_json_new_indent(session, abuf);
99 session->currentjsondepth++;
100 session->entrynumber[session->currentjsondepth] = 0;
102 assert(session->currentjsondepth == 1);
103 session->entrynumber[session->currentjsondepth] = 0;
104 session->currentjsondepth--;
105 abuf_json_new_indent(session, abuf);
106 if (session->pretty) {
107 abuf_puts(abuf, "\n");
109 abuf_puts(abuf, "}");
113 void abuf_json_mark_object(struct json_session *session, bool open, bool array, struct autobuf *abuf, const char* header) {
118 abuf_json_insert_comma(session, abuf);
119 abuf_json_new_indent(session, abuf);
121 abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
123 abuf_appendf(abuf, "%s", array ? "[" : "{");
125 session->entrynumber[session->currentjsondepth]++;
126 session->currentjsondepth++;
127 assert(session->currentjsondepth < INFO_JSON_ENTRY_MAX_DEPTH);
128 session->entrynumber[session->currentjsondepth] = 0;
130 session->entrynumber[session->currentjsondepth] = 0;
131 session->currentjsondepth--;
132 assert(session->currentjsondepth >= 0);
133 abuf_json_new_indent(session, abuf);
134 abuf_appendf(abuf, "%s", array ? "]" : "}");
138 void abuf_json_mark_array_entry(struct json_session *session, bool open, struct autobuf *abuf) {
142 abuf_json_mark_object(session, open, false, abuf, NULL);
145 void abuf_json_boolean(struct json_session *session, struct autobuf *abuf, const char* key, bool value) {
150 abuf_json_insert_comma(session, abuf);
151 abuf_json_new_indent(session, abuf);
152 abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
153 session->entrynumber[session->currentjsondepth]++;
156 void abuf_json_string(struct json_session *session, struct autobuf *abuf, const char* key, const char* value) {
161 assert(key || value);
169 abuf_json_insert_comma(session, abuf);
170 abuf_json_new_indent(session, abuf);
172 abuf_appendf(abuf, "\"%s\"", value);
174 abuf_appendf(abuf, "\"%s\": \"%s\"", key, val);
176 session->entrynumber[session->currentjsondepth]++;
179 void abuf_json_int(struct json_session *session, struct autobuf *abuf, const char* key, long long value) {
187 fmt = "\"%s\": %lld";
192 abuf_json_insert_comma(session, abuf);
193 abuf_json_new_indent(session, abuf);
194 abuf_appendf(abuf, fmt, key, value);
195 session->entrynumber[session->currentjsondepth]++;
198 void abuf_json_float(struct json_session *session, struct autobuf *abuf, const char* key, double value) {
200 int isInf = isinf(v);
208 } else if (isInf < 0) {
210 } else if (isInf > 0) {
214 abuf_json_insert_comma(session, abuf);
215 abuf_json_new_indent(session, abuf);
216 abuf_appendf(abuf, "\"%s\": %f", key, v);
217 session->entrynumber[session->currentjsondepth]++;
220 void abuf_json_ip_address(struct json_session *session, struct autobuf *abuf, const char* key, union olsr_ip_addr *ip) {
221 struct ipaddr_str ipStr;
231 value = olsr_ip_to_string(&ipStr, ip);
234 abuf_json_insert_comma(session, abuf);
235 abuf_json_new_indent(session, abuf);
237 abuf_appendf(abuf, "\"%s\"", value);
239 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
241 session->entrynumber[session->currentjsondepth]++;
244 void abuf_json_ip_address46(struct json_session *session, struct autobuf *abuf, const char* key, void *ip, int af) {
245 struct ipaddr_str ipStr;
254 } else if (af == AF_INET) {
255 value = ip4_to_string(&ipStr, *((const struct in_addr*) ip));
257 value = ip6_to_string(&ipStr, (const struct in6_addr * const ) ip);
260 abuf_json_insert_comma(session, abuf);
261 abuf_json_new_indent(session, abuf);
263 abuf_appendf(abuf, "\"%s\"", value);
265 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
267 session->entrynumber[session->currentjsondepth]++;
270 void abuf_json_prefix(struct json_session *session, struct autobuf *abuf, const char* key, struct olsr_ip_prefix *prefix) {
271 struct ipaddr_str ipStr;
272 const char * value = empty;
273 int prefixLen = INT_MIN;
277 assert(key || prefix);
280 value = olsr_ip_to_string(&ipStr, &prefix->prefix);
281 prefixLen = prefix->prefix_len;
284 abuf_json_insert_comma(session, abuf);
285 abuf_json_new_indent(session, abuf);
287 abuf_appendf(abuf, "\"%s", value);
289 abuf_appendf(abuf, "\"%s\": \"%s", key, value);
291 if (prefixLen != INT_MIN) {
292 abuf_appendf(abuf, "/%d", prefixLen);
294 abuf_puts(abuf, "\"");
295 session->entrynumber[session->currentjsondepth]++;