Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/include/sudo_json.h
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2013-2020 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#ifndef SUDO_JSON_H
20
#define SUDO_JSON_H
21
22
#include <sys/types.h> /* for id_t */
23
24
#ifdef HAVE_STDBOOL_H
25
# include <stdbool.h>
26
#else
27
# include <compat/stdbool.h>
28
#endif
29
30
/*
31
* JSON values may be of the following types.
32
*/
33
enum json_value_type {
34
JSON_STRING,
35
JSON_ID,
36
JSON_NUMBER,
37
JSON_OBJECT,
38
JSON_ARRAY,
39
JSON_BOOL,
40
JSON_NULL
41
};
42
43
/*
44
* JSON value suitable for printing.
45
* Note: this does not support object values.
46
*/
47
struct json_value {
48
enum json_value_type type;
49
union {
50
const char *string;
51
long long number;
52
id_t id;
53
bool boolean;
54
} u;
55
};
56
57
struct json_container {
58
char *buf;
59
unsigned int buflen;
60
unsigned int bufsize;
61
unsigned int indent_level;
62
unsigned int indent_increment;
63
bool minimal;
64
bool memfatal;
65
bool need_comma;
66
bool quiet;
67
};
68
69
sudo_dso_public bool sudo_json_init_v1(struct json_container *jsonc, unsigned int indent, bool minimal, bool memfatal);
70
sudo_dso_public bool sudo_json_init_v2(struct json_container *jsonc, unsigned int indent, bool minimal, bool memfatal, bool quiet);
71
#define sudo_json_init(_a, _b, _c, _d, _e) sudo_json_init_v2((_a), (_b), (_c), (_d), (_e))
72
73
sudo_dso_public void sudo_json_free_v1(struct json_container *jsonc);
74
#define sudo_json_free(_a) sudo_json_free_v1((_a))
75
76
sudo_dso_public bool sudo_json_open_object_v1(struct json_container *jsonc, const char *name);
77
#define sudo_json_open_object(_a, _b) sudo_json_open_object_v1((_a), (_b))
78
79
sudo_dso_public bool sudo_json_close_object_v1(struct json_container *jsonc);
80
#define sudo_json_close_object(_a) sudo_json_close_object_v1((_a))
81
82
sudo_dso_public bool sudo_json_open_array_v1(struct json_container *jsonc, const char *name);
83
#define sudo_json_open_array(_a, _b) sudo_json_open_array_v1((_a), (_b))
84
85
sudo_dso_public bool sudo_json_close_array_v1(struct json_container *jsonc);
86
#define sudo_json_close_array(_a) sudo_json_close_array_v1((_a))
87
88
sudo_dso_public bool sudo_json_add_value_v1(struct json_container *jsonc, const char *name, struct json_value *value);
89
#define sudo_json_add_value(_a, _b, _c) sudo_json_add_value_v1((_a), (_b), (_c))
90
91
sudo_dso_public bool sudo_json_add_value_as_object_v1(struct json_container *jsonc, const char *name, struct json_value *value);
92
#define sudo_json_add_value_as_object(_a, _b, _c) sudo_json_add_value_as_object_v1((_a), (_b), (_c))
93
94
sudo_dso_public char *sudo_json_get_buf_v1(struct json_container *jsonc);
95
#define sudo_json_get_buf(_a) sudo_json_get_buf_v1((_a))
96
97
sudo_dso_public unsigned int sudo_json_get_len_v1(struct json_container *jsonc);
98
#define sudo_json_get_len(_a) sudo_json_get_len_v1((_a))
99
100
#endif /* SUDO_JSON_H */
101
102