]> git.proxmox.com Git - mirror_frr.git/blame - lib/json.c
debian: add pkg-config to build-depends
[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 */
ac4d0be5 32int use_json(const int argc, struct cmd_token *argv[])
db7c8528 33{
ac4d0be5 34 if (argc == 0)
35 return 0;
db7c8528 36
ac4d0be5 37 if (argv[argc - 1]->arg && strcmp(argv[argc - 1]->arg, "json") == 0)
38 return 1;
db7c8528 39
ac4d0be5 40 return 0;
db7c8528
DS
41}
42
ac4d0be5 43void json_object_string_add(struct json_object *obj, const char *key,
44 const char *s)
f1aa5d8a 45{
ac4d0be5 46 json_object_object_add(obj, key, json_object_new_string(s));
f1aa5d8a
DS
47}
48
ac4d0be5 49void json_object_int_add(struct json_object *obj, const char *key, int32_t i)
f1aa5d8a 50{
ac4d0be5 51 json_object_object_add(obj, key, json_object_new_int(i));
f1aa5d8a
DS
52}
53
ac4d0be5 54void json_object_long_add(struct json_object *obj, const char *key, int64_t i)
c6a7d59c 55{
873d76e7 56#if defined(HAVE_JSON_C_JSON_H)
ac4d0be5 57 json_object_object_add(obj, key, json_object_new_int64(i));
873d76e7 58#else
ac4d0be5 59 json_object_object_add(obj, key, json_object_new_int((int)i));
873d76e7 60#endif
c6a7d59c
DW
61}
62
ac4d0be5 63void json_object_boolean_false_add(struct json_object *obj, const char *key)
f1aa5d8a 64{
ac4d0be5 65 json_object_object_add(obj, key, json_object_new_boolean(0));
f1aa5d8a
DS
66}
67
ac4d0be5 68void json_object_boolean_true_add(struct json_object *obj, const char *key)
f1aa5d8a 69{
ac4d0be5 70 json_object_object_add(obj, key, json_object_new_boolean(1));
f1aa5d8a
DS
71}
72
ac4d0be5 73struct json_object *json_object_lock(struct json_object *obj)
f1aa5d8a 74{
ac4d0be5 75 return json_object_get(obj);
f1aa5d8a
DS
76}
77
ac4d0be5 78void json_object_free(struct json_object *obj)
f1aa5d8a 79{
ac4d0be5 80 json_object_put(obj);
f1aa5d8a 81}
2047fdb5
DS
82
83#if !defined(HAVE_JSON_C_JSON_H)
ac4d0be5 84int json_object_object_get_ex(struct json_object *obj, const char *key,
85 struct json_object **value)
2047fdb5 86{
ac4d0be5 87 *value = json_object_object_get(obj, key);
2047fdb5 88
ac4d0be5 89 if (*value)
90 return 1;
2047fdb5 91
ac4d0be5 92 return 0;
2047fdb5
DS
93}
94#endif