]> git.proxmox.com Git - mirror_frr.git/blob - lib/json.c
Merge pull request #12248 from pguibert6WIND/bgpasdot
[mirror_frr.git] / lib / json.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* json-c wrapper
3 * Copyright (C) 2015 Cumulus Networks, Inc.
4 */
5
6 #include <zebra.h>
7
8 #include "command.h"
9 #include "lib/json.h"
10
11 /*
12 * This function assumes that the json keyword
13 * is the *last* keyword on the line no matter
14 * what.
15 */
16 bool use_json(const int argc, struct cmd_token *argv[])
17 {
18 if (argc == 0)
19 return false;
20
21 if (argv[argc - 1]->arg && strmatch(argv[argc - 1]->text, "json"))
22 return true;
23
24 return false;
25 }
26
27 struct json_object *json_object_new_stringv(const char *fmt, va_list args)
28 {
29 struct json_object *ret;
30 char *text, buf[256];
31
32 text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), fmt, args);
33 ret = json_object_new_string(text);
34
35 if (text != buf)
36 XFREE(MTYPE_TMP, text);
37 return ret;
38 }
39
40 void json_array_string_add(json_object *json, const char *str)
41 {
42 json_object_array_add(json, json_object_new_string(str));
43 }
44
45 void json_array_string_addv(json_object *json, const char *fmt, va_list args)
46 {
47 json_object_array_add(json, json_object_new_stringv(fmt, args));
48 }
49
50 void json_object_string_add(struct json_object *obj, const char *key,
51 const char *s)
52 {
53 json_object_object_add(obj, key, json_object_new_string(s));
54 }
55
56 void json_object_string_addv(struct json_object *obj, const char *key,
57 const char *fmt, va_list args)
58 {
59 json_object_object_add(obj, key, json_object_new_stringv(fmt, args));
60 }
61
62 void json_object_object_addv(struct json_object *parent,
63 struct json_object *child, const char *keyfmt,
64 va_list args)
65 {
66 char *text, buf[256];
67
68 text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), keyfmt, args);
69 json_object_object_add(parent, text, child);
70
71 if (text != buf)
72 XFREE(MTYPE_TMP, text);
73 }
74
75 void json_object_int_add(struct json_object *obj, const char *key, int64_t i)
76 {
77 json_object_object_add(obj, key, json_object_new_int64(i));
78 }
79
80 void json_object_double_add(struct json_object *obj, const char *key, double i)
81 {
82 json_object_object_add(obj, key, json_object_new_double(i));
83 }
84
85 void json_object_boolean_false_add(struct json_object *obj, const char *key)
86 {
87 json_object_object_add(obj, key, json_object_new_boolean(0));
88 }
89
90 void json_object_boolean_true_add(struct json_object *obj, const char *key)
91 {
92 json_object_object_add(obj, key, json_object_new_boolean(1));
93 }
94
95 void json_object_boolean_add(struct json_object *obj, const char *key, bool val)
96 {
97 json_object_object_add(obj, key, json_object_new_boolean(val));
98 }
99
100 struct json_object *json_object_lock(struct json_object *obj)
101 {
102 return json_object_get(obj);
103 }
104
105 void json_object_free(struct json_object *obj)
106 {
107 json_object_put(obj);
108 }