Line data Source code
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 5 : bool use_json(const int argc, struct cmd_token *argv[])
17 : {
18 5 : if (argc == 0)
19 : return false;
20 :
21 5 : if (argv[argc - 1]->arg && strmatch(argv[argc - 1]->text, "json"))
22 5 : return true;
23 :
24 : return false;
25 : }
26 :
27 40 : struct json_object *json_object_new_stringv(const char *fmt, va_list args)
28 : {
29 40 : struct json_object *ret;
30 40 : char *text, buf[256];
31 :
32 40 : text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), fmt, args);
33 40 : ret = json_object_new_string(text);
34 :
35 40 : if (text != buf)
36 0 : XFREE(MTYPE_TMP, text);
37 40 : return ret;
38 : }
39 :
40 0 : void json_array_string_add(json_object *json, const char *str)
41 : {
42 0 : json_object_array_add(json, json_object_new_string(str));
43 0 : }
44 :
45 0 : void json_array_string_addv(json_object *json, const char *fmt, va_list args)
46 : {
47 0 : json_object_array_add(json, json_object_new_stringv(fmt, args));
48 0 : }
49 :
50 210 : void json_object_string_add(struct json_object *obj, const char *key,
51 : const char *s)
52 : {
53 210 : json_object_object_add(obj, key, json_object_new_string(s));
54 210 : }
55 :
56 40 : void json_object_string_addv(struct json_object *obj, const char *key,
57 : const char *fmt, va_list args)
58 : {
59 40 : json_object_object_add(obj, key, json_object_new_stringv(fmt, args));
60 40 : }
61 :
62 0 : void json_object_object_addv(struct json_object *parent,
63 : struct json_object *child, const char *keyfmt,
64 : va_list args)
65 : {
66 0 : char *text, buf[256];
67 :
68 0 : text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), keyfmt, args);
69 0 : json_object_object_add(parent, text, child);
70 :
71 0 : if (text != buf)
72 0 : XFREE(MTYPE_TMP, text);
73 0 : }
74 :
75 466 : void json_object_int_add(struct json_object *obj, const char *key, int64_t i)
76 : {
77 466 : json_object_object_add(obj, key, json_object_new_int64(i));
78 466 : }
79 :
80 0 : void json_object_double_add(struct json_object *obj, const char *key, double i)
81 : {
82 0 : json_object_object_add(obj, key, json_object_new_double(i));
83 0 : }
84 :
85 26 : void json_object_boolean_false_add(struct json_object *obj, const char *key)
86 : {
87 26 : json_object_object_add(obj, key, json_object_new_boolean(0));
88 26 : }
89 :
90 18 : void json_object_boolean_true_add(struct json_object *obj, const char *key)
91 : {
92 18 : json_object_object_add(obj, key, json_object_new_boolean(1));
93 18 : }
94 :
95 20 : void json_object_boolean_add(struct json_object *obj, const char *key, bool val)
96 : {
97 20 : json_object_object_add(obj, key, json_object_new_boolean(val));
98 20 : }
99 :
100 0 : struct json_object *json_object_lock(struct json_object *obj)
101 : {
102 0 : return json_object_get(obj);
103 : }
104 :
105 99 : void json_object_free(struct json_object *obj)
106 : {
107 99 : json_object_put(obj);
108 99 : }
|