The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
Macros | Functions | Variables
json.c File Reference

Common functions for working with json-c. More...

#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/base16.h>
#include <freeradius-devel/util/base64.h>
#include <freeradius-devel/util/types.h>
#include <freeradius-devel/util/value.h>
#include "base.h"
#include "lib/util/strerror.h"
+ Include dependency graph for json.c:

Go to the source code of this file.

Macros

#define INVALID_TYPE
 

Functions

static ssize_t attr_name_with_prefix (fr_sbuff_t *out, fr_dict_attr_t const *da, fr_json_format_t const *format)
 Get attribute name with optional prefix.
 
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.
 
bool fr_json_format_verify (fr_json_format_t const *format, bool verbose)
 Verify that the options in fr_json_format_t are valid.
 
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.
 
fr_slen_t fr_json_str_from_value (fr_sbuff_t *out, fr_value_box_t const *vb, bool include_quotes)
 Print a value box as its equivalent JSON format without going via a struct json_object (in most cases)
 
void fr_json_version_print (void)
 Print JSON-C version.
 
static int json_afrom_pair (TALLOC_CTX *ctx, json_object **out, fr_pair_t *vp, fr_json_format_t const *format)
 Convert fr_pair_t into a JSON object.
 
static int json_afrom_value_box (TALLOC_CTX *ctx, json_object **out, fr_value_box_t const *vb, fr_json_format_t const *format)
 Convert a value box into a JSON object.
 
static struct json_object * json_array_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON array representation of a list of value pairs.
 
static struct json_object * json_attr_array_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON array of a list of value pairs.
 
static json_object * json_object_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON object representation of a list of value pairs.
 
json_object * json_object_from_value_box (fr_value_box_t const *data)
 Convert boxed value_box to a JSON object.
 
static void json_object_put_assert (json_object *obj)
 
static json_object * json_simple_obj_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON object representation of a list of value pairs.
 
static struct json_object * json_value_array_afrom_pair_list (TALLOC_CTX *ctx, fr_pair_list_t *vps, fr_json_format_t const *format)
 Returns a JSON array of a list of value pairs.
 

Variables

static fr_json_format_t const default_json_format
 
fr_table_num_sorted_t const fr_json_binary_format_table []
 
size_t fr_json_binary_format_table_len = NUM_ELEMENTS(fr_json_binary_format_table)
 
conf_parser_t const fr_json_format_config []
 
fr_table_num_sorted_t const fr_json_format_table []
 
size_t fr_json_format_table_len = NUM_ELEMENTS(fr_json_format_table)
 
static conf_parser_t const json_format_attr_config []
 
static conf_parser_t const json_format_value_config []
 

Detailed Description

Common functions for working with json-c.

Id
6dce13eeb977662a2fae15d213ce0e4c02b5af84
Author
Arran Cudbard-Bell
Matthew Newton

Definition in file json.c.

Macro Definition Documentation

◆ INVALID_TYPE

#define INVALID_TYPE
Value:
do { \
fr_assert(0); \
fr_strerror_printf("Invalid type %s for attribute %s", fr_type_to_str(vp->vp_type), vp->da->name); \
goto error; \
} while (0)
fr_pair_t * vp
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454

Definition at line 678 of file json.c.

Function Documentation

◆ attr_name_with_prefix()

static ssize_t attr_name_with_prefix ( fr_sbuff_t out,
fr_dict_attr_t const *  da,
fr_json_format_t const *  format 
)
inlinestatic

Get attribute name with optional prefix.

If the format "attr.prefix" string is set then prepend this to the given attribute name, otherwise just return name alone.

Parameters
[out]outsbuff to write the new name
[in]dadictionary attribute to get name of
[in]formatjson format structure
Returns
length of attribute name

Definition at line 605 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_afrom_pair_list()

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.

The result is a talloc-ed string, freeing the string is the responsibility of the caller.

The 'format' struct contains settings to configure the output JSON document format.

See also
fr_json_format_s

Default output, when format is NULL, is:

{
        "<attribute0>":{
                "type":"<type0>",
                "value":[<value0>,<value1>,<valueN>]
        },
        "<attribute1>":{
                "type":"<type1>",
                "value":[...]
        },
        "<attributeN>":{
                "type":"<typeN>",
                "value":[...]
        }
}
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, can be NULL to use default format.
Returns
JSON string representation of the value pairs

Definition at line 1321 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_format_verify()

bool fr_json_format_verify ( fr_json_format_t const *  format,
bool  verbose 
)

Verify that the options in fr_json_format_t are valid.

Warnings are optional, will fatal error if the format is corrupt.

Parameters
[in]formatthe format structure to check
[in]verboseprint out warnings if set
Returns
true if format is good, otherwise false

Definition at line 632 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_object_to_value_box()

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.

