JSON Utilities. More...

Modules

Types
JSON data types.
 
Macros
JSON macros.
 

Functions

gos_result_t gos_json_parse_context_init (gos_json_parse_context_t **context_ptr, const gos_json_parse_config_t *config)
 Initialize JSON parsing context. More...
 
gos_result_t gos_json_parse_context_deinit (gos_json_parse_context_t *context)
 Cleanup JSON parser context. More...
 
gos_result_t gos_json_parse_chunked (gos_json_parse_context_t *context, void *user)
 Parse JSON formatted data in chunks. More...
 
gos_json_tok_tgos_json_context_get_token (gos_json_parse_context_t *context, const char *str_val, const gos_json_tok_t *offset_tok)
 Retrieve a token from the JSON context. More...
 
gos_json_tok_tgos_json_context_get_value (gos_json_parse_context_t *context, const char *str_key, const gos_json_tok_t *offset_tok)
 Return the value token for a specified key. More...
 
gos_json_tok_tgos_json_context_get_value_with_limit (gos_json_parse_context_t *context, const char *str_key, const gos_json_tok_t *offset_tok, uint32_t limit)
 This does the exact same as gos_json_context_get_value() but limits the number of tokens to search.
 
gos_json_tok_tgos_json_context_skip_tokens (gos_json_parse_context_t *context, const gos_json_tok_t *current_tok, uint16_t count)
 Skip past specified number of tokens in context. More...
 
gos_json_tok_tgos_json_context_get_current_parent (gos_json_parse_context_t *context)
 Get the current parent Object or Array token. More...
 
void gos_json_encode_buffer (gos_buffer_t *fragment)
 Delimit any JSON special characters in supplied buffer. More...
 
gos_result_t gos_json_decode_buffer (gos_buffer_t *fragment)
 Decoding delimited JSON data to original representation. More...
 

Detailed Description

JSON Utilities.

See JSON Website for more information.

Note
MessagePack is very similar to JSON but much more efficient for embedded devices.

Function Documentation

◆ gos_json_context_get_current_parent()

gos_json_tok_t* gos_json_context_get_current_parent ( gos_json_parse_context_t context)

Get the current parent Object or Array token.

This returns the current parent Object or Array token.

Parameters
contextPointer to initialized context
Returns
pointer to parent token, NULL else

◆ gos_json_context_get_token()

gos_json_tok_t* gos_json_context_get_token ( gos_json_parse_context_t context,
const char *  str_val,
const gos_json_tok_t offset_tok 
)

Retrieve a token from the JSON context.

After calling gos_json_parse_chunked(), the gos_json_parse_context_t may have one or more tokens. This retrieves a given token from the context.

If the 'str_val' parameter is specified then this finds the first string token with the given value. If the 'offset_tok' parameter is specified then it starts searching from 'offset_tok' token onward. If both parameters are NULL then return the head token. If the token isn't found or there are no tokens in the context then return NULL.

Note
This returns a 'token'. It has no concept of key/value. Refer to gos_json_context_get_value() if the value of a given key is required.
Parameters
contextInitialized JSON context
str_valOptional string value to search for, leave NULL if unused
offset_tokStarting token to begin search, leave NULL if unused
Returns
pointer to found token, NULL else

◆ gos_json_context_get_value()

gos_json_tok_t* gos_json_context_get_value ( gos_json_parse_context_t context,
const char *  str_key,
const gos_json_tok_t offset_tok 
)

Return the value token for a specified key.

After calling json_parse_chunked(), the gos_json_parse_context_t may have one or more tokens. This returns a corresponding value token for the specified key.

This searches linearly starting at the first token in the context (or 'offset_tok' if supplied) for the first GOS_JSON_TYPE_STRING token with its value equal to 'str_key'. It then returns the key token's value (i.e. the token following the found key token).

If the key token is not found or the context is empty then NULL is returned.

Note
'offset_tok' must be either a GOS_JSON_TYPE_OBJECT or GOS_JSON_TYPE_ARRAY. If it's any other type, the API will automatically begin searching for a GOS_JSON_TYPE_OBJECT or GOS_JSON_TYPE_ARRAY starting at offset_tok.
Parameters
contextInitialized JSON context
str_keyString key value to search
offset_tokStarting token to begin search, leave NULL if unused
Returns
pointer to value of found key token, NULL else

◆ gos_json_context_skip_tokens()

gos_json_tok_t* gos_json_context_skip_tokens ( gos_json_parse_context_t context,
const gos_json_tok_t current_tok,
uint16_t  count 
)

Skip past specified number of tokens in context.

After calling json_parse_chunked(), the gos_json_parse_context_t may have one or more tokens. This is a helper API for skipping past a specified number of tokens.

If current_tok is given as an argument, then this will return current_tok + count tokens. If current_tok is NOT given, then this will return the first token in the context + count tokens. If the count token is not available then this returns NULL.

Parameters
contextInitialized JSON context
current_tokOptional token to begin from, begin from first token in context otherwise
countNumber of tokens to skip
Returns
pointer to first token after skipped tokens, NULL if not available

◆ gos_json_decode_buffer()

gos_result_t gos_json_decode_buffer ( gos_buffer_t fragment)

Decoding delimited JSON data to original representation.

The decoding is done in-place. The gos_buffer_t will be updated to reflect the new data size.

Parameters
fragmentBuffer contain data to JSON decode
Returns
result of decoding

◆ gos_json_encode_buffer()

void gos_json_encode_buffer ( gos_buffer_t fragment)

Delimit any JSON special characters in supplied buffer.

This backslash delimits any special JSON characters in the supplied buffer data in-place. The given buffer must be larger enough to hold the additional backslash characters (if necessary). The size of the gos_buffer_t will be updated to reflect the new data size.

Parameters
fragmentBuffer hold data to JSON encode, buffer must be large enough to hold additional backslash characters

◆ gos_json_parse_chunked()

gos_result_t gos_json_parse_chunked ( gos_json_parse_context_t context,
void *  user 
)

Parse JSON formatted data in chunks.

This will parse JSON data and optionally allocate tokens. This may be called multiple times for a given set of JSON data.

Call json_parse_context_init() first to initialize a parsing context.

After parsing call json_context_get_token() to retrieve a token.

Parameters
contextPreviously initialize JSON context
userOptional user data to be passed to gos_json_parse_reader_t and gos_json_token_callback_t
Returns
gos_result_t result of api call

◆ gos_json_parse_context_deinit()

gos_result_t gos_json_parse_context_deinit ( gos_json_parse_context_t context)

Cleanup JSON parser context.

This cleans up the allocated parsing context. This also de-allocates any allocated tokens from parsing.

Parameters
contextJSON parsing context to de-initialize
Returns
gos_result_t result of api call

◆ gos_json_parse_context_init()

gos_result_t gos_json_parse_context_init ( gos_json_parse_context_t **  context_ptr,
const gos_json_parse_config_t config 
)

Initialize JSON parsing context.

This allocates a JSON parsing context configured with the given configuration.

Call json_parse_context_deinit() to cleanup this context.

Parameters
context_ptrPointer to hold pointer to allocated context
configContext configuration
Returns
gos_result_t result of api call