jsmnx#
Extended version of the simple JSMN JSON parser.
This header provides a few useful additional functions in addition to those provided by jsmn.h. When using jsmnx, you should import this file instead of jsmn.h in your project.
We allow dependencies on the standard library in this file.
For completeness, this file also include prototypes for jsmn_init() and jsmn_parse(), even though they are also declared in jsmn.h.
See also
Defines
-
JSMN_HEADER#
-
JSMN_STRICT#
-
JSMN_PARENT_LINKS#
-
JSMN_CHUNK_SIZE#
Chunck size when reallocating new chunks
Functions
-
void jsmn_init(jsmn_parser *parser)#
Initializes a JSON parser.
-
int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens)#
Parse a JSON data string into and array of tokens, each describing a single JSON object.
Arguments
parser: pointer to initialized parser
js: the JSON input string
len: length of
js
tokens: pointer to a preallocated buffer of JSMN tokens. May be NULL in order to return the number of tokens in the input.
num_tokens: the size of
tokens
Returns
On success, it returns the number of tokens actually used by the parser. If
tokens
is NULL, the number of needed will be returned.On error, one of the following (negative) codes is returned:
JSMN_ERROR_INVAL: bad token, JSON string is corrupted
JSMN_ERROR_NOMEM: not enough tokens, JSON string is too large
JSMN_ERROR_PART: JSON string is too short, expecting more JSON data
-
int jsmn_parse_alloc(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t **tokens_ptr, unsigned int *num_tokens_ptr)#
Like jsmn_parse(), but realloc’s the buffer pointed to by
tokens_ptr
if it is too small.num_tokens_ptr
should point to the number of allocated tokens or be zero iftokens_ptr
is not pre-allocated.Returns JSMN_ERROR_NOMEM on allocation error.
-
int jsmn_required_tokens(const char *js, size_t len)#
Returns number of tokens required to parse JSON string
js
of lengthlen
. On error, JSMN_ERROR_INVAL or JSMN_ERROR_PART is retuned.
-
int jsmn_count(const jsmntok_t *t)#
Returns number of sub-tokens contained in
t
or -1 on error.
-
const jsmntok_t *jsmn_item(const char *js, const jsmntok_t *t, const char *key)#
Returns a pointer to the value of item
key
of JSMN object tokent
.js
is the JSON source.Returns NULL on error.
-
const jsmntok_t *jsmn_element(const char *js, const jsmntok_t *t, int i)#
Returns a pointer to the element
i
of JSMN array tokent
.js
is the JSON source.Returns NULL on error.
-
const char *jsmn_strerror(int r)#
Returns error message corresponding to return value from jsmn_parse().