Parameters
[in]ctxto allocate any value buffers in (should usually be the same as out).
[in]outWhere to write value. Must be initialised.
[in]objectto convert.
[in]enumvAny string values are assumed to be in PRESENTATION format, meaning that if an enumv is specified, they'll be checked against the list of aliases for that enumeration, and possibly converted into one of the enumeration values (which may not be a string).
[in]taintedWhether the data source is untrusted.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 107 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_str_from_value()

fr_slen_t fr_json_str_from_value ( fr_sbuff_t out,
fr_value_box_t const *  vb,
bool  include_quotes 
)

Print a value box as its equivalent JSON format without going via a struct json_object (in most cases)

Parameters
[out]outbuffer to write to.
[in]vbto print.
[in]include_quoteswhether we should wrap string values, or non-native types like IPv4 addresses in quotes.
Returns
  • <0 on error.
  • >= number of bytes written.

Definition at line 291 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_json_version_print()

void fr_json_version_print ( void  )

Print JSON-C version.

Definition at line 475 of file json.c.

+ Here is the caller graph for this function:

◆ json_afrom_pair()

static int json_afrom_pair ( TALLOC_CTX *  ctx,
json_object **  out,
fr_pair_t vp,
fr_json_format_t const *  format 
)
static

Convert fr_pair_t into a JSON object.

If format.value.enum_as_int is set, and the given VP is an enum value, the integer value is returned as a json_object rather than the text representation.

If format.value.always_string is set then a numeric value will be returned as a JSON string object.

Parameters
[in]ctxTalloc context.
[out]outreturned json object.
[in]vpto get the value of.
[in]formatformat definition, or NULL.
Returns
  • 0 on success.
  • -1 on error.

Definition at line 578 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_afrom_value_box()

static int json_afrom_value_box ( TALLOC_CTX *  ctx,
json_object **  out,
fr_value_box_t const *  vb,
fr_json_format_t const *  format 
)
inlinestatic

Convert a value box into a JSON object.

If format.value.always_string is set then a numeric value will be returned as a JSON string object.

Parameters
[in]ctxTalloc context.
[out]outreturned json object.
[in]vbto get the value of.
[in]formatformat definition, or NULL.
Returns
  • 0 on success.
  • -1 on error.

Definition at line 499 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_array_afrom_pair_list()

static struct json_object * json_array_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)
static

Returns a JSON array representation of a list of value pairs.

The result is a struct json_object, which should be free'd with json_object_put() by the caller. Intended to only be called by fr_json_afrom_pair_list().

This function generates the "array" format, JSON_MODE_ARRAY.

See also
fr_json_format_s
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, must be set.
Returns
JSON object with the generated representation.

Definition at line 1024 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_attr_array_afrom_pair_list()

static struct json_object * json_attr_array_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)
static

Returns a JSON array of a list of value pairs.

The result is a struct json_object, which should be free'd with json_object_put() by the caller. Intended to only be called by fr_json_afrom_pair_list().

This function generates the "array_of_names" format, JSON_MODE_ARRAY_OF_NAMES, listing just the attribute names.

See also
fr_json_format_s
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, must be set.
Returns
JSON object with the generated representation.

Definition at line 1236 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_object_afrom_pair_list()

static json_object * json_object_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)
static

Returns a JSON object representation of a list of value pairs.

The result is a struct json_object, which should be free'd with json_object_put() by the caller. Intended to only be called by fr_json_afrom_pair_list().

This function generates the "object" format, JSON_MODE_OBJECT.

See also
fr_json_format_s
{
        "<attribute0>":{
                "type":"<type0>",
                "value":[<value0>,<value1>,<valueN>]            // if value_is_always_array is true
        },                                                      // or
        "<attribute1>":{
                "type":"<type1>",
                "value":<value0>                                // if value_is_always_array is false
                                                                // and there is only one value
        },
        "<attributeN>":{
                "type":"<typeN>",
                "value":[...]
        }
}
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, must be set.
Returns
JSON object with the generated representation.

Definition at line 718 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_object_from_value_box()

json_object * json_object_from_value_box ( fr_value_box_t const *  data)

Convert boxed value_box to a JSON object.

Parameters
[in]datato convert.

Definition at line 204 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_object_put_assert()

static void json_object_put_assert ( json_object *  obj)
inlinestatic

Definition at line 83 of file json.c.

+ Here is the caller graph for this function:

◆ json_simple_obj_afrom_pair_list()

static json_object * json_simple_obj_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)
static

Returns a JSON object representation of a list of value pairs.

The result is a struct json_object, which should be free'd with json_object_put() by the caller. Intended to only be called by fr_json_afrom_pair_list().

This function generates the "simple object" format, JSON_MODE_OBJECT_SIMPLE.

