LwJSON

group LWJSON

LwJSON - Lightweight JSON format parser.

LWJSON_STREAM_SEQ

Helper functions for stack analysis in a callback function

Note

Useful exclusively for streaming functions

lwjson_stack_seq_1(jsp, start_num, sp0)

Check the sequence of JSON stack, starting from start_number index.

Note

This applies only to one sequence element. Other macros, starting with lwjson_stack_seq_X (where X is the sequence length), provide more parameters for longer sequences.

Parameters
  • jsp[in] LwJSON stream instance

  • start_num[in] Start number in the stack. Typically starts with 0, but user may choose another number, if intention is to check partial sequence only

  • sp0[in] Stream stack type. Value of lwjson_stream_type_t, but only last part of the enum. If user is interested in the LWJSON_STREAM_TYPE_OBJECT, you should only write OBJECT as parameter. Idea behind is to make code smaller and easier to read, especially when using other sequence values with more parameters.

Returns

0 if sequence doesn’t match, non-zero otherwise

lwjson_stack_seq_2(jsp, start_num, sp0, sp1)
lwjson_stack_seq_3(jsp, start_num, sp0, sp1, sp2)
lwjson_stack_seq_4(jsp, start_num, sp0, sp1, sp2, sp3)
lwjson_stack_seq_5(jsp, start_num, sp0, sp1, sp2, sp3, sp4)
lwjson_stack_seq_6(jsp, start_num, sp0, sp1, sp2, sp3, sp4, sp5)
lwjson_stack_seq_7(jsp, start_num, sp0, sp1, sp2, sp3, sp4, sp5, sp6)
lwjson_stack_seq_8(jsp, start_num, sp0, sp1, sp2, sp3, sp4, sp5, sp6, sp7)

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(lwobj)

Get number of tokens used to parse JSON.

Parameters
  • lwobj[in] Pointer to LwJSON instance

Returns

Number of tokens used to parse JSON

lwjson_get_first_token(lwobj)

Get very first token of LwJSON instance.

Parameters
  • lwobj[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.

typedef void (*lwjson_stream_parser_callback_fn)(struct lwjson_stream_parser *jsp, lwjson_stream_type_t type)

Callback function for various events.

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 = 0x00

Function returns successfully

enumerator lwjsonERR

Generic error message

enumerator lwjsonERRJSON

Error JSON format

enumerator lwjsonERRMEM

Memory error

enumerator lwjsonERRPAR

Parameter error

enumerator lwjsonSTREAMWAITFIRSTCHAR

Streaming parser did not yet receive first valid character indicating start of JSON sequence

enumerator lwjsonSTREAMDONE

Streaming parser is done, closing character matched the stream opening one

enumerator lwjsonSTREAMINPROG

Stream parsing is still in progress

enum lwjson_stream_type_t

Object type for streaming parser.

Values:

enumerator LWJSON_STREAM_TYPE_NONE

No entry - not used

enumerator LWJSON_STREAM_TYPE_OBJECT

Object indication

enumerator LWJSON_STREAM_TYPE_OBJECT_END

Object end indication

enumerator LWJSON_STREAM_TYPE_ARRAY

Array indication

enumerator LWJSON_STREAM_TYPE_ARRAY_END

Array end indication

enumerator LWJSON_STREAM_TYPE_KEY

Key string

enumerator LWJSON_STREAM_TYPE_STRING

Strin type

enumerator LWJSON_STREAM_TYPE_TRUE

True primitive

enumerator LWJSON_STREAM_TYPE_FALSE

False primitive

enumerator LWJSON_STREAM_TYPE_NULL

Null primitive

enumerator LWJSON_STREAM_TYPE_NUMBER

Generic number

enum lwjson_stream_state_t

Values:

enumerator LWJSON_STREAM_STATE_WAITINGFIRSTCHAR = 0x00

State to wait for very first opening character

enumerator LWJSON_STREAM_STATE_PARSING

In parsing of the first char state - detecting next character state

enumerator LWJSON_STREAM_STATE_PARSING_STRING

Parse string primitive

enumerator LWJSON_STREAM_STATE_PARSING_PRIMITIVE

Parse any primitive that is non-string, either “true”, “false”, “null” or a number

enumerator LWJSON_STREAM_STATE_EXPECTING_COMMA_OR_END

Expecting ‘,’, ‘}’ or ‘]’

