Index · Directives systemd 263~devel

Name

SD_JSON_BUILD_OBJECT, SD_JSON_BUILD_EMPTY_OBJECT, SD_JSON_BUILD_ARRAY, SD_JSON_BUILD_EMPTY_ARRAY, SD_JSON_BUILD_PAIR, SD_JSON_BUILD_PAIR_CONDITION, SD_JSON_BUILD_STRING, SD_JSON_BUILD_INTEGER, SD_JSON_BUILD_UNSIGNED, SD_JSON_BUILD_REAL, SD_JSON_BUILD_BOOLEAN, SD_JSON_BUILD_NULL, SD_JSON_BUILD_VARIANT, SD_JSON_BUILD_VARIANT_ARRAY, SD_JSON_BUILD_LITERAL, SD_JSON_BUILD_STRV, SD_JSON_BUILD_BASE64, SD_JSON_BUILD_BASE32HEX, SD_JSON_BUILD_HEX, SD_JSON_BUILD_OCTESCAPE, SD_JSON_BUILD_BYTE_ARRAY, SD_JSON_BUILD_ID128, SD_JSON_BUILD_UUID, SD_JSON_BUILD_CALLBACK, SD_JSON_BUILD_PAIR_STRING, SD_JSON_BUILD_PAIR_INTEGER, SD_JSON_BUILD_PAIR_UNSIGNED, SD_JSON_BUILD_PAIR_REAL, SD_JSON_BUILD_PAIR_BOOLEAN, SD_JSON_BUILD_PAIR_ARRAY, SD_JSON_BUILD_PAIR_EMPTY_ARRAY, SD_JSON_BUILD_PAIR_OBJECT, SD_JSON_BUILD_PAIR_EMPTY_OBJECT, SD_JSON_BUILD_PAIR_NULL, SD_JSON_BUILD_PAIR_VARIANT, SD_JSON_BUILD_PAIR_VARIANT_ARRAY, SD_JSON_BUILD_PAIR_LITERAL, SD_JSON_BUILD_PAIR_STRV, SD_JSON_BUILD_PAIR_BASE64, SD_JSON_BUILD_PAIR_BASE32HEX, SD_JSON_BUILD_PAIR_HEX, SD_JSON_BUILD_PAIR_OCTESCAPE, SD_JSON_BUILD_PAIR_BYTE_ARRAY, SD_JSON_BUILD_PAIR_ID128, SD_JSON_BUILD_PAIR_UUID, SD_JSON_BUILD_PAIR_CALLBACK, sd_json_build_callback_t — Macros describing JSON variants to construct with sd_json_build()

Synopsis

#include <systemd/sd-json.h>
SD_JSON_BUILD_OBJECT(pair…)
SD_JSON_BUILD_EMPTY_OBJECT
SD_JSON_BUILD_ARRAY(value…)
SD_JSON_BUILD_EMPTY_ARRAY
SD_JSON_BUILD_PAIR(const char *name, value)
SD_JSON_BUILD_PAIR_CONDITION(int condition, const char *name, value)
SD_JSON_BUILD_STRING(const char *s)
SD_JSON_BUILD_INTEGER(int64_t i)
SD_JSON_BUILD_UNSIGNED(uint64_t u)
SD_JSON_BUILD_REAL(double d)
SD_JSON_BUILD_BOOLEAN(int b)
SD_JSON_BUILD_NULL
SD_JSON_BUILD_VARIANT(sd_json_variant *v)
SD_JSON_BUILD_VARIANT_ARRAY(sd_json_variant **v, size_t n)
SD_JSON_BUILD_LITERAL(const char *l)
SD_JSON_BUILD_STRV(char **l)
SD_JSON_BUILD_BASE64(const void *p, size_t n)
SD_JSON_BUILD_BASE32HEX(const void *p, size_t n)
SD_JSON_BUILD_HEX(const void *p, size_t n)
SD_JSON_BUILD_OCTESCAPE(const void *p, size_t n)
SD_JSON_BUILD_BYTE_ARRAY(const void *p, size_t n)
SD_JSON_BUILD_ID128(sd_id128_t id)
SD_JSON_BUILD_UUID(sd_id128_t id)
SD_JSON_BUILD_CALLBACK(sd_json_build_callback_t c, void *userdata)
SD_JSON_BUILD_PAIR_STRING(const char *name, const char *s)
SD_JSON_BUILD_PAIR_INTEGER(const char *name, int64_t i)
SD_JSON_BUILD_PAIR_UNSIGNED(const char *name, uint64_t u)
SD_JSON_BUILD_PAIR_REAL(const char *name, double d)
SD_JSON_BUILD_PAIR_BOOLEAN(const char *name, int b)
SD_JSON_BUILD_PAIR_ARRAY(const char *name, value…)
SD_JSON_BUILD_PAIR_EMPTY_ARRAY(const char *name)
SD_JSON_BUILD_PAIR_OBJECT(const char *name, pair…)
SD_JSON_BUILD_PAIR_EMPTY_OBJECT(const char *name)
SD_JSON_BUILD_PAIR_NULL(const char *name)
SD_JSON_BUILD_PAIR_VARIANT(const char *name, sd_json_variant *v)
SD_JSON_BUILD_PAIR_VARIANT_ARRAY(const char *name, sd_json_variant **v, size_t n)
SD_JSON_BUILD_PAIR_LITERAL(const char *name, const char *l)
SD_JSON_BUILD_PAIR_STRV(const char *name, char **l)
SD_JSON_BUILD_PAIR_BASE64(const char *name, const void *p, size_t n)
SD_JSON_BUILD_PAIR_BASE32HEX(const char *name, const void *p, size_t n)
SD_JSON_BUILD_PAIR_HEX(const char *name, const void *p, size_t n)
SD_JSON_BUILD_PAIR_OCTESCAPE(const char *name, const void *p, size_t n)
SD_JSON_BUILD_PAIR_BYTE_ARRAY(const char *name, const void *p, size_t n)
SD_JSON_BUILD_PAIR_ID128(const char *name, sd_id128_t id)
SD_JSON_BUILD_PAIR_UUID(const char *name, sd_id128_t id)
SD_JSON_BUILD_PAIR_CALLBACK(const char *name, sd_json_build_callback_t c, void *userdata)
typedef int (*sd_json_build_callback_t)(sd_json_variant **ret,
 const char *name,
 void *userdata);
 

