Skip to content

File syn_json_read.h

FileList > src > syntropic > util > syn_json_read.h

Go to the source code of this file

Minimal JSON reader — in-place, zero-alloc. More...

  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_JsonReader
JSON reader — token array + parse state.
struct SYN_JsonToken
Parsed JSON token — key + value + type.

Public Types

Type Name
enum SYN_JsonType
JSON value types.

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.

Macros

Type Name
define SYN_JSON_MAX_TOKENS 32
Maximum number of parsed JSON tokens per document.

Detailed Description

Parses a JSON string in-place, providing token-based access. No dynamic memory, no callbacks — just walks through the buffer and lets you query values by key path.

** **

char json[] = "{\"ssid\":\"MyNet\",\"channel\":6,\"hidden\":false}";
SYN_JsonReader r;
syn_json_parse(&r, json, strlen(json));

char ssid[33];
if (syn_json_get_str(&r, "ssid", ssid, sizeof(ssid))) { ... }

int32_t ch;
if (syn_json_get_int(&r, "channel", &ch)) { ... }

bool hidden;
if (syn_json_get_bool(&r, "hidden", &hidden)) { ... }

** **

  • Top-level must be an object {}
  • Nested objects accessed via dot notation: "wifi.ssid"
  • No array indexing (arrays can be iterated)
  • Modifies the input buffer (inserts null terminators)
  • Max 32 tokens (keys + values) per parse

Public Types Documentation

enum SYN_JsonType

JSON value types.

enum SYN_JsonType {
    SYN_JSON_NONE = 0,
    SYN_JSON_STRING = 1,
    SYN_JSON_NUMBER = 2,
    SYN_JSON_BOOL = 3,
    SYN_JSON_NULL = 4,
    SYN_JSON_OBJECT = 5,
    SYN_JSON_ARRAY = 6
};


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.


Macro Definition Documentation

define SYN_JSON_MAX_TOKENS

Maximum number of parsed JSON tokens per document.

#define SYN_JSON_MAX_TOKENS `32`



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