See also
fr_json_format_s
{
        "<attribute0>":[<value0>,<value1>,<valueN>]     // if value_is_always_array is true
                                                        // or
        "<attribute1>":<value0>                         // if value_is_always_array is false,
                                                        // and there is only one value
        "<attributeN>":[<value0>,<value1>,<valueN>]
}
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, must be set.
Returns
JSON object with the generated representation.

Definition at line 884 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ json_value_array_afrom_pair_list()

static struct json_object * json_value_array_afrom_pair_list ( TALLOC_CTX *  ctx,
fr_pair_list_t vps,
fr_json_format_t const *  format 
)
static

Returns a JSON array of a list of value pairs.

The result is a struct json_object, which should be free'd with json_object_put() by the caller. Intended to only be called by fr_json_afrom_pair_list().

This function generates the "array_of_values" format, JSON_MODE_ARRAY_OF_VALUES, listing just the attribute values.

See also
fr_json_format_s
Parameters
[in]ctxTalloc context.
[in]vpsa list of value pairs.
[in]formatFormatting control, must be set.
Returns
JSON object with the generated representation.

Definition at line 1172 of file json.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ default_json_format

fr_json_format_t const default_json_format
static
Initial value:
= {
.attr = { .prefix = NULL },
.value = { .value_is_always_array = true },
.output_mode = JSON_MODE_OBJECT
}
@ JSON_MODE_OBJECT
Definition base.h:70

Definition at line 53 of file json.c.

◆ fr_json_binary_format_table

fr_table_num_sorted_t const fr_json_binary_format_table[]
Initial value:
= {
{ L("base16"), JSON_BINARY_FORMAT_BASE16 },
{ L("base64"), JSON_BINARY_FORMAT_BASE64 },
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:210
@ JSON_BINARY_FORMAT_BASE64
Base64-encode octets values.
Definition base.h:58
@ JSON_BINARY_FORMAT_RAW
Current behaviour - raw bytes as JSON string.
Definition base.h:56
@ JSON_BINARY_FORMAT_BASE16
Base16-encode octets values.
Definition base.h:57

Definition at line 46 of file json.c.

◆ fr_json_binary_format_table_len

size_t fr_json_binary_format_table_len = NUM_ELEMENTS(fr_json_binary_format_table)

Definition at line 51 of file json.c.

◆ fr_json_format_config

conf_parser_t const fr_json_format_config[]
Initial value:
= {
{ FR_CONF_OFFSET("output_mode", fr_json_format_t, output_mode_str), .dflt = "object" },
}
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:657
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:309
Test enumeration values.
Definition dict_test.h:92
JSON document formatting options.
Definition base.h:231
static conf_parser_t const json_format_value_config[]
Definition json.c:64
static conf_parser_t const json_format_attr_config[]
Definition json.c:59

Definition at line 74 of file json.c.

◆ fr_json_format_table

fr_table_num_sorted_t const fr_json_format_table[]
Initial value:
= {
{ L("array"), JSON_MODE_ARRAY },
{ L("array_of_names"), JSON_MODE_ARRAY_OF_NAMES },
{ L("array_of_values"), JSON_MODE_ARRAY_OF_VALUES },
{ L("object"), JSON_MODE_OBJECT },
{ L("object_simple"), JSON_MODE_OBJECT_SIMPLE },
}
@ JSON_MODE_ARRAY
Definition base.h:72
@ JSON_MODE_ARRAY_OF_NAMES
Definition base.h:74
@ JSON_MODE_OBJECT_SIMPLE
Definition base.h:71
@ JSON_MODE_ARRAY_OF_VALUES
Definition base.h:73

Definition at line 37 of file json.c.

◆ fr_json_format_table_len

size_t fr_json_format_table_len = NUM_ELEMENTS(fr_json_format_table)

Definition at line 44 of file json.c.

◆ json_format_attr_config

conf_parser_t const json_format_attr_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("prefix", fr_json_format_attr_t, prefix) },
}
Attribute formatting options for fr_json_afrom_pair_list()
Definition base.h:99

Definition at line 59 of file json.c.

◆ json_format_value_config

conf_parser_t const json_format_value_config[]
static
Initial value:
= {
{ FR_CONF_OFFSET("single_value_as_array", fr_json_format_value_t, value_is_always_array), .dflt = "no" },
{ FR_CONF_OFFSET("enum_as_integer", fr_json_format_value_t, enum_as_int), .dflt = "no" },
{ FR_CONF_OFFSET("always_string", fr_json_format_value_t, always_string), .dflt = "no" },
{ FR_CONF_OFFSET("binary_format", fr_json_format_value_t, binary_format), .dflt = "raw",
}
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition cf_parse.c:1635
Value formatting options for fr_json_afrom_pair_list()
Definition base.h:158
fr_table_num_sorted_t const fr_json_binary_format_table[]
Definition json.c:46
size_t fr_json_binary_format_table_len
Definition json.c:51

Definition at line 64 of file json.c.