]> git.proxmox.com Git - mirror_frr.git/blob - lib/json.c
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / lib / json.c
1 /* json-c wrapper
2 * Copyright (C) 2015 Cumulus Networks, Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "command.h"
25 #include "lib/json.h"
26
27 /*
28 * This function assumes that the json keyword
29 * is the *last* keyword on the line no matter
30 * what.
31 */
32 int
33 use_json (const int argc, struct cmd_token *argv[])
34 {
35 if (argc == 0)
36 return 0;
37
38 if (argv[argc-1]->arg && strcmp(argv[argc-1]->arg, "json") == 0)
39 return 1;
40
41 return 0;
42 }
43
44 void
45 json_object_string_add(struct json_object* obj, const char *key,
46 const char *s)
47 {
48 json_object_object_add(obj, key, json_object_new_string(s));
49 }
50
51 void
52 json_object_int_add(struct json_object* obj, const char *key, int32_t i)
53 {
54 json_object_object_add(obj, key, json_object_new_int(i));
55 }
56
57 void
58 json_object_long_add(struct json_object* obj, const char *key, int64_t i)
59 {
60 #if defined(HAVE_JSON_C_JSON_H)
61 json_object_object_add(obj, key, json_object_new_int64(i));
62 #else
63 json_object_object_add(obj, key, json_object_new_int((int)i));
64 #endif
65 }
66
67 void
68 json_object_boolean_false_add(struct json_object* obj, const char *key)
69 {
70 json_object_object_add(obj, key, json_object_new_boolean(0));
71 }
72
73 void
74 json_object_boolean_true_add(struct json_object* obj, const char *key)
75 {
76 json_object_object_add(obj, key, json_object_new_boolean(1));
77 }
78
79 struct json_object*
80 json_object_lock(struct json_object *obj)
81 {
82 return json_object_get(obj);
83 }
84
85 void
86 json_object_free(struct json_object *obj)
87 {
88 json_object_put(obj);
89 }
90
91 #if !defined(HAVE_JSON_C_JSON_H)
92 int
93 json_object_object_get_ex(struct json_object *obj,
94 const char *key,
95 struct json_object **value)
96 {
97 *value = json_object_object_get(obj, key);
98
99 if (*value)
100 return 1;
101
102 return 0;
103 }
104 #endif