enumerator LWJSON_STREAM_STATE_EXPECTING_COLON

Expecting ‘:’

Functions

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

Setup LwJSON instance for parsing JSON strings.

Parameters
  • lwobj[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 *lwobj, const void *json_data, size_t len)

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

Parameters
  • lwobj[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 *lwobj, const char *json_str)

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

Parameters
  • lwobj[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 *lwobj, const char *path)

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

Parameters
  • lwobj[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 *lwobj, 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
  • lwobj[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 *lwobj)

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

Parameters

lwobj[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 *lwobj)

Prints and outputs full parsed LwJSON instance.

Note

This function is not re-entrant

Parameters

lwobj[in] LwJSON instance to print

lwjsonr_t lwjson_stream_init(lwjson_stream_parser_t *jsp, lwjson_stream_parser_callback_fn evt_fn)

Initialize LwJSON stream object before parsing takes place.

Parameters

jsp[inout] Stream JSON structure

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

lwjsonr_t lwjson_stream_set_user_data(lwjson_stream_parser_t *jsp, void *user_data)

Set user_data in stream parser.

Parameters
  • jsp[inout] LwJSON stream parser

  • user_data[in] user data

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

void *lwjson_stream_get_user_data(lwjson_stream_parser_t *jsp)

Get user_data in stream parser.

Parameters

jsp[in] LwJSON stream parser

Returns

pointer to user data

lwjsonr_t lwjson_stream_reset(lwjson_stream_parser_t *jsp)

Reset LwJSON stream structure.

Parameters

jsp[inout] LwJSON stream parser

Returns

lwjsonOK on success, member of lwjsonr_t otherwise

lwjsonr_t lwjson_stream_parse(lwjson_stream_parser_t *jsp, char c)

Parse JSON string in streaming mode.

Parameters
  • jsp[inout] Stream JSON structure

  • chr[in] Character to parse

Returns

lwjsonSTREAMWAITFIRSTCHAR when stream did not start parsing since no valid start character has been received

Returns

lwjsonSTREAMINPROG if parsing is in progress and no hard error detected

Returns

lwjsonSTREAMDONE when valid JSON was detected and stack level reached back 0 level

Returns

One of enumeration otherwise

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

struct lwjson_stream_stack_t
#include <lwjson.h>

Stream parsing stack object.

Public Members

lwjson_stream_type_t type

Streaming type - current value

char name[LWJSON_CFG_STREAM_KEY_MAX_LEN + 1]

Last known key name, used only for LWJSON_STREAM_TYPE_KEY type

uint16_t index

Current index when type is an array

union lwjson_stream_stack_t::[anonymous] meta

Meta information

struct lwjson_stream_parser_t
#include <lwjson.h>

LwJSON streaming structure.

Public Members

lwjson_stream_stack_t stack[LWJSON_CFG_STREAM_STACK_SIZE]

Stack used for parsing. TODO: Add conditional compilation flag

size_t stack_pos

Current stack position

lwjson_stream_state_t parse_state

Parser state

lwjson_stream_parser_callback_fn evt_fn

Event function for user

void *user_data

User data for callback function

char buff[LWJSON_CFG_STREAM_STRING_MAX_LEN + 1]

Buffer to write temporary data. TODO: Size to be variable with define

Temporary write buffer

size_t buff_pos

Buffer position for next write (length of bytes in buffer)

Buffer position for next write

size_t buff_total_pos

Total buffer position used up to now (in several data chunks)

uint8_t is_last

Status indicates if this is the last part of the string

struct lwjson_stream_parser_t::[anonymous]::[anonymous] str

String structure. It is only used for keys and string objects. Use primitive part for all other options

struct lwjson_stream_parser_t::[anonymous]::[anonymous] prim

Primitive object. Used for all types, except key or string

union lwjson_stream_parser_t::[anonymous] data

Data union used to parse various

char prev_c

History of characters