]> git.proxmox.com Git - mirror_frr.git/blame - lib/json.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / json.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
f1aa5d8a
DS
2/* json-c wrapper
3 * Copyright (C) 2015 Cumulus Networks, Inc.
f1aa5d8a
DS
4 */
5
6#ifndef _QUAGGA_JSON_H
7#define _QUAGGA_JSON_H
8
5e244469
RW
9#ifdef __cplusplus
10extern "C" {
11#endif
12
7bdafd3b 13#include "command.h"
2c4dfddb 14#include "printfrr.h"
112072ac 15#include <json-c/json.h>
8519fe88
RZ
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
0b11b56a
DS
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
9f049418 39extern bool use_json(const int argc, struct cmd_token *argv[]);
d62a17ae 40extern void json_object_string_add(struct json_object *obj, const char *key,
41 const char *s);
42extern void json_object_int_add(struct json_object *obj, const char *key,
43 int64_t i);
2fff50ec
QY
44void json_object_boolean_add(struct json_object *obj, const char *key,
45 bool val);
799cc002
PG
46
47extern void json_object_double_add(struct json_object *obj, const char *key,
48 double i);
d62a17ae 49extern void json_object_boolean_false_add(struct json_object *obj,
50 const char *key);
51extern void json_object_boolean_true_add(struct json_object *obj,
52 const char *key);
53extern struct json_object *json_object_lock(struct json_object *obj);
f1aa5d8a 54extern void json_object_free(struct json_object *obj);
a2339ed9 55extern void json_array_string_add(json_object *json, const char *str);
f1aa5d8a 56
2c4dfddb
DL
57/* printfrr => json helpers */
58
59PRINTFRR(3, 0)
60extern void json_object_string_addv(struct json_object *obj, const char *key,
61 const char *fmt, va_list args);
62PRINTFRR(3, 4)
63static 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
74PRINTFRR(2, 0)
75extern void json_array_string_addv(json_object *json, const char *fmt,
76 va_list args);
77PRINTFRR(2, 3)
78static 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
88PRINTFRR(1, 0)
89extern struct json_object *json_object_new_stringv(const char *fmt,
90 va_list args);
91PRINTFRR(1, 2)
92static 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}
a8dfd147 103
424ec384
DL
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 */
110PRINTFRR(3, 0)
111extern void json_object_object_addv(struct json_object *parent,
112 struct json_object *child,
113 const char *keyfmt, va_list args);
114PRINTFRR(3, 4)
115static 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
51f8c3f2
DS
126#define JSON_STR "JavaScript Object Notation\n"
127
0f478e30
CS
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
5e244469
RW
143#ifdef __cplusplus
144}
145#endif
146
f1aa5d8a 147#endif /* _QUAGGA_JSON_H */