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 top token on a list.

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 for child token for LWJSON_TYPE_OBJECT or LWJSON_TYPE_ARRAY types.

Return

Pointer to first child

Parameters
  • [in] token: token with integer 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

lwjsonr_t lwjson_reset(lwjson_t *lw)

Reset token instances and prepare for new parsing.

Return

lwjsonOK on success, member of lwjsonr_t otherwise

Parameters
  • [inout] lw: LwJSON instance

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

lwjsonr_t lwjson_free(lwjson_t *lw)
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(lwjson_token_t *token, size_t *str_len)

Get string value from JSON token.

Return

Pointer to string

Parameters
  • [in] token: Token with string type

  • [out] str_len: Pointer to variable holding length of string

struct lwjson_token_t
#include <lwjson.h>

JSON token.

Public Members

struct lwjson_token *next

Next token on a list

struct lwjson_token *parent

Parent token (think about optimization and remove this one?)

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

Value if type is not LWJSON_TYPE_OBJECT or LWJSON_TYPE_ARRAY

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

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