LwJSON

group LWJSON

LwJSON - Lightweight JSON format parser.

Defines

LWJSON_ARRAYSIZE(x)

Get size of statically allocated array.

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

Returns

Number of elements in array

lwjson_get_tokens_used(lw)

Get number of tokens used to parse JSON.

Parameters
  • lw[in] Pointer to LwJSON instance

Returns

Number of tokens used to parse JSON

lwjson_get_first_token(lw)

Get very first token of LwJSON instance.

Parameters
  • lw[in] Pointer to LwJSON instance

Returns

Pointer to first token

lwjson_get_val_int(token)

Get token value for LWJSON_TYPE_NUM_INT type.

Parameters
  • token[in] token with integer type

Returns

Int number if type is integer, 0 otherwise

lwjson_get_val_real(token)

Get token value for LWJSON_TYPE_NUM_REAL type.

Parameters
  • token[in] token with real type

Returns

Real numbeer if type is real, 0 otherwise

lwjson_get_first_child(token)

Get first child token for LWJSON_TYPE_OBJECT or LWJSON_TYPE_ARRAY types.

Parameters
  • token[in] token with integer type

Returns

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

lwjson_get_val_string_length(token)

Get length of string for LWJSON_TYPE_STRING token type.

Parameters
  • token[in] token with string type

Returns

Length of string in units of bytes

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

Generic error message

enumerator lwjsonERRJSON

Error JSON format

enumerator lwjsonERRMEM

Memory error

enumerator lwjsonERRPAR

Parameter error

Functions

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

Setup LwJSON instance for parsing JSON strings.

Parameters
  • lw[inout] LwJSON instance

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

  • tokens_len[in] Number of tokens

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

lwjsonr_t lwjson_parse_ex(lwjson_t *lw, const void *json_data, size_t len)

Parse JSON data with length parameter JSON format must be complete and must comply with RFC4627.

Parameters
  • lw[inout] LwJSON instance

  • json_data[in] JSON string to parse

  • jsonČlen[in] JSON data length

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

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.

Parameters
  • lw[inout] LwJSON instance

  • json_str[in] JSON string to parse

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

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.

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

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

Returns

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

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.

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

  • token[in] 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

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

Returns

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

lwjsonr_t lwjson_free(lwjson_t *lw)

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

Parameters

lw[inout] LwJSON instance

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

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

token[in] 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

lw[in] LwJSON instance to print

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

Get string value from JSON token.

Parameters
  • token[in] Token with string type

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

Returns

Pointer to string or NULL if invalid token type

static inline uint8_t lwjson_string_compare(const lwjson_token_t *token, const char *str)

Compare string token with user input string for a case-sensitive match.

Parameters
  • token[in] Token with string type

  • str[in] NULL-terminated string to compare

Returns

1 if equal, 0 otherwise

static inline uint8_t lwjson_string_compare_n(const lwjson_token_t *token, const char *str, size_t len)

Compare string token with user input string for a case-sensitive match.

Parameters
  • token[in] Token with string type

  • str[in] NULL-terminated string to compare

  • len[in] Length of the string in bytes

Returns

1 if equal, 0 otherwise

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