Description

The SD_JSON_BUILD_…() macros are used to describe the structure and contents of a JSON variant to construct with sd_json_build(3) and related calls. Each macro expands to a type identifier followed by its arguments, cast to the precise types the builder expects. They may only be used in the variadic argument list of these calls, and there is no type checking beyond what the casts provide.

A description consists of exactly one value. Values are generated by the macros listed under "Values" below, or by SD_JSON_BUILD_OBJECT() or SD_JSON_BUILD_ARRAY(), which in turn take a list of fields or values, and may be nested arbitrarily.

Containers

SD_JSON_BUILD_OBJECT(pair…)

Generates a JSON object. Takes a list of zero or more key/value pairs, i.e. object fields, each described by SD_JSON_BUILD_PAIR(), SD_JSON_BUILD_PAIR_CONDITION() or one of the SD_JSON_BUILD_PAIR_…() shortcut macros. The fields are added to the object in the order specified. Each key may be used only once in the same object, see sd_json_variant_new_object(3).

Added in version 257.

SD_JSON_BUILD_EMPTY_OBJECT

Generates an empty JSON object, i.e. "{}".

Added in version 257.

SD_JSON_BUILD_ARRAY(value…)

Generates a JSON array. Takes a list of zero or more values, which are added to the array in the order specified.

Added in version 257.

SD_JSON_BUILD_EMPTY_ARRAY

Generates an empty JSON array, i.e. "[]".

Added in version 257.

Object Fields

SD_JSON_BUILD_PAIR(name, value)

Generates an object field. Takes the key name as a string, followed by the description of exactly one value. May only be used directly inside of SD_JSON_BUILD_OBJECT() (or sd_json_buildo()).

Added in version 257.

SD_JSON_BUILD_PAIR_CONDITION(condition, name, value)

Similar to SD_JSON_BUILD_PAIR(), but takes an additional boolean condition as first argument. If false, the field is omitted from the generated object. In this case the value is not generated either, with one exception: a JSON string specified with SD_JSON_BUILD_LITERAL() is parsed even if the field is suppressed, and parse errors are reported. Note that a field omitted this way does not count when checking for duplicate keys, which allows multiple fields of the same name with mutually exclusive conditions.

Added in version 257.

SD_JSON_BUILD_PAIR_STRING(), SD_JSON_BUILD_PAIR_INTEGER(), SD_JSON_BUILD_PAIR_UNSIGNED(), SD_JSON_BUILD_PAIR_REAL(), SD_JSON_BUILD_PAIR_BOOLEAN(), SD_JSON_BUILD_PAIR_ARRAY(), SD_JSON_BUILD_PAIR_EMPTY_ARRAY(), SD_JSON_BUILD_PAIR_OBJECT(), SD_JSON_BUILD_PAIR_EMPTY_OBJECT(), SD_JSON_BUILD_PAIR_NULL(), SD_JSON_BUILD_PAIR_VARIANT(), SD_JSON_BUILD_PAIR_VARIANT_ARRAY(), SD_JSON_BUILD_PAIR_LITERAL(), SD_JSON_BUILD_PAIR_STRV(), SD_JSON_BUILD_PAIR_BASE64(), SD_JSON_BUILD_PAIR_BASE32HEX(), SD_JSON_BUILD_PAIR_HEX(), SD_JSON_BUILD_PAIR_OCTESCAPE(), SD_JSON_BUILD_PAIR_BYTE_ARRAY(), SD_JSON_BUILD_PAIR_ID128(), SD_JSON_BUILD_PAIR_UUID(), SD_JSON_BUILD_PAIR_CALLBACK()

