The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or (at
6 * your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 3c83655354ec790888dffe385d4bf1299887be8a $
20 * @file lib/json/base.h
21 * @brief Implements the evaluation and parsing functions for the FreeRADIUS version of jpath.
22 *
23 * @author Arran Cudbard-Bell
24 * @author Matthew Newton
25 *
26 * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
27 * @copyright 2015,2020 Network RADIUS SAS (legal@networkradius.com)
28 * @copyright 2015 The FreeRADIUS Server Project
29 */
30RCSIDH(json_h, "$Id: 3c83655354ec790888dffe385d4bf1299887be8a $")
31
32#include "config.h"
33
34#ifdef HAVE_JSON
35# if defined(HAVE_JSONMC_JSON_H)
36
38DIAG_OFF(documentation-deprecated-sync)
39# include <json-c/json.h>
40DIAG_ON(documentation-deprecated-sync)
42
43# elif defined(HAVE_JSON_JSON_H)
44# include <json/json.h>
45# else
46# error "Need json-c headers"
47# endif
48# include <freeradius-devel/server/base.h>
49
51extern size_t fr_json_format_table_len;
52
53/** List of possible JSON format output modes.
54 *
55 * @see fr_json_format_s
56 */
65
66
67/** Attribute formatting options for fr_json_afrom_pair_list()
68 *
69 * Controls how attributes are formatted in JSON documents
70 * produced from fr_json_afrom_pair_list().
71 *
72 * **prefix** adds a string prefix to all attribute names in the
73 * JSON document, with a colon delimiter.
74 *
75 * Example, when prefix is NULL:
76@verbatim
77{"User-Name":{"type":"string","value":["john"]}}
78@endverbatim
79 *
80 * Example, when prefix is set to `foo`:
81@verbatim
82{"foo:User-Name":{"type":"string","value":["john"]}}
83@endverbatim
84 *
85 * @see struct fr_json_format_s
86 *
87 */
88typedef struct {
89 char const *prefix; //!< Prefix to add to all attribute names
91
92
93/** Value formatting options for fr_json_afrom_pair_list()
94 *
95 * Controls how values are formatted in JSON documents
96 * produced from fr_json_afrom_pair_list().
97 *
98 * Not all these options are valid for all output modes.
99 * @see fr_json_format_verify(), fr_json_format_s
100 *
101 *
102 * If an attribute appears only once then the value will normally
103 * be written as an object. When an attribute appears more than
104 * once then the values will be added as an array instead. Setting
105 * **value_is_always_array** will ensure that values are always written as
106 * an array, even if containing only a single entry.
107 *
108 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is false:
109@verbatim
110{"User-Name":"john","Filter-Id":["f1","f2"]}
111@endverbatim
112 *
113 * Example with output_mode `JSON_MODE_OBJECT_SIMPLE` and `value_is_always_array` is true:
114@verbatim
115{"User-Name":["john"],"Filter-Id":["f1","f2"]}
116@endverbatim
117 *
118 *
119 * Set **enum_as_int** to write enumerated values in their integer form.
120 *
121 * When false, the string form is output:
122@verbatim
123{"Service-Type":{"type":"uint32","value":"Login-User"}}
124@endverbatim
125 *
126 * When true, the integer is output:
127@verbatim
128{"Service-Type":{"type":"uint32","value":1}}
129@endverbatim
130 *
131 *
132 * Numeric data types will usually be written to the JSON document
133 * as numbers. **always_string** ensures that all values are written as
134 * strings:
135 *
136 * Example when `always_string` is false:
137@verbatim
138{"NAS-Port":{"type":"uint32","value":999}}
139@endverbatim
140 *
141 * Example when `always_string` is true:
142@verbatim
143{"NAS-Port":{"type":"uint32","value":"999"}}
144@endverbatim
145 *
146 */
147typedef struct {
148 bool value_is_always_array; //!< Use JSON array for multiple attribute values.
149 bool enum_as_int; //!< Output enums as value, not their string representation.
150 bool always_string; //!< Output all data types as strings.
152
153
154/** JSON document formatting options
155 *
156 * These options control the format of JSON document which is
157 * produced by fr_json_afrom_pair_list().
158 *
159 * The **output_mode** determines the format of JSON that is created:
160 *
161 * When JSON_MODE_OBJECT:
162@verbatim
163{
164 "<attribute0>": {
165 "type":"<str-type0>",
166 "value":["value0"]
167 },
168 "<attribute1>": {
169 "type":"<str-type1>",
170 "value":["value1.0", "value1.1"]
171 },
172 "<attribute2>": {
173 "type":"<int-type2>",
174 "value":[2]
175 },
176}
177@endverbatim
178 *
179 * When JSON_MODE_OBJECT_SIMPLE:
180@verbatim
181{
182 "<attribute0>":"<value0>",
183 "<attribute1>":["<value1.0>","<value1.1>"],
184 "<attribute2>":2
185}
186@endverbatim
187 *
188 * When JSON_MODE_ARRAY:
189@verbatim
190[
191 {"name":"<attribute0>","type":"<str-type0>","value":"<value0>"},
192 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.0>"},
193 {"name":"<attribute1>","type":"<str-type1>","value":"<value1.1>"},
194 {"name":"<attribute2>","type":"<int-type2>","value":2}
195]
196@endverbatim
197 *
198 * When JSON_MODE_ARRAY_OF_VALUES:
199@verbatim
200[
201 <value0>,
202 <value1.0>,
203 <value1.1>,
204 <value2>
205]
206@endverbatim
207 *
208 * When JSON_MODE_ARRAY_OF_NAMES:
209@verbatim
210[
211 <attribute0>,
212 <attribute1>,
213 <attribute1>,
214 <attribute2>
215]
216@endverbatim
217 *
218 */
220 char const *output_mode_str; //!< For conf_parser_t only.
221
222 json_mode_type_t output_mode; //!< Determine the format of JSON document
223 //!< to generate.
224
225 fr_json_format_attr_t attr; //!< Formatting options for attribute names.
226 fr_json_format_value_t value; //!< Formatting options for attribute values.
227
228 bool include_type; //!< Include attribute type where possible.
229};
230
232
234
235
236/* jpath .c */
238
239ssize_t fr_jpath_escape_func(UNUSED request_t *request, char *out, size_t outlen,
240 char const *in, UNUSED void *arg);
241
242int fr_jpath_evaluate_leaf(TALLOC_CTX *ctx, fr_value_box_list_t *out,
243 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
244 json_object *root, fr_jpath_node_t const *jpath);
245
246char *fr_jpath_asprint(TALLOC_CTX *ctx, fr_jpath_node_t const *head);
247
248ssize_t fr_jpath_parse(TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen);
249
250/* json.c */
251int fr_json_object_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object,
252 fr_dict_attr_t const *enumv, bool tainted);
253
255
257
258void fr_json_version_print(void);
259
260char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t *vps,
261 fr_json_format_t const *format);
262
263bool fr_json_format_verify(fr_json_format_t const *format, bool verbose);
264#endif
#define DIAG_UNKNOWN_PRAGMAS
Definition build.h:456
#define DIAG_ON(_x)
Definition build.h:458
#define RCSIDH(h, id)
Definition build.h:484
#define UNUSED
Definition build.h:315
#define DIAG_OFF(_x)
Definition build.h:457
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:579
static fr_slen_t in
Definition dict.h:824
Node in a jpath selector sequence.
Definition jpath.c:87
bool value_is_always_array
Use JSON array for multiple attribute values.
Definition base.h:148
json_mode_type_t output_mode
Determine the format of JSON document to generate.
Definition base.h:222
char * fr_json_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
Returns a JSON string of a list of value pairs.
Definition json.c:1239
size_t fr_json_format_table_len
Definition json.c:43
json_mode_type_t
List of possible JSON format output modes.
Definition base.h:57
@ JSON_MODE_ARRAY
Definition base.h:61
@ JSON_MODE_UNSET
Definition base.h:58
@ JSON_MODE_OBJECT
Definition base.h:59
@ JSON_MODE_ARRAY_OF_NAMES
Definition base.h:63
@ JSON_MODE_OBJECT_SIMPLE
Definition base.h:60
@ JSON_MODE_ARRAY_OF_VALUES
Definition base.h:62
bool include_type
Include attribute type where possible.
Definition base.h:228
fr_json_format_attr_t attr
Formatting options for attribute names.
Definition base.h:225
char * fr_jpath_asprint(TALLOC_CTX *ctx, fr_jpath_node_t const *head)
Print a node list to a string for debugging.
Definition jpath.c:411
json_object * json_object_from_value_box(fr_value_box_t const *data)
Convert boxed value_box to a JSON object.
Definition json.c:193
fr_json_format_value_t value
Formatting options for attribute values.
Definition base.h:226
fr_slen_t fr_json_str_from_value(fr_sbuff_t *out, fr_value_box_t *vb, bool include_quotes)
Print a value box as its equivalent JSON format without going via a struct json_object (in most cases...
Definition json.c:279
char const * output_mode_str
For conf_parser_t only.
Definition base.h:220
conf_parser_t const fr_json_format_config[]
Definition json.c:63
int fr_jpath_evaluate_leaf(TALLOC_CTX *ctx, fr_value_box_list_t *out, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, json_object *root, fr_jpath_node_t const *jpath)
Evaluate a parsed jpath expression against a json-c tree.
Definition jpath.c:383
ssize_t fr_jpath_escape_func(UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
Escapes special chars.
Definition jpath.c:106
char const * prefix
Prefix to add to all attribute names.
Definition base.h:89
void fr_json_version_print(void)
Print JSON-C version.
Definition json.c:459
fr_table_num_sorted_t const fr_json_format_table[]
Definition json.c:36
int fr_json_object_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, json_object *object, fr_dict_attr_t const *enumv, bool tainted)
Convert json object to fr_value_box_t.
Definition json.c:96
bool enum_as_int
Output enums as value, not their string representation.
Definition base.h:149
bool always_string
Output all data types as strings.
Definition base.h:150
bool fr_json_format_verify(fr_json_format_t const *format, bool verbose)
Verify that the options in fr_json_format_t are valid.
Definition json.c:559
ssize_t fr_jpath_parse(TALLOC_CTX *ctx, fr_jpath_node_t **head, char const *in, size_t inlen)
Parse a jpath string.
Definition jpath.c:813
Attribute formatting options for fr_json_afrom_pair_list()
Definition base.h:88
JSON document formatting options.
Definition base.h:219
Value formatting options for fr_json_afrom_pair_list()
Definition base.h:147
fr_type_t
long int ssize_t
ssize_t fr_slen_t
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
static fr_slen_t head
Definition xlat.h:422
static fr_slen_t data
Definition value.h:1265
static size_t char fr_sbuff_t size_t inlen
Definition value.h:997
static size_t char ** out
Definition value.h:997