2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
42 #include "info_json_helpers.h"
54 #endif /* __linux__ */
56 static const char * empty = "";
58 /* JSON support functions */
60 /* JSON does not allow commas dangling at the end of arrays, so we need to
61 * count which entry number we're at in order to make sure we don't tack a
62 * dangling comma on at the end */
63 #define ENTRY_NUMBER_MAX_DEPTH 16
64 static int entrynumber[ENTRY_NUMBER_MAX_DEPTH] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
65 static int currentjsondepth = 0;
67 void abuf_json_reset_entry_number_and_depth(void) {
72 static void abuf_json_new_indent(struct autobuf *abuf) {
75 if (currentjsondepth) {
76 int i = currentjsondepth;
78 abuf_puts(abuf, "\n");
85 void abuf_json_insert_comma(struct autobuf *abuf) {
88 if (entrynumber[currentjsondepth])
89 abuf_appendf(abuf, ",");
92 void abuf_json_mark_output(bool open, struct autobuf *abuf) {
96 assert(!currentjsondepth);
97 abuf_json_new_indent(abuf);
100 entrynumber[currentjsondepth] = 0;
102 assert(currentjsondepth == 1);
103 entrynumber[currentjsondepth] = 0;
105 abuf_json_new_indent(abuf);
106 abuf_puts(abuf, "\n}");
110 void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header) {
114 abuf_json_insert_comma(abuf);
115 abuf_json_new_indent(abuf);
117 abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
119 abuf_appendf(abuf, "%s", array ? "[" : "{");
121 entrynumber[currentjsondepth]++;
123 assert(currentjsondepth < ENTRY_NUMBER_MAX_DEPTH);
124 entrynumber[currentjsondepth] = 0;
126 entrynumber[currentjsondepth] = 0;
128 assert(currentjsondepth >= 0);
129 abuf_json_new_indent(abuf);
130 abuf_appendf(abuf, "%s", array ? "]" : "}");
134 void abuf_json_mark_array_entry(bool open, struct autobuf *abuf) {
137 abuf_json_mark_object(open, false, abuf, NULL);
140 void abuf_json_boolean(struct autobuf *abuf, const char* key, bool value) {
144 abuf_json_insert_comma(abuf);
145 abuf_json_new_indent(abuf);
146 abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
147 entrynumber[currentjsondepth]++;
150 void abuf_json_string(struct autobuf *abuf, const char* key, const char* value) {
154 assert(key || value);
162 abuf_json_insert_comma(abuf);
163 abuf_json_new_indent(abuf);
165 abuf_appendf(abuf, "\"%s\"", value);
167 abuf_appendf(abuf, "\"%s\": \"%s\"", key, val);
169 entrynumber[currentjsondepth]++;
172 void abuf_json_int(struct autobuf *abuf, const char* key, long long value) {
179 fmt = "\"%s\": %lld";
184 abuf_json_insert_comma(abuf);
185 abuf_json_new_indent(abuf);
186 abuf_appendf(abuf, fmt, key, value);
187 entrynumber[currentjsondepth]++;
190 void abuf_json_float(struct autobuf *abuf, const char* key, double value) {
192 int isInf = isinf(v);
199 } else if (isInf < 0) {
201 } else if (isInf > 0) {
205 abuf_json_insert_comma(abuf);
206 abuf_json_new_indent(abuf);
207 abuf_appendf(abuf, "\"%s\": %f", key, v);
208 entrynumber[currentjsondepth]++;
211 void abuf_json_ip_address(struct autobuf *abuf, const char* key, union olsr_ip_addr *ip) {
212 struct ipaddr_str ipStr;
221 value = olsr_ip_to_string(&ipStr, ip);
224 abuf_json_insert_comma(abuf);
225 abuf_json_new_indent(abuf);
227 abuf_appendf(abuf, "\"%s\"", value);
229 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
231 entrynumber[currentjsondepth]++;
234 void abuf_json_ip_address46(struct autobuf *abuf, const char* key, void *ip, int af) {
235 struct ipaddr_str ipStr;
243 } else if (af == AF_INET) {
244 value = ip4_to_string(&ipStr, *((const struct in_addr*) ip));
246 value = ip6_to_string(&ipStr, (const struct in6_addr * const ) ip);
249 abuf_json_insert_comma(abuf);
250 abuf_json_new_indent(abuf);
252 abuf_appendf(abuf, "\"%s\"", value);
254 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
256 entrynumber[currentjsondepth]++;