Index · Directives systemd 263~devel

Name

sd_json_build, sd_json_buildv, sd_json_buildo — Construct JSON variants of any type

Synopsis

#include <systemd/sd-json.h>
int sd_json_build(sd_json_variant **ret,
 …);
 
int sd_json_buildv(sd_json_variant **ret,
 va_list ap);
 
int sd_json_buildo(sd_json_variant **ret,
 …);
 

Description

sd_json_build() constructs a JSON variant from a description of its structure, passed as variadic arguments, and returns it in ret. The description must consist of exactly one top-level value, which may be of any JSON type: an object, an array, a string, a number, a boolean or null. It is generated with the SD_JSON_BUILD_…() macros, which may be nested arbitrarily to build up complex objects and arrays in a single call. See SD_JSON_BUILD_OBJECT(3) for a description of all of them.

sd_json_buildv() is similar, but takes the description as va_list.

sd_json_buildo() is a macro that wraps its arguments in SD_JSON_BUILD_OBJECT(), and is a shortcut for the common case of building an object at the top level. Unlike sd_json_build() it hence can only be used to build JSON objects, and its arguments must be a list of object fields, i.e. of SD_JSON_BUILD_PAIR…() macros. This means

sd_json_buildo(&v, SD_JSON_BUILD_PAIR_STRING("foo", "bar"));

is equivalent to

sd_json_build(&v, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRING("foo", "bar")));

Any objects generated, at any nesting level, are subject to the same rules as objects created with sd_json_variant_new_object(3), in particular each key may only be used once within the same object. If the description contains the same key more than once in the same object, the call fails with -ENOTUNIQ. Fields suppressed with SD_JSON_BUILD_PAIR_CONDITION() do not count. Note that this check happens at runtime, hence code that conditionally generates fields needs to make sure that the conditions of fields of the same name are mutually exclusive.

Return Value

On success, these functions return 0. On failure, they return a negative errno-style error code.

Errors

Returned errors may indicate the following problems:

-EINVAL

An argument is invalid, or the description is not well-formed (for example, a value is specified where an object field is expected).

-ENOTUNIQ

An object would contain the same key more than once.

Added in version 263.

-ENOMEM

Memory allocation failed.

Examples

Example 1. Build a JSON object with sd_json_buildo()

_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;

r = sd_json_buildo(
                &v,
                SD_JSON_BUILD_PAIR_STRING("name", "foo"),
                SD_JSON_BUILD_PAIR_UNSIGNED("size", 4711),
                SD_JSON_BUILD_PAIR_CONDITION(!!description, "description", SD_JSON_BUILD_STRING(description)),
                SD_JSON_BUILD_PAIR_ARRAY("tags",
                                         SD_JSON_BUILD_STRING("a"),
                                         SD_JSON_BUILD_STRING("b")));
if (r < 0)
        return r;

Example 2. Build a JSON array with sd_json_build()

_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
int r;

r = sd_json_build(
                &v,
                SD_JSON_BUILD_ARRAY(
                                SD_JSON_BUILD_STRING("foo"),
                                SD_JSON_BUILD_UNSIGNED(4711),
                                SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_BOOLEAN("enabled", true)),
                                SD_JSON_BUILD_NULL));
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

sd_json_build(), sd_json_buildv() and sd_json_buildo() were added in version 257.

Before version 263 objects with duplicate keys were accepted by these functions.

See Also

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