| 1 |
<?xml version='1.0'?> <!--*-nxml-*--> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="sd_json_parse" xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 7 |
|
| 8 |
<refentryinfo> |
| 9 |
<title>sd_json_parse</title> |
| 10 |
<productname>systemd</productname> |
| 11 |
</refentryinfo> |
| 12 |
|
| 13 |
<refmeta> |
| 14 |
<refentrytitle>sd_json_parse</refentrytitle> |
| 15 |
<manvolnum>3</manvolnum> |
| 16 |
</refmeta> |
| 17 |
|
| 18 |
<refnamediv> |
| 19 |
<refname>sd_json_parse</refname> |
| 20 |
<refname>sd_json_parse_continue</refname> |
| 21 |
<refname>sd_json_parse_with_source</refname> |
| 22 |
<refname>sd_json_parse_with_source_continue</refname> |
| 23 |
<refname>sd_json_parse_file</refname> |
| 24 |
<refname>sd_json_parse_file_at</refname> |
| 25 |
<refname>sd_json_parse_fd</refname> |
| 26 |
<refname>SD_JSON_PARSE_SENSITIVE</refname> |
| 27 |
<refname>SD_JSON_PARSE_MUST_BE_OBJECT</refname> |
| 28 |
<refname>SD_JSON_PARSE_MUST_BE_ARRAY</refname> |
| 29 |
<refname>SD_JSON_PARSE_SEEK0</refname> |
| 30 |
<refname>SD_JSON_PARSE_DONATE_FD</refname> |
| 31 |
<refname>SD_JSON_PARSE_REOPEN_FD</refname> |
| 32 |
|
| 33 |
<refpurpose>Parse JSON strings and files into JSON variant objects</refpurpose> |
| 34 |
</refnamediv> |
| 35 |
|
| 36 |
<refsynopsisdiv> |
| 37 |
<funcsynopsis> |
| 38 |
<funcsynopsisinfo>#include <systemd/sd-json.h></funcsynopsisinfo> |
| 39 |
|
| 40 |
<funcprototype> |
| 41 |
<funcdef>int <function>sd_json_parse</function></funcdef> |
| 42 |
<paramdef>const char *<parameter>string</parameter></paramdef> |
| 43 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 44 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 45 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 46 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 47 |
</funcprototype> |
| 48 |
|
| 49 |
<funcprototype> |
| 50 |
<funcdef>int <function>sd_json_parse_continue</function></funcdef> |
| 51 |
<paramdef>const char **<parameter>p</parameter></paramdef> |
| 52 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 53 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 54 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 55 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 56 |
</funcprototype> |
| 57 |
|
| 58 |
<funcprototype> |
| 59 |
<funcdef>int <function>sd_json_parse_with_source</function></funcdef> |
| 60 |
<paramdef>const char *<parameter>string</parameter></paramdef> |
| 61 |
<paramdef>const char *<parameter>source</parameter></paramdef> |
| 62 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 63 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 64 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 65 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 66 |
</funcprototype> |
| 67 |
|
| 68 |
<funcprototype> |
| 69 |
<funcdef>int <function>sd_json_parse_with_source_continue</function></funcdef> |
| 70 |
<paramdef>const char **<parameter>p</parameter></paramdef> |
| 71 |
<paramdef>const char *<parameter>source</parameter></paramdef> |
| 72 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 73 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 74 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 75 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 76 |
</funcprototype> |
| 77 |
|
| 78 |
<funcprototype> |
| 79 |
<funcdef>int <function>sd_json_parse_file</function></funcdef> |
| 80 |
<paramdef>FILE *<parameter>f</parameter></paramdef> |
| 81 |
<paramdef>const char *<parameter>path</parameter></paramdef> |
| 82 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 83 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 84 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 85 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 86 |
</funcprototype> |
| 87 |
|
| 88 |
<funcprototype> |
| 89 |
<funcdef>int <function>sd_json_parse_file_at</function></funcdef> |
| 90 |
<paramdef>FILE *<parameter>f</parameter></paramdef> |
| 91 |
<paramdef>int <parameter>dir_fd</parameter></paramdef> |
| 92 |
<paramdef>const char *<parameter>path</parameter></paramdef> |
| 93 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 94 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 95 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 96 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 97 |
</funcprototype> |
| 98 |
|
| 99 |
<funcprototype> |
| 100 |
<funcdef>int <function>sd_json_parse_fd</function></funcdef> |
| 101 |
<paramdef>const char *<parameter>path</parameter></paramdef> |
| 102 |
<paramdef>int <parameter>fd</parameter></paramdef> |
| 103 |
<paramdef>sd_json_parse_flags_t <parameter>flags</parameter></paramdef> |
| 104 |
<paramdef>sd_json_variant **<parameter>ret</parameter></paramdef> |
| 105 |
<paramdef>unsigned *<parameter>reterr_line</parameter></paramdef> |
| 106 |
<paramdef>unsigned *<parameter>reterr_column</parameter></paramdef> |
| 107 |
</funcprototype> |
| 108 |
</funcsynopsis> |
| 109 |
</refsynopsisdiv> |
| 110 |
|
| 111 |
<refsect1> |
| 112 |
<title>Description</title> |
| 113 |
|
| 114 |
<para><function>sd_json_parse()</function> parses the JSON string in <parameter>string</parameter> and |
| 115 |
returns the resulting JSON variant object in <parameter>ret</parameter>. The input must contain exactly |
| 116 |
one JSON value (object, array, string, number, boolean, or null); any trailing non-whitespace content |
| 117 |
after the first parsed value is considered an error.</para> |
| 118 |
|
| 119 |
<para>If parsing fails, the <parameter>reterr_line</parameter> and <parameter>reterr_column</parameter> |
| 120 |
arguments are set to the line and column (both one-based) where the parse error occurred. One or both |
| 121 |
may be passed as <constant>NULL</constant> if the caller is not interested in error location |
| 122 |
information. On success, the return value is non-negative and <parameter>ret</parameter> is set to a |
| 123 |
newly allocated JSON variant object (which must be freed with |
| 124 |
<citerefentry><refentrytitle>sd_json_variant_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 125 |
when no longer needed). <parameter>ret</parameter> may be passed as <constant>NULL</constant>, in which |
| 126 |
case the input is validated but no object is returned.</para> |
| 127 |
|
| 128 |
<para><function>sd_json_parse_continue()</function> is similar, but is intended for parsing a sequence of |
| 129 |
concatenated JSON values from a single input string. Instead of taking a <type>const char *</type> string |
| 130 |
directly, it takes a pointer to a <type>const char *</type> pointer. After each successful parse, the |
| 131 |
pointer is advanced past the consumed input, so that subsequent calls will parse the next JSON |
| 132 |
value. This is useful for parsing newline-delimited JSON (NDJSON) streams or similar concatenated JSON |
| 133 |
formats. Unlike <function>sd_json_parse()</function>, trailing content after the first JSON value is not |
| 134 |
considered an error — it is expected to be the beginning of the next value.</para> |
| 135 |
|
| 136 |
<para><function>sd_json_parse_with_source()</function> and |
| 137 |
<function>sd_json_parse_with_source_continue()</function> are similar to |
| 138 |
<function>sd_json_parse()</function> and <function>sd_json_parse_continue()</function>, respectively, but |
| 139 |
take an additional <parameter>source</parameter> argument. This is a human-readable string (typically a |
| 140 |
file name or other origin identifier) that is attached to the parsed JSON variant object and can later be |
| 141 |
retrieved via |
| 142 |
<citerefentry><refentrytitle>sd_json_variant_get_source</refentrytitle><manvolnum>3</manvolnum></citerefentry>. If |
| 143 |
<parameter>source</parameter> is <constant>NULL</constant>, no source information is |
| 144 |
attached. <function>sd_json_parse()</function> and <function>sd_json_parse_continue()</function> are |
| 145 |
equivalent to calling their <function>_with_source</function> counterparts with |
| 146 |
<parameter>source</parameter> set to <constant>NULL</constant>.</para> |
| 147 |
|
| 148 |
<para><function>sd_json_parse_file()</function> reads and parses a JSON value from a file. If the |
| 149 |
<parameter>f</parameter> argument is non-<constant>NULL</constant>, the JSON text is read from the |
| 150 |
specified <type>FILE</type> stream. If <parameter>f</parameter> is <constant>NULL</constant>, the file |
| 151 |
indicated by <parameter>path</parameter> is opened and read instead. The <parameter>path</parameter> |
| 152 |
argument serves a dual purpose: it is both used for opening the file (if <parameter>f</parameter> is |
| 153 |
<constant>NULL</constant>) and recorded as source information in the resulting JSON variant (see |
| 154 |
above).</para> |
| 155 |
|
| 156 |
<para><function>sd_json_parse_file_at()</function> is similar to |
| 157 |
<function>sd_json_parse_file()</function>, but takes an additional <parameter>dir_fd</parameter> argument |
| 158 |
which specifies a file descriptor referring to the directory to resolve relative paths specified in |
| 159 |
<parameter>path</parameter> against. If set to <constant>AT_FDCWD</constant>, relative paths are resolved |
| 160 |
against the current working directory, which is the default behaviour of |
| 161 |
<function>sd_json_parse_file()</function>.</para> |
| 162 |
|
| 163 |
<para><function>sd_json_parse_fd()</function> reads and parses a JSON value from the file referenced by |
| 164 |
the file descriptor <parameter>fd</parameter>. By default the file descriptor is internally duplicated |
| 165 |
and the caller's descriptor is left untouched (the current file offset will be shared with the original |
| 166 |
file descriptor however); the JSON text is read starting at the descriptor's current file offset. This |
| 167 |
behaviour may be modified via the <constant>SD_JSON_PARSE_SEEK0</constant>, |
| 168 |
<constant>SD_JSON_PARSE_DONATE_FD</constant> and <constant>SD_JSON_PARSE_REOPEN_FD</constant> flags, see |
| 169 |
below. The <parameter>path</parameter> argument is not used to open anything in this case; it is only |
| 170 |
recorded as source information in the resulting JSON variant (see above) and may be passed as |
| 171 |
<constant>NULL</constant>.</para> |
| 172 |
|
| 173 |
<para>The <parameter>flags</parameter> argument is a bitmask of zero or more of the following |
| 174 |
flags:</para> |
| 175 |
|
| 176 |
<variablelist> |
| 177 |
<varlistentry> |
| 178 |
<term><constant>SD_JSON_PARSE_SENSITIVE</constant></term> |
| 179 |
|
| 180 |
<listitem><para>Marks the resulting JSON variant as "sensitive", indicating that it contains secret |
| 181 |
key material or similar confidential data. Sensitive variants are erased from memory when freed and |
| 182 |
are excluded from certain debug logging and introspection operations. See |
| 183 |
<citerefentry><refentrytitle>sd_json_variant_sensitive</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 184 |
for details.</para></listitem> |
| 185 |
</varlistentry> |
| 186 |
|
| 187 |
<varlistentry> |
| 188 |
<term><constant>SD_JSON_PARSE_MUST_BE_OBJECT</constant></term> |
| 189 |
|
| 190 |
<listitem><para>Requires that the top-level JSON value be a JSON object (i.e. <literal>{…}</literal>). |
| 191 |
If the top-level value is an array, string, number, boolean, or null, parsing fails with |
| 192 |
<constant>-EINVAL</constant>.</para> |
| 193 |
|
| 194 |
<xi:include href="version-info.xml" xpointer="v261"/></listitem> |
| 195 |
</varlistentry> |
| 196 |
|
| 197 |
<varlistentry> |
| 198 |
<term><constant>SD_JSON_PARSE_MUST_BE_ARRAY</constant></term> |
| 199 |
|
| 200 |
<listitem><para>Requires that the top-level JSON value be a JSON array (i.e. <literal>[…]</literal>). |
| 201 |
If the top-level value is an object, string, number, boolean, or null, parsing fails with |
| 202 |
<constant>-EINVAL</constant>.</para> |
| 203 |
|
| 204 |
<xi:include href="version-info.xml" xpointer="v261"/></listitem> |
| 205 |
</varlistentry> |
| 206 |
|
| 207 |
<varlistentry> |
| 208 |
<term><constant>SD_JSON_PARSE_SEEK0</constant></term> |
| 209 |
|
| 210 |
<listitem><para>Before reading, seek the input to its beginning (i.e. file offset 0). This flag has |
| 211 |
no effect when parsing from a string. When used together with |
| 212 |
<constant>SD_JSON_PARSE_REOPEN_FD</constant> in <function>sd_json_parse_fd()</function> it is |
| 213 |
redundant, since a freshly reopened file descriptor starts at offset 0 anyway.</para> |
| 214 |
|
| 215 |
<xi:include href="version-info.xml" xpointer="v261"/></listitem> |
| 216 |
</varlistentry> |
| 217 |
|
| 218 |
<varlistentry> |
| 219 |
<term><constant>SD_JSON_PARSE_DONATE_FD</constant></term> |
| 220 |
|
| 221 |
<listitem><para>Only has an effect on <function>sd_json_parse_fd()</function>. If set, ownership of |
| 222 |
the file descriptor passed in <parameter>fd</parameter> is transferred into the call: the descriptor |
| 223 |
is consumed and closed before the function returns, including in the error path. If not set (the |
| 224 |
default), the caller retains ownership of <parameter>fd</parameter> and the function operates on an |
| 225 |
internally duplicated descriptor instead. This flag may not be combined with |
| 226 |
<constant>SD_JSON_PARSE_REOPEN_FD</constant>.</para> |
| 227 |
|
| 228 |
<xi:include href="version-info.xml" xpointer="v261"/></listitem> |
| 229 |
</varlistentry> |
| 230 |
|
| 231 |
<varlistentry> |
| 232 |
<term><constant>SD_JSON_PARSE_REOPEN_FD</constant></term> |
| 233 |
|
| 234 |
<listitem><para>Only has an effect on <function>sd_json_parse_fd()</function>. If set, the file |
| 235 |
descriptor passed in <parameter>fd</parameter> is reopened (read-only) before reading, instead of |
| 236 |
being duplicated. This is primarily useful to obtain an independent file offset (positioned at the |
| 237 |
beginning of the file) and a clean, read-only access mode, even if the original descriptor was opened |
| 238 |
differently (for example with <constant>O_PATH</constant>). The caller retains ownership of the |
| 239 |
original descriptor, which is left untouched. This flag may not be combined with |
| 240 |
<constant>SD_JSON_PARSE_DONATE_FD</constant>.</para> |
| 241 |
|
| 242 |
<xi:include href="version-info.xml" xpointer="v261"/></listitem> |
| 243 |
</varlistentry> |
| 244 |
</variablelist> |
| 245 |
|
| 246 |
<para>If both <constant>SD_JSON_PARSE_MUST_BE_OBJECT</constant> and |
| 247 |
<constant>SD_JSON_PARSE_MUST_BE_ARRAY</constant> are set, both objects and arrays are accepted, but |
| 248 |
non-container values (strings, numbers, booleans, null) are still refused.</para> |
| 249 |
</refsect1> |
| 250 |
|
| 251 |
<refsect1> |
| 252 |
<title>Return Value</title> |
| 253 |
|
| 254 |
<para>On success, these functions return 0. On failure, they return a negative errno-style error |
| 255 |
code.</para> |
| 256 |
|
| 257 |
<refsect2> |
| 258 |
<title>Errors</title> |
| 259 |
|
| 260 |
<para>Returned errors may indicate the following problems:</para> |
| 261 |
|
| 262 |
<variablelist> |
| 263 |
<varlistentry> |
| 264 |
<term><constant>-EINVAL</constant></term> |
| 265 |
|
| 266 |
<listitem><para>The input is not valid JSON, the input contains trailing content after the parsed |
| 267 |
value (only for non-<function>_continue</function> variants), or a top-level type constraint |
| 268 |
specified via <constant>SD_JSON_PARSE_MUST_BE_OBJECT</constant> or |
| 269 |
<constant>SD_JSON_PARSE_MUST_BE_ARRAY</constant> was violated.</para></listitem> |
| 270 |
</varlistentry> |
| 271 |
|
| 272 |
<varlistentry> |
| 273 |
<term><constant>-ENODATA</constant></term> |
| 274 |
|
| 275 |
<listitem><para>The input string is empty or <constant>NULL</constant>.</para></listitem> |
| 276 |
</varlistentry> |
| 277 |
|
| 278 |
<varlistentry> |
| 279 |
<term><constant>-ENOMEM</constant></term> |
| 280 |
|
| 281 |
<listitem><para>Memory allocation failed.</para></listitem> |
| 282 |
</varlistentry> |
| 283 |
</variablelist> |
| 284 |
</refsect2> |
| 285 |
</refsect1> |
| 286 |
|
| 287 |
<xi:include href="libsystemd-pkgconfig.xml" /> |
| 288 |
|
| 289 |
<refsect1> |
| 290 |
<title>History</title> |
| 291 |
<para><function>sd_json_parse()</function>, |
| 292 |
<function>sd_json_parse_continue()</function>, |
| 293 |
<function>sd_json_parse_with_source()</function>, |
| 294 |
<function>sd_json_parse_with_source_continue()</function>, |
| 295 |
<function>sd_json_parse_file()</function>, and |
| 296 |
<function>sd_json_parse_file_at()</function> were added in version 257.</para> |
| 297 |
|
| 298 |
<para><function>sd_json_parse_fd()</function> and the <constant>SD_JSON_PARSE_MUST_BE_OBJECT</constant>, |
| 299 |
<constant>SD_JSON_PARSE_MUST_BE_ARRAY</constant>, <constant>SD_JSON_PARSE_SEEK0</constant>, |
| 300 |
<constant>SD_JSON_PARSE_DONATE_FD</constant> and <constant>SD_JSON_PARSE_REOPEN_FD</constant> flags were |
| 301 |
added in version 261.</para> |
| 302 |
</refsect1> |
| 303 |
|
| 304 |
<refsect1> |
| 305 |
<title>See Also</title> |
| 306 |
|
| 307 |
<para><simplelist type="inline"> |
| 308 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 309 |
<member><citerefentry><refentrytitle>sd-json</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 310 |
<member><citerefentry><refentrytitle>sd_json_variant_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 311 |
<member><citerefentry><refentrytitle>sd_json_variant_get_source</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 312 |
<member><citerefentry><refentrytitle>sd_json_dispatch</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 313 |
</simplelist></para> |
| 314 |
</refsect1> |
| 315 |
</refentry> |