]>
git.proxmox.com Git - mirror_frr.git/blob - lib/json.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Cumulus Networks, Inc.
15 #include <json-c/json.h>
18 * FRR style JSON iteration.
19 * Usage: JSON_FOREACH(...) { ... }
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)))
30 #define JSON_OBJECT_NEW_ARRAY(json_func, fields, n) \
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])); \
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
,
42 extern void json_object_int_add(struct json_object
*obj
, const char *key
,
44 void json_object_boolean_add(struct json_object
*obj
, const char *key
,
47 extern void json_object_double_add(struct json_object
*obj
, const char *key
,
49 extern void json_object_boolean_false_add(struct json_object
*obj
,
51 extern void json_object_boolean_true_add(struct json_object
*obj
,
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
);
57 /* printfrr => json helpers */
60 extern void json_object_string_addv(struct json_object
*obj
, const char *key
,
61 const char *fmt
, va_list args
);
63 static inline void json_object_string_addf(struct json_object
*obj
,
64 const char *key
, const char *fmt
,
70 json_object_string_addv(obj
, key
, fmt
, args
);
75 extern void json_array_string_addv(json_object
*json
, const char *fmt
,
78 static inline void json_array_string_addf(struct json_object
*obj
,
84 json_array_string_addv(obj
, fmt
, args
);
89 extern struct json_object
*json_object_new_stringv(const char *fmt
,
92 static inline struct json_object
*json_object_new_stringf(const char *fmt
, ...)
94 struct json_object
*ret
;
98 ret
= json_object_new_stringv(fmt
, args
);
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)
111 extern void json_object_object_addv(struct json_object
*parent
,
112 struct json_object
*child
,
113 const char *keyfmt
, va_list args
);
115 static inline void json_object_object_addf(struct json_object
*parent
,
116 struct json_object
*child
,
117 const char *keyfmt
, ...)
121 va_start(args
, keyfmt
);
122 json_object_object_addv(parent
, child
, keyfmt
, args
);
126 #define JSON_STR "JavaScript Object Notation\n"
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.
135 #ifndef JSON_C_TO_STRING_NOSLASHESCAPE
138 * Don't escape forward slashes.
140 #define JSON_C_TO_STRING_NOSLASHESCAPE (1<<4)
147 #endif /* _QUAGGA_JSON_H */