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