Skip to content

File syn_json_read.c

FileList > src > syntropic > util > syn_json_read.c

Go to the source code of this file

Minimal JSON reader — in-place tokenizer.

  • #include "../util/syn_assert.h"
  • #include "syn_json_read.h"
  • #include <string.h>

Public Functions

Type Name
const SYN_JsonToken * syn_json_find (const SYN_JsonReader * r, const char * key)
Find a token by key name.
bool syn_json_get_bool (const SYN_JsonReader * r, const char * key, bool * out)
Get a boolean value by key.
bool syn_json_get_int (const SYN_JsonReader * r, const char * key, int32_t * out)
Get an integer value by key.
bool syn_json_get_str (const SYN_JsonReader * r, const char * key, char * out, size_t out_sz)
Get a string value by key.
SYN_JsonType syn_json_get_type (const SYN_JsonReader * r, const char * key)
Get the type of a value by key.
bool syn_json_is_null (const SYN_JsonReader * r, const char * key)
Check if a key exists and is null.
bool syn_json_parse (SYN_JsonReader * r, char * json, size_t len)
Parse a JSON string in-place.

Public Static Functions

Type Name
int32_t parse_int (const char * s)
Parse a signed integer from a string.
char * parse_object (SYN_JsonReader * r, char * p, const char * end, uint8_t depth)
Recursive JSON object parser.
char * parse_string (char * p, const char * end, const char ** out)
Parse a JSON string starting at p.
char * skip_value (char * p, const char * end)
Skip a JSON value (any type).
char * skip_ws (char * p, const char * end)
Skip whitespace.

Public Functions Documentation

function syn_json_find

Find a token by key name.

const SYN_JsonToken * syn_json_find (
    const SYN_JsonReader * r,
    const char * key
) 

Use dot notation for nested keys: "wifi.ssid"

Parameters:

  • r Reader.
  • key Key to find.

Returns:

Pointer to token, or NULL if not found.


function syn_json_get_bool

Get a boolean value by key.

bool syn_json_get_bool (
    const SYN_JsonReader * r,
    const char * key,
    bool * out
) 

Parameters:

  • r Reader.
  • key Key to find.
  • out Output value.

Returns:

true if key found and value is a boolean.


function syn_json_get_int

Get an integer value by key.

bool syn_json_get_int (
    const SYN_JsonReader * r,
    const char * key,
    int32_t * out
) 

Parameters:

  • r Reader.
  • key Key to find.
  • out Output value.

Returns:

true if key found and value is a number.


function syn_json_get_str

Get a string value by key.

bool syn_json_get_str (
    const SYN_JsonReader * r,
    const char * key,
    char * out,
    size_t out_sz
) 

Parameters:

  • r Reader.
  • key Key to find.
  • out Output buffer for the string.
  • out_sz Buffer capacity.

Returns:

true if key found and value is a string.


function syn_json_get_type

Get the type of a value by key.

SYN_JsonType syn_json_get_type (
    const SYN_JsonReader * r,
    const char * key
) 

Parameters:

  • r Reader.
  • key Key to find.

Returns:

SYN_JsonType, or SYN_JSON_NONE if not found.


function syn_json_is_null

Check if a key exists and is null.

bool syn_json_is_null (
    const SYN_JsonReader * r,
    const char * key
) 

Parameters:

  • r Reader.
  • key Key to find.

Returns:

true if key exists and value is null.


function syn_json_parse

Parse a JSON string in-place.

bool syn_json_parse (
    SYN_JsonReader * r,
    char * json,
    size_t len
) 

Tokenizes the JSON into key-value pairs. Modifies the input buffer (inserts null terminators at string boundaries).

Parameters:

  • r Reader instance.
  • json JSON string (will be modified).
  • len Length of JSON string.

Returns:

true if parsing succeeded.


Public Static Functions Documentation

function parse_int

Parse a signed integer from a string.

static int32_t parse_int (
    const char * s
) 

Parameters:

  • s Input string.

Returns:

Parsed value.


function parse_object

Recursive JSON object parser.

static char * parse_object (
    SYN_JsonReader * r,
    char * p,
    const char * end,
    uint8_t depth
) 

Tokenizes key-value pairs at the given depth.

Parameters:

  • r JSON reader instance.
  • p Current position.
  • end End of buffer.
  • depth Current nesting depth.

Returns:

Pointer past the object.


function parse_string

Parse a JSON string starting at p.

static char * parse_string (
    char * p,
    const char * end,
    const char ** out
) 

Must point to opening '"'. Null-terminates in-place.

Parameters:

  • p Current position (at opening '"').
  • end End of buffer.
  • out [out] Start of the string content.

Returns:

Pointer past the closing '"', or NULL on error.


function skip_value

Skip a JSON value (any type).

static char * skip_value (
    char * p,
    const char * end
) 

Parameters:

  • p Current position.
  • end End of buffer.

Returns:

Pointer past the value.


function skip_ws

Skip whitespace.

static char * skip_ws (
    char * p,
    const char * end
) 

Parameters:

  • p Current position.
  • end End of buffer.

Returns:

Pointer to next non-whitespace char.



The documentation for this class was generated from the following file src/syntropic/util/syn_json_read.c