]> git.proxmox.com Git - mirror_frr.git/blame - lib/json.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / json.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
f1aa5d8a
DS
2/* json-c wrapper
3 * Copyright (C) 2015 Cumulus Networks, Inc.
f1aa5d8a
DS
4 */
5
4b25d72d
DS
6#include <zebra.h>
7
8a2d6083 8#include "command.h"
f1aa5d8a
DS
9#include "lib/json.h"
10
db7c8528
DS
11/*
12 * This function assumes that the json keyword
13 * is the *last* keyword on the line no matter
14 * what.
15 */
9f049418 16bool use_json(const int argc, struct cmd_token *argv[])
db7c8528 17{
d62a17ae 18 if (argc == 0)
9f049418 19 return false;
db7c8528 20
d62a17ae 21 if (argv[argc - 1]->arg && strmatch(argv[argc - 1]->text, "json"))
9f049418 22 return true;
db7c8528 23
9f049418 24 return false;
db7c8528
DS
25}
26
2c4dfddb
DL
27struct json_object *json_object_new_stringv(const char *fmt, va_list args)
28{
29 struct json_object *ret;
30 char *text, buf[256];
31
32 text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), fmt, args);
33 ret = json_object_new_string(text);
34
35 if (text != buf)
36 XFREE(MTYPE_TMP, text);
37 return ret;
38}
39
a2339ed9
AK
40void json_array_string_add(json_object *json, const char *str)
41{
42 json_object_array_add(json, json_object_new_string(str));
43}
44
2c4dfddb
DL
45void json_array_string_addv(json_object *json, const char *fmt, va_list args)
46{
47 json_object_array_add(json, json_object_new_stringv(fmt, args));
48}
49
d62a17ae 50void json_object_string_add(struct json_object *obj, const char *key,
51 const char *s)
f1aa5d8a 52{
d62a17ae 53 json_object_object_add(obj, key, json_object_new_string(s));
f1aa5d8a
DS
54}
55
2c4dfddb
DL
56void json_object_string_addv(struct json_object *obj, const char *key,
57 const char *fmt, va_list args)
58{
59 json_object_object_add(obj, key, json_object_new_stringv(fmt, args));
60}
61
424ec384
DL
62void json_object_object_addv(struct json_object *parent,
63 struct json_object *child, const char *keyfmt,
64 va_list args)
65{
66 char *text, buf[256];
67
68 text = vasnprintfrr(MTYPE_TMP, buf, sizeof(buf), keyfmt, args);
69 json_object_object_add(parent, text, child);
70
71 if (text != buf)
72 XFREE(MTYPE_TMP, text);
73}
74
d62a17ae 75void json_object_int_add(struct json_object *obj, const char *key, int64_t i)
c6a7d59c 76{
d62a17ae 77 json_object_object_add(obj, key, json_object_new_int64(i));
c6a7d59c
DW
78}
79
799cc002
PG
80void json_object_double_add(struct json_object *obj, const char *key, double i)
81{
82 json_object_object_add(obj, key, json_object_new_double(i));
83}
84
d62a17ae 85void json_object_boolean_false_add(struct json_object *obj, const char *key)
f1aa5d8a 86{
d62a17ae 87 json_object_object_add(obj, key, json_object_new_boolean(0));
f1aa5d8a
DS
88}
89
d62a17ae 90void json_object_boolean_true_add(struct json_object *obj, const char *key)
f1aa5d8a 91{
d62a17ae 92 json_object_object_add(obj, key, json_object_new_boolean(1));
f1aa5d8a
DS
93}
94
e91c9247
QY
95void json_object_boolean_add(struct json_object *obj, const char *key, bool val)
96{
97 json_object_object_add(obj, key, json_object_new_boolean(val));
98}
99
d62a17ae 100struct json_object *json_object_lock(struct json_object *obj)
f1aa5d8a 101{
d62a17ae 102 return json_object_get(obj);
f1aa5d8a
DS
103}
104
d62a17ae 105void json_object_free(struct json_object *obj)
f1aa5d8a 106{
d62a17ae 107 json_object_put(obj);
f1aa5d8a 108}