- Added possibility to specify the lenght of the input buffer (if not null-terminated).

- Added "extern C" to use it with CPP.
master
Gabriel Gritsch 11 years ago
parent 86350f2bb1
commit 385b42e740

@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include "jsmn.h" #include "jsmn.h"
@ -143,13 +144,18 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
/** /**
* Parse JSON string and fill tokens. * Parse JSON string and fill tokens.
*/ */
jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length, jsmntok_t *tokens,
unsigned int num_tokens) { unsigned int num_tokens) {
jsmnerr_t r; jsmnerr_t r;
int i; int i;
jsmntok_t *token; jsmntok_t *token;
for (; js[parser->pos] != '\0'; parser->pos++) { if (js_length <= 0)
{
js_length = INT_MAX;
}
for (; parser->pos < js_length && js[parser->pos] != '\0'; parser->pos++) {
char c; char c;
jsmntype_t type; jsmntype_t type;

@ -1,6 +1,10 @@
#ifndef __JSMN_H_ #ifndef __JSMN_H_
#define __JSMN_H_ #define __JSMN_H_
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* JSON type identifier. Basic types are: * JSON type identifier. Basic types are:
* o Object * o Object
@ -61,7 +65,11 @@ 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 string into and array of tokens, each describing
* a single JSON object. * a single JSON object.
*/ */
jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, int js_length,
jsmntok_t *tokens, unsigned int num_tokens); jsmntok_t *tokens, unsigned int num_tokens);
#ifdef __cplusplus
}
#endif
#endif /* __JSMN_H_ */ #endif /* __JSMN_H_ */

Loading…
Cancel
Save