]> git.proxmox.com Git - mirror_frr.git/blame - lib/json.c
Merge pull request #488 from donaldsharp/sudoers3
[mirror_frr.git] / lib / json.c
CommitLineData
f1aa5d8a
DS
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
4b25d72d
DS
22#include <zebra.h>
23
8a2d6083 24#include "command.h"
f1aa5d8a
DS
25#include "lib/json.h"
26
db7c8528
DS
27/*
28 * This function assumes that the json keyword
29 * is the *last* keyword on the line no matter
30 * what.
31 */
32int
4dcadbef 33use_json (const int argc, struct cmd_token *argv[])
db7c8528
DS
34{
35 if (argc == 0)
36 return 0;
37
8a2d6083 38 if (argv[argc-1]->arg && strcmp(argv[argc-1]->arg, "json") == 0)
db7c8528
DS
39 return 1;
40
41 return 0;
42}
43
f1aa5d8a
DS
44void
45json_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
51void
52json_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
c6a7d59c
DW
57void
58json_object_long_add(struct json_object* obj, const char *key, int64_t i)
59{
873d76e7 60#if defined(HAVE_JSON_C_JSON_H)
c6a7d59c 61 json_object_object_add(obj, key, json_object_new_int64(i));
873d76e7
DS
62#else
63 json_object_object_add(obj, key, json_object_new_int((int)i));
64#endif
c6a7d59c
DW
65}
66
f1aa5d8a
DS
67void
68json_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
73void
74json_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
79struct json_object*
80json_object_lock(struct json_object *obj)
81{
82 return json_object_get(obj);
83}
84
85void
86json_object_free(struct json_object *obj)
87{
88 json_object_put(obj);
89}
2047fdb5
DS
90
91#if !defined(HAVE_JSON_C_JSON_H)
92int
93json_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