1 xtk_json
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_json.h

Reads JSON text into a tree of values.

This module parses JSON text into a tree of XtkJsonValue nodes, which the caller then walks with the getters below. It knows no schema: what the keys and values mean is up to the caller. It does not draw anything and needs no X display. See also xtk_csv.h and xtk_xml.h.

It reads objects, arrays, strings, numbers, true, false and null. Strings may use all the JSON escape sequences, including \uXXXX with surrogate pairs for characters outside the Basic Multilingual Plane; the decoded text is UTF-8; a surrogate without its other half becomes U+FFFD. Numbers are read in JSON's own form, with . as the decimal point whatever the locale. A UTF-8 byte order mark at the start is skipped.

Use. Call xtk_json_parse(), check the result for NULL, walk it with xtk_json_object_get(), xtk_json_array_count(), xtk_json_array_get() and the xtk_json_get_*() calls, and free it with xtk_json_destroy(). Each array and object owns its children, so destroying the root frees the whole tree. Pointers returned by the getters point into the tree and are valid until it is destroyed.

Example.

#include <stdio.h>
#include "xtk_json.h"

XtkJsonValue *root = xtk_json_parse("{\"name\": \"xtk\", \"sizes\": [10, 12]}");
if (root) {
    const char *name = xtk_json_get_string(xtk_json_object_get(root, "name"));
    const XtkJsonValue *sizes = xtk_json_object_get(root, "sizes");
    printf("%s\n", name ? name : "(none)");
    for (int i = 0; i < xtk_json_array_count(sizes); i++)
        printf("%g\n", xtk_json_get_number(xtk_json_array_get(sizes, i), 0));
    xtk_json_destroy(root);
}
 * 

Limitations.

  • It is not a validating parser. On bad input it stops where the error is and returns the part of the tree it has built; text after the first value is ignored. A value it cannot read inside an array or object becomes null.
  • Nesting deeper than 500 arrays or objects fails: xtk_json_parse() returns NULL. This keeps deep input from overflowing the stack.
  • Duplicate keys are all kept; xtk_json_object_get() finds the first one.
  • A number is read as far as JSON's number form goes (-, digits, a fraction, an exponent); forms JSON does not allow, such as hexadecimal, are not numbers.
  • A \u0000 escape ends the decoded string at that point.
  • Allocation failures are not checked.
#include "xtk_json.h"

Types: XtkJsonType, XtkJsonValue

Functions: xtk_json_parse, xtk_json_destroy, xtk_json_object_get, xtk_json_array_count, xtk_json_array_get, xtk_json_get_string, xtk_json_get_number, xtk_json_get_bool

Types

XtkJsonType

The kind of a JSON value.

Name Value Description
XTK_JSON_NULL null. No field holds data.
XTK_JSON_BOOL true or false, in boolean.
XTK_JSON_NUMBER A number, in number.
XTK_JSON_STRING A string, in string.
XTK_JSON_ARRAY An array, in items and item_count.
XTK_JSON_OBJECT An object, in keys, values and member_count.

XtkJsonValue

One JSON value. An array or object owns its children.

Field Type Description
type XtkJsonType The kind of value. It decides which of the fields below hold data.
string char * For XTK_JSON_STRING: the decoded UTF-8 text, NUL-terminated.
number double For XTK_JSON_NUMBER: the value.
boolean int For XTK_JSON_BOOL: 1 for true, 0 for false.
items struct XtkJsonValue ** For XTK_JSON_ARRAY: the elements, in order.
item_count int For XTK_JSON_ARRAY: the number of elements.
keys char ** For XTK_JSON_OBJECT: the member keys, in order; keys[i] belongs to values[i].
values struct XtkJsonValue ** For XTK_JSON_OBJECT: the member values, in the same order as keys.
member_count int For XTK_JSON_OBJECT: the number of members.

Functions

xtk_json_parse

XtkJsonValue *xtk_json_parse(const char *text);

Parses JSON text into a tree of values.

A leading UTF-8 byte order mark is skipped. Only the first value in the text is read.

Parameter Description
text NUL-terminated JSON text, or NULL.

Returns: The root value, which the caller frees with xtk_json_destroy(), or NULL when text is NULL, does not start with a JSON value (after white space), or nests deeper than 500 levels.

xtk_json_destroy

void xtk_json_destroy(XtkJsonValue *value);

Frees a value and everything it holds.

Parameter Description
value The value to free, usually the root from xtk_json_parse(). NULL is allowed and does nothing.

xtk_json_object_get

const XtkJsonValue *xtk_json_object_get(const XtkJsonValue *value, const char *key);

Finds a member of an object by its key.

Parameter Description
value The object, or NULL.
key The key to look for, or NULL (which finds nothing).

Returns: The value of the first member with that key, or NULL when value is NULL, is not an object, has no such key, or key is NULL.

xtk_json_array_count

int xtk_json_array_count(const XtkJsonValue *value);

Gives the number of elements in an array.

Parameter Description
value The array, or NULL.

Returns: The number of elements, or 0 when value is NULL or not an array.

xtk_json_array_get

const XtkJsonValue *xtk_json_array_get(const XtkJsonValue *value, int index);

Gives one element of an array.

Parameter Description
value The array, or NULL.
index The index of the element, from 0.

Returns: The element, or NULL when value is NULL, is not an array, or index is out of range.

xtk_json_get_string

const char *xtk_json_get_string(const XtkJsonValue *value);

Gives the text of a string value.

Parameter Description
value The value, or NULL.

Returns: The string, or NULL when value is NULL or not a string. An empty JSON string gives "", so a caller can tell an empty string from a missing one.

xtk_json_get_number

double xtk_json_get_number(const XtkJsonValue *value, double fallback);

Gives the value of a number.

Parameter Description
value The value, or NULL.
fallback The value to return when value is not a number.

Returns: The number, or fallback when value is NULL or not a number.

xtk_json_get_bool

int xtk_json_get_bool(const XtkJsonValue *value, int fallback);

Gives the value of a boolean.

Parameter Description
value The value, or NULL.
fallback The value to return when value is not a boolean.

Returns: 1 for true, 0 for false, or fallback when value is NULL or not a boolean.