LwJSON

group LWJSON

LwJSON - Lightweight JSON format parser.

Defines

LWJSON_ARRAYSIZE(x)

Get size of statically allocated array.

Return

Number of elements in array

Parameters
  • [in] x: Object to get array size of

lwjson_get_tokens_used(lw)

Get number of tokens used to parse JSON.

Return

Number of tokens used to parse JSON

Parameters
  • [in] lw: Pointer to LwJSON instance

lwjson_get_first_token(lw)

Get very first token of LwJSON instance.

Return

Pointer to first token

Parameters
  • [in] lw: Pointer to LwJSON instance

lwjson_get_val_int(token)

Get token value for LWJSON_TYPE_NUM_INT type.

Return

Int number if type is integer, 0 otherwise

Parameters
  • [in] token: token with integer type

lwjson_get_val_real(token)

Get token value for LWJSON_TYPE_NUM_REAL type.

Return

Real numbeer if type is real, 0 otherwise

Parameters
  • [in] token: token with real type

lwjson_get_first_child(token)

Get first child token for LWJSON_TYPE_OBJECT or LWJSON_TYPE_ARRAY types.

Return

Pointer to first child or NULL if parent token is not object or array

Parameters
  • [in] token: token with integer type

lwjson_get_val_string_length(token)

Get length of string for LWJSON_TOKEN_STRING token type.

Return

Length of string in units of bytes

Parameters
  • [in] token: token with string type

Typedefs

typedef LWJSON_CFG_REAL_TYPE lwjson_real_t

Real data type.

typedef LWJSON_CFG_INT_TYPE lwjson_int_t

Integer data type.

Enums

enum lwjson_type_t

List of supported JSON types.

Values:

enumerator LWJSON_TYPE_STRING

String/Text format. Everything that has beginning and ending quote character

enumerator LWJSON_TYPE_NUM_INT

Number type for integer

enumerator LWJSON_TYPE_NUM_REAL

Number type for real number

enumerator LWJSON_TYPE_OBJECT

Object data type

enumerator LWJSON_TYPE_ARRAY

Array data type

enumerator LWJSON_TYPE_TRUE

True boolean value

enumerator LWJSON_TYPE_FALSE

False boolean value

enumerator LWJSON_TYPE_NULL

Null value

enum lwjsonr_t

JSON result enumeration.

Values:

enumerator lwjsonOK

Function returns successfully

enumerator lwjsonERR
enumerator lwjsonERRJSON

Error JSON format

enumerator lwjsonERRMEM

Memory error

Functions

lwjsonr_t lwjson_init(lwjson_t *lw, lwjson_token_t *tokens, size_t tokens_len)

Setup LwJSON instance for parsing JSON strings.

Return

lwjsonOK on success, member of lwjsonr_t otherwise

Parameters
  • [inout] lw: LwJSON instance

  • [in] tokens: Pointer to array of tokens used for parsing

  • [in] tokens_len: Number of tokens

lwjsonr_t lwjson_parse(lwjson_t *lw, const char *json_str)

Parse input JSON format JSON format must be complete and must comply with RFC4627.

Return

lwjsonOK on success, member of lwjsonr_t otherwise

Parameters
  • [inout] lw: LwJSON instance

  • [in] json_str: JSON string to parse

const lwjson_token_t *lwjson_find(lwjson_t *lw, const char *path)

Find first match in the given path for JSON entry JSON must be valid and parsed with lwjson_parse function.

Return

Pointer to found token on success, NULL if token cannot be found

Parameters
  • [in] lw: JSON instance with parsed JSON string

  • [in] path: Path with dot-separated entries to search for the JSON key to return

const lwjson_token_t *lwjson_find_ex(lwjson_t *lw, const lwjson_token_t *token, const char *path)

Find first match in the given path for JSON path JSON must be valid and parsed with lwjson_parse function.

Return

Pointer to found token on success, NULL if token cannot be found

Parameters
  • [in] lw: JSON instance with parsed JSON string

  • [in] token: Root token to start search at. Token must be type LWJSON_TYPE_OBJECT or LWJSON_TYPE_ARRAY. Set to NULL to use root token of LwJSON object

  • [in] path: path with dot-separated entries to search for JSON key

lwjsonr_t lwjson_free(lwjson_t *lw)

Free token instances (specially used in case of dynamic memory allocation)

Return

lwjsonOK on success, member of lwjsonr_t otherwise

Parameters
  • [inout] lw: LwJSON instance

void lwjson_print_token(const lwjson_token_t *token)

Prints and outputs token data to the stream output.

Note

This function is not re-entrant

Parameters
  • [in] token: Token to print

void lwjson_print_json(const lwjson_t *lw)

Prints and outputs full parsed LwJSON instance.

Note

This function is not re-entrant

Parameters
  • [in] lw: LwJSON instance to print

const char *lwjson_get_val_string(const lwjson_token_t *token, size_t *str_len)

Get string value from JSON token.

Return

Pointer to string or NULL if invalid token type

Parameters
  • [in] token: Token with string type

  • [out] str_len: Pointer to variable holding length of string. Set to NULL if not used

struct lwjson_token_t
#include <lwjson.h>

JSON token.

Public Members

struct lwjson_token *next

Next token on a list

lwjson_type_t type

Token type

const char *token_name

Token name (if exists)

size_t token_name_len

Length of token name (this is needed to support const input strings to parse)

const char *token_value

Pointer to the beginning of the string

size_t token_value_len

Length of token value (this is needed to support const input strings to parse)

struct lwjson_token_t::[anonymous]::[anonymous] str

String data

lwjson_real_t num_real

Real number format

lwjson_int_t num_int

Int number format

struct lwjson_token *first_child

First children object for object or array type

union lwjson_token_t::[anonymous] u

Union with different data types

struct lwjson_t
#include <lwjson.h>

LwJSON instance.

Public Members

lwjson_token_t *tokens

Pointer to array of tokens

size_t tokens_len

Size of all tokens

size_t next_free_token_pos

Position of next free token instance

lwjson_token_t first_token

First token on a list

uint8_t parsed

Flag indicating JSON parsing has finished successfully

struct lwjson_t::[anonymous] flags

List of flags