11.2. json — JSON encoding and decoding

Source code: src/encode/json.h, src/encode/json.c

Test code: tst/encode/json/main.c

Test coverage: src/encode/json.c


Enums

enum json_type_t

Values:

JSON_UNDEFINED = 0

Undefined type.

JSON_OBJECT = 1

Object, {}.

JSON_ARRAY = 2

Array, [].

JSON_STRING = 3

String, \"...\\".

JSON_PRIMITIVE = 4

Other primitive: number, boolean (true/false) or null.

enum json_err_t

Values:

JSON_ERROR_NOMEM = -1

Not enough tokens were provided.

JSON_ERROR_INVAL = -2

Invalid character inside JSON string.

JSON_ERROR_PART = -3

The string is not a full JSON packet, more bytes expected.

Functions

int json_init(struct json_t *self_p, struct json_tok_t *tokens_p, int num_tokens)

Initialize given JSON object. The JSON object must be initialized before it can be used to parse and dump JSON data.

Return
zero(0) or negative error code.
Parameters
  • self_p: JSON object to initialize.
  • tokens_p: Array of tokens. The tokens are either filled by the parsing function json_parse(), or already filled by the user when calling this function. The latter can be used to dump the tokens as a string by calling json_dump() or json_dumps().
  • num_tokens: Number of tokens in the array.

int json_parse(struct json_t *self_p, const char *js_p, size_t len)

Parse given JSON data string into and array of tokens, each describing a single JSON object.

Return
Number of decoded tokens or negative error code.
Parameters
  • self_p: JSON object.
  • js_p: JSON string to parse.
  • len: JSON string length in bytes.

ssize_t json_dumps(struct json_t *self_p, struct json_tok_t *tokens_p, char *js_p)

Format and write given JSON tokens into a string.

Return
Dumped string length (not including termination) or negative error code.
Parameters
  • self_p: JSON object.
  • tokens_p: Root token to dump. Set to NULL to dump the whole object.
  • js_p: Dumped null terminated JSON string.

ssize_t json_dump(struct json_t *self_p, struct json_tok_t *tokens_p, void *out_p)

Format and write given JSON tokens to given channel.

Return
Dumped string length (not including termination) or negative error code.
Parameters
  • self_p: JSON object.
  • tokens_p: Root token to dump. Set to NULL to dump the whole object.
  • out_p: Channel to dump the null terminated JSON string to.

struct json_tok_t *json_root(struct json_t *self_p)

Get the root token.

Return
The root token or NULL on failure.
Parameters
  • self_p: JSON object.

struct json_tok_t *json_object_get(struct json_t *self_p, const char *key_p, struct json_tok_t *object_p)

Get the value the string token with given key.

Return
Token or NULL on error.
Parameters
  • self_p: JSON object.
  • key_p: Key of the value to get.
  • object_p: The object to get the value from.

struct json_tok_t *json_object_get_primitive(struct json_t *self_p, const char *key_p, struct json_tok_t *object_p)

Get the value of the primitive token with given key.

Return
Token or NULL on error.
Parameters
  • self_p: JSON object.
  • key_p: Key of the value to get.
  • object_p: The object to get the value from.

struct json_tok_t *json_array_get(struct json_t *self_p, int index, struct json_tok_t *array_p)

Get the token of given array index.

Return
Token or NULL on error.
Parameters
  • self_p: JSON object.
  • index: Index to get.
  • array_p: The array to get the element from.

void json_token_object(struct json_tok_t *token_p, int num_keys)

Initialize a JSON object token.

Parameters
  • token_p: Initialized token.
  • num_keys: Number of keys in the object.

void json_token_array(struct json_tok_t *token_p, int num_elements)

Initialize a JSON array token.

Parameters
  • token_p: Initialized token.
  • num_elements: Number of array elements.

void json_token_true(struct json_tok_t *token_p)

Initialize a JSON boolean true token.

Parameters
  • token_p: Initialized token.

void json_token_false(struct json_tok_t *token_p)

Initialize a JSON boolean false token.

Parameters
  • token_p: Initialized token.

void json_token_null(struct json_tok_t *token_p)

Initialize a JSON null token.

Parameters
  • token_p: Initialized token.

void json_token_number(struct json_tok_t *token_p, const char *buf_p, size_t size)

Initialize a JSON number (integer/float) token.

Parameters
  • token_p: Initialized token.
  • buf_p: Number as a string.
  • size: String length.

void json_token_string(struct json_tok_t *token_p, const char *buf_p, size_t size)

Initialize a JSON string token.

Parameters
  • token_p: Initialized token.
  • buf_p: String.
  • size: String length.

struct json_tok_t

Public Members

json_type_t type
const char *buf_p
size_t size
int num_tokens
struct json_t

Public Members

unsigned int pos

Offset in the JSON string.

unsigned int toknext

Next token to allocate.

int toksuper

Superior token node, e.g parent object or array.

struct json_tok_t *tokens_p

Array of tokens.

int num_tokens

Number of tokens in the tokens array.