The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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/sbuff.h>
#include <freeradius-devel/util/types.h>
#include <freeradius-devel/util/value.h>
#include "base.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. More...
 
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. More...
 
bool fr_json_format_verify (fr_json_format_t const *format, bool verbose)
 Verify that the options in fr_json_format_t are valid. More...
 
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. More...
 
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) More...
 
void fr_json_version_print (void)
 Print JSON-C version. More...
 
static int json_afrom_value_box (TALLOC_CTX *ctx, json_object **out, fr_pair_t *vp, fr_json_format_t const *format)
 Convert fr_pair_t into a JSON object. More...
 
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. More...
 
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. More...
 
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. More...
 
json_object * json_object_from_value_box (fr_value_box_t const *data)
 Convert boxed value_box to a JSON object. More...
 
static void json_object_put_assert (json_object *obj)
 
static json_object * json_smplobj_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. More...
 
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. More...
 

Variables

static fr_json_format_t const default_json_format
 
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
e90ea072c88da82d354740724ec29a9a8b1c7a5b
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); \
return NULL; \
} 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:433

Definition at line 605 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 532 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 1239 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 559 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 96 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 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 279 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 459 of file json.c.

+ 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_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 pair 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
  • 1 if 'out' is the integer enum value, 0 otherwise
  • -1 on error.

Definition at line 486 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 950 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 1157 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 644 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 193 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 72 of file json.c.

+ Here is the caller graph for this function:

◆ json_smplobj_afrom_pair_list()

static json_object* json_smplobj_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 811 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 1095 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:59

Definition at line 45 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:626
#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:268
#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:297
Test enumeration values.
Definition: dict_test.h:92
JSON document formatting options.
Definition: base.h:219
static conf_parser_t const json_format_value_config[]
Definition: json.c:56
static conf_parser_t const json_format_attr_config[]
Definition: json.c:51

Definition at line 63 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 },
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207
@ JSON_MODE_ARRAY
Definition: base.h:61
@ 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

Definition at line 36 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 43 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:88

Definition at line 51 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" },
}
Value formatting options for fr_json_afrom_pair_list()
Definition: base.h:147

Definition at line 56 of file json.c.