changed API: parse now is more flexible, but init jsut resets the parser; added new test macro to compate strings, fixed Makefile

master
Serge A. Zaitsev 13 years ago
parent 4b5c5ed66a
commit bed0a7a3e6

@ -3,9 +3,6 @@
all: libjsmn.a all: libjsmn.a
#demo: libjsmn.a demo.o
# $(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@
libjsmn.a: jsmn.o libjsmn.a: jsmn.o
$(AR) rc $@ $^ $(AR) rc $@ $^
@ -19,9 +16,9 @@ jsmn_test: jsmn_test.o
$(CC) -L. -ljsmn $< -o $@ $(CC) -L. -ljsmn $< -o $@
clean: clean:
rm -f jsmn.o demo.o rm -f jsmn.o jsmn_test.o
rm -f jsmn_test
rm -f libjsmn.a rm -f libjsmn.a
rm -f demo
.PHONY: all clean test demo .PHONY: all clean test

@ -28,26 +28,6 @@ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
token->end = end; token->end = end;
} }
/**
* Creates a new parser based over a given buffer with an array of tokens
* available.
*/
void jsmn_init_parser(jsmn_parser *parser, const char *js,
jsmntok_t *tokens, unsigned int num_tokens) {
unsigned int i;
parser->js = js;
parser->pos = 0;
parser->tokens = tokens;
parser->num_tokens = num_tokens;
parser->curtoken = 0;
parser->cursize = NULL;
for (i = 0; i < parser->num_tokens; i++) {
jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
}
}
/** /**
* Fills next available token with JSON primitive. * Fills next available token with JSON primitive.
*/ */
@ -132,14 +112,20 @@ static int jsmn_parse_string(jsmn_parser *parser) {
/** /**
* Parse JSON string and fill tokens. * Parse JSON string and fill tokens.
*/ */
jsmnerr_t jsmn_parse(jsmn_parser *parser) { jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens,
unsigned int num_tokens) {
int r; int r;
int i; int i;
const char *js;
jsmntype_t type; jsmntype_t type;
jsmntok_t *token; jsmntok_t *token;
js = parser->js; /* initialize the rest of tokens (they could be reallocated) */
parser->num_tokens = num_tokens;
parser->tokens = tokens;
parser->js = js;
for (i = parser->curtoken; i < parser->num_tokens; i++) {
jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
}
for (; js[parser->pos] != '\0'; parser->pos++) { for (; js[parser->pos] != '\0'; parser->pos++) {
char c; char c;
@ -199,3 +185,13 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) {
return JSMN_SUCCESS; return JSMN_SUCCESS;
} }
/**
* Creates a new parser based over a given buffer with an array of tokens
* available.
*/
void jsmn_init(jsmn_parser *parser) {
parser->pos = 0;
parser->curtoken = 0;
parser->cursize = NULL;
}

@ -16,9 +16,13 @@ typedef enum {
} jsmntype_t; } jsmntype_t;
typedef enum { typedef enum {
/* Not enough tokens were provided */
JSMN_ERROR_NOMEM = -1, JSMN_ERROR_NOMEM = -1,
/* Invalid character inside JSON string */
JSMN_ERROR_INVAL = -2, JSMN_ERROR_INVAL = -2,
/* The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_PART = -3, JSMN_ERROR_PART = -3,
/* Everything was fine */
JSMN_SUCCESS = 0 JSMN_SUCCESS = 0
} jsmnerr_t; } jsmnerr_t;
@ -51,13 +55,13 @@ typedef struct {
/** /**
* Create JSON parser over an array of tokens * Create JSON parser over an array of tokens
*/ */
void jsmn_init_parser(jsmn_parser *parser, const char *js, void jsmn_init(jsmn_parser *parser);
jsmntok_t *tokens, unsigned int num_tokens);
/** /**
* 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); jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js,
jsmntok_t *tokens, unsigned int num_tokens);
#endif /* __JSMN_H_ */ #endif /* __JSMN_H_ */

@ -1,4 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsmn.c" #include "jsmn.c"
@ -30,28 +32,45 @@ static void test(int (*func)(void), const char *name) {
&& (t).end == tok_end \ && (t).end == tok_end \
&& (t).type == (tok_type)) && (t).type == (tok_type))
#define TOKEN_STIRNG(js, t, s) \
(strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
&& strlen(s) == (t).end - (t).start)
#define TOKEN_PRINT(t) \ #define TOKEN_PRINT(t) \
printf("start: %d, end: %d, type: %d\n", (t).start, (t).end, (t).type) printf("start: %d, end: %d, type: %d\n", (t).start, (t).end, (t).type)
int test_primitive() { int test_simple() {
const char *js;
int r; int r;
jsmn_parser p; jsmn_parser p;
jsmntok_t tokens[10]; jsmntok_t tokens[10];
jsmn_init_parser(&p, "{\"a\": 0}", tokens, 10); js = "{\"a\": 0}";
r = jsmn_parse(&p); jsmn_init(&p);
r = jsmn_parse(&p, js, tokens, 10);
check(r == JSMN_SUCCESS); check(r == JSMN_SUCCESS);
check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT)); check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING)); check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE)); check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));
check(TOKEN_STIRNG(js, tokens[0], js));
check(TOKEN_STIRNG(js, tokens[1], "a"));
check(TOKEN_STIRNG(js, tokens[2], "0"));
return 0; return 0;
} }
int test_primitive() {
jsmn_parser p;
jsmntok_t tokens[10];
const char *js;
}
int main() { int main() {
test(test_primitive, "test primitive values"); test(test_simple, "general test for a simple JSON string");
return 0; return 0;
} }

Loading…
Cancel
Save