]> git.proxmox.com Git - mirror_frr.git/blob - lib/json.h
Merge pull request #12845 from sri-mohan1/sri-mohan-ldp
[mirror_frr.git] / lib / json.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* json-c wrapper
3 * Copyright (C) 2015 Cumulus Networks, Inc.
4 */
5
6 #ifndef _QUAGGA_JSON_H
7 #define _QUAGGA_JSON_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 #include "command.h"
14 #include "printfrr.h"
15 #include <json-c/json.h>
16
17 /*
18 * FRR style JSON iteration.
19 * Usage: JSON_FOREACH(...) { ... }
20 */
21 #define JSON_FOREACH(jo, joi, join) \
22 /* struct json_object *jo; */ \
23 /* struct json_object_iterator joi; */ \
24 /* struct json_object_iterator join; */ \
25 for ((joi) = json_object_iter_begin((jo)), \
26 (join) = json_object_iter_end((jo)); \
27 json_object_iter_equal(&(joi), &(join)) == 0; \
28 json_object_iter_next(&(joi)))
29
30 #define JSON_OBJECT_NEW_ARRAY(json_func, fields, n) \
31 ({ \
32 struct json_object *_json_array = json_object_new_array(); \
33 for (int _i = 0; _i < (n); _i++) \
34 json_object_array_add(_json_array, \
35 (json_func)((fields)[_i])); \
36 (_json_array); \
37 })
38
39 extern bool use_json(const int argc, struct cmd_token *argv[]);
40 extern void json_object_string_add(struct json_object *obj, const char *key,
41 const char *s);
42 extern void json_object_int_add(struct json_object *obj, const char *key,
43 int64_t i);
44 void json_object_boolean_add(struct json_object *obj, const char *key,
45 bool val);
46
47 extern void json_object_double_add(struct json_object *obj, const char *key,
48 double i);
49 extern void json_object_boolean_false_add(struct json_object *obj,
50 const char *key);
51 extern void json_object_boolean_true_add(struct json_object *obj,
52 const char *key);
53 extern struct json_object *json_object_lock(struct json_object *obj);
54 extern void json_object_free(struct json_object *obj);
55 extern void json_array_string_add(json_object *json, const char *str);
56
57 /* printfrr => json helpers */
58
59 PRINTFRR(3, 0)
60 extern void json_object_string_addv(struct json_object *obj, const char *key,
61 const char *fmt, va_list args);
62 PRINTFRR(3, 4)
63 static inline void json_object_string_addf(struct json_object *obj,
64 const char *key, const char *fmt,
65 ...)
66 {
67 va_list args;
68
69 va_start(args, fmt);
70 json_object_string_addv(obj, key, fmt, args);
71 va_end(args);
72 }
73
74 PRINTFRR(2, 0)
75 extern void json_array_string_addv(json_object *json, const char *fmt,
76 va_list args);
77 PRINTFRR(2, 3)
78 static inline void json_array_string_addf(struct json_object *obj,
79 const char *fmt, ...)
80 {
81 va_list args;
82
83 va_start(args, fmt);
84 json_array_string_addv(obj, fmt, args);
85 va_end(args);
86 }
87
88 PRINTFRR(1, 0)
89 extern struct json_object *json_object_new_stringv(const char *fmt,
90 va_list args);
91 PRINTFRR(1, 2)
92 static inline struct json_object *json_object_new_stringf(const char *fmt, ...)
93 {
94 struct json_object *ret;
95 va_list args;
96
97 va_start(args, fmt);
98 ret = json_object_new_stringv(fmt, args);
99 va_end(args);
100
101 return ret;
102 }
103
104 /* NOTE: argument order differs! (due to varargs)
105 * json_object_object_add(parent, key, child)
106 * json_object_object_addv(parent, child, key, va)
107 * json_object_object_addf(parent, child, key, ...)
108 * (would be weird to have the child inbetween the format string and args)
109 */
110 PRINTFRR(3, 0)
111 extern void json_object_object_addv(struct json_object *parent,
112 struct json_object *child,
113 const char *keyfmt, va_list args);
114 PRINTFRR(3, 4)
115 static inline void json_object_object_addf(struct json_object *parent,
116 struct json_object *child,
117 const char *keyfmt, ...)
118 {
119 va_list args;
120
121 va_start(args, keyfmt);
122 json_object_object_addv(parent, child, keyfmt, args);
123 va_end(args);
124 }
125
126 #define JSON_STR "JavaScript Object Notation\n"
127
128 /* NOTE: json-c lib has following commit 316da85 which
129 * handles escape of forward slash.
130 * This allows prefix "20.0.14.0\/24":{
131 * to "20.0.14.0/24":{ some platforms do not have
132 * latest copy of json-c where defining below macro.
133 */
134
135 #ifndef JSON_C_TO_STRING_NOSLASHESCAPE
136
137 /**
138 * Don't escape forward slashes.
139 */
140 #define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
141 #endif
142
143 #ifdef __cplusplus
144 }
145 #endif
146
147 #endif /* _QUAGGA_JSON_H */