Shortcuts that combine SD_JSON_BUILD_PAIR() with the value macro of the same suffix: they take the key name as first argument, followed by the arguments of the respective value macro. For example, SD_JSON_BUILD_PAIR_STRING("foo", "bar") is equivalent to SD_JSON_BUILD_PAIR("foo", SD_JSON_BUILD_STRING("bar")), and SD_JSON_BUILD_PAIR_EMPTY_ARRAY("foo") is equivalent to SD_JSON_BUILD_PAIR("foo", SD_JSON_BUILD_EMPTY_ARRAY).

Added in version 257.

Values

SD_JSON_BUILD_STRING(s)

Generates a JSON string from a NUL-terminated C string, which must be valid UTF-8. If s is NULL, a JSON null value is generated instead.

Added in version 257.

SD_JSON_BUILD_INTEGER(i)

Generates a JSON number from a signed 64-bit integer.

Added in version 257.

SD_JSON_BUILD_UNSIGNED(u)

Generates a JSON number from an unsigned 64-bit integer.

Added in version 257.

SD_JSON_BUILD_REAL(d)

Generates a JSON number from a double.

Added in version 257.

SD_JSON_BUILD_BOOLEAN(b)

Generates a JSON boolean. Any non-zero value of b results in true, zero in false.

Added in version 257.

SD_JSON_BUILD_NULL

Generates a JSON null value.

Added in version 257.

SD_JSON_BUILD_VARIANT(v)

Inserts an existing sd_json_variant object, which may be of any type. A new reference to the object is taken, the caller's reference is not consumed. If v is NULL, a JSON null value is generated instead.

Added in version 257.

SD_JSON_BUILD_VARIANT_ARRAY(v, n)

Generates a JSON array from a C array of n sd_json_variant objects, see sd_json_variant_new_array().

Added in version 257.

SD_JSON_BUILD_LITERAL(l)

Parses the specified string as JSON (with the same rules as sd_json_parse(3)) and inserts the result. If l is NULL, a JSON null value is generated instead.

Added in version 257.

SD_JSON_BUILD_STRV(l)

Generates a JSON array of strings from a NULL-terminated C string array. If l is NULL or empty, an empty array is generated.

Added in version 257.

SD_JSON_BUILD_BASE64(p, n), SD_JSON_BUILD_BASE32HEX(p, n), SD_JSON_BUILD_HEX(p, n), SD_JSON_BUILD_OCTESCAPE(p, n)

Generate a JSON string from the binary data of n bytes at p, encoded in Base64, in Base32 with the "Extended Hex" alphabet (without padding), in lowercase hexadecimal, or with C-style octal escapes (e.g. "\033") for all bytes that are not printable ASCII characters as well as for backslashes and double quotes, respectively. p may be NULL if n is zero.

Added in version 257.

SD_JSON_BUILD_BYTE_ARRAY(p, n)

Generates a JSON array of numbers in the range 0…255 from the binary data of n bytes at p, one element per byte. p may be NULL if n is zero.

Added in version 257.

SD_JSON_BUILD_ID128(id), SD_JSON_BUILD_UUID(id)

Generate a JSON string from a 128-bit ID, formatted as 32 lowercase hexadecimal characters, or in the RFC 9562 UUID format (with dashes), respectively. Note that the address of id is taken, hence it must be an lvalue.

Added in version 257.

SD_JSON_BUILD_CALLBACK(c, userdata)

Invokes the callback c to generate the value. The callback is passed a pointer to the variant to return in its ret argument, and the userdata pointer specified in the macro. If the value is generated for an object field, the field's key name is passed in name. The callback should return a negative errno-style error code on failure, which is propagated to the caller of sd_json_build(), and a non-negative value on success. If the callback leaves ret unset (i.e. NULL), or if c is NULL, a JSON null value is generated. The callback is not invoked for fields suppressed with SD_JSON_BUILD_PAIR_CONDITION().

Added in version 257.

Examples

Example 1. Nested description

sd_id128_t id = …;
char **tags = …;
const uint8_t data[] = { 1, 2, 3 };
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;

r = sd_json_build(
                &v,
                SD_JSON_BUILD_OBJECT(
                                SD_JSON_BUILD_PAIR("name", SD_JSON_BUILD_STRING("foo")),
                                SD_JSON_BUILD_PAIR_ID128("id", id),
                                SD_JSON_BUILD_PAIR_STRV("tags", tags),
                                SD_JSON_BUILD_PAIR_BASE64("data", data, sizeof(data)),
                                SD_JSON_BUILD_PAIR_CONDITION(tags && tags[0], "firstTag", SD_JSON_BUILD_STRING(tags[0])),
                                SD_JSON_BUILD_PAIR_ARRAY("values",
                                                         SD_JSON_BUILD_UNSIGNED(4711),
                                                         SD_JSON_BUILD_REAL(0.5),
                                                         SD_JSON_BUILD_NULL),
                                SD_JSON_BUILD_PAIR_LITERAL("extra", "{\"a\":[1,2,3]}")));
if (r < 0)
        return r;

Notes

Functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file.

The code described here uses getenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call setenv(3) from a parallel thread. It is recommended to only do calls to setenv() from an early phase of the program when no other threads have been started.

History

All macros described here and sd_json_build_callback_t were added in version 257.

See Also

systemd(1), sd-json(3), sd_json_build(3), sd_json_variant_new_object(3), sd_json_parse(3)