diff --git a/jsmn.c b/jsmn.c index 0bf92ff..729d5a2 100644 --- a/jsmn.c +++ b/jsmn.c @@ -1,5 +1,6 @@ #include -#include +#include // for SIZE_MAX +//#include // for UINT_MAX #include "jsmn.h" @@ -141,19 +142,24 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, return JSMN_ERROR_PART; } +static inline jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, unsigned int num_tokens) +{ + return jsmn_parseV2(parser, js, SIZE_MAX /*UINT_MAX*/, tokens, num_tokens); +} + /** * Parse JSON string and fill tokens. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, jsmntok_t *tokens, +jsmnerr_t jsmn_parseV2(jsmn_parser *parser, const char *js, size_t js_length, jsmntok_t *tokens, unsigned int num_tokens) { + if (js==NULL || js_length==0) + { + return JSMN_ERROR_PART; + } + jsmnerr_t r; int i; jsmntok_t *token; - - if (js_length <= 0) - { - js_length = INT_MAX; - } for (; parser->pos < js_length && js[parser->pos] != '\0'; parser->pos++) { char c; diff --git a/jsmn.h b/jsmn.h index ecb4752..8f10cf9 100644 --- a/jsmn.h +++ b/jsmn.h @@ -62,10 +62,17 @@ typedef struct { void jsmn_init(jsmn_parser *parser); /** - * Run JSON parser. It parses a JSON data string into and array of tokens, each describing + * Run JSON parser. It parses a JSON data null-terminated-string into and array of tokens, each describing * a single JSON object. */ -jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, +static inline jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, + jsmntok_t *tokens, unsigned int num_tokens); + +/** + * Run JSON parser. It parses a JSON data buffer with specified length into and array of tokens, each describing + * a single JSON object. + */ +jsmnerr_t jsmn_parseV2(jsmn_parser *parser, const char *js, size_t js_length, jsmntok_t *tokens, unsigned int num_tokens); #ifdef __cplusplus