From bed0a7a3e647abb7204d73c69f9f44b37bdfc84f Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Wed, 1 Feb 2012 13:56:06 +0200 Subject: [PATCH] changed API: parse now is more flexible, but init jsut resets the parser; added new test macro to compate strings, fixed Makefile --- Makefile | 9 +++------ jsmn.c | 42 +++++++++++++++++++----------------------- jsmn.h | 10 +++++++--- jsmn_test.c | 27 +++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index bb9337a..f897581 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,6 @@ all: libjsmn.a -#demo: libjsmn.a demo.o -# $(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@ - libjsmn.a: jsmn.o $(AR) rc $@ $^ @@ -19,9 +16,9 @@ jsmn_test: jsmn_test.o $(CC) -L. -ljsmn $< -o $@ clean: - rm -f jsmn.o demo.o + rm -f jsmn.o jsmn_test.o + rm -f jsmn_test rm -f libjsmn.a - rm -f demo -.PHONY: all clean test demo +.PHONY: all clean test diff --git a/jsmn.c b/jsmn.c index 04404a7..5554c8f 100644 --- a/jsmn.c +++ b/jsmn.c @@ -28,26 +28,6 @@ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, 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. */ @@ -132,14 +112,20 @@ static int jsmn_parse_string(jsmn_parser *parser) { /** * 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 i; - const char *js; jsmntype_t type; 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++) { char c; @@ -199,3 +185,13 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) { 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; +} + diff --git a/jsmn.h b/jsmn.h index 8b21cab..57112fa 100644 --- a/jsmn.h +++ b/jsmn.h @@ -16,9 +16,13 @@ typedef enum { } jsmntype_t; typedef enum { + /* Not enough tokens were provided */ JSMN_ERROR_NOMEM = -1, + /* Invalid character inside JSON string */ JSMN_ERROR_INVAL = -2, + /* The string is not a full JSON packet, more bytes expected */ JSMN_ERROR_PART = -3, + /* Everything was fine */ JSMN_SUCCESS = 0 } jsmnerr_t; @@ -51,13 +55,13 @@ typedef struct { /** * Create JSON parser over an array of tokens */ -void jsmn_init_parser(jsmn_parser *parser, const char *js, - jsmntok_t *tokens, unsigned int num_tokens); +void jsmn_init(jsmn_parser *parser); /** * Run JSON parser. It parses a JSON data string into and array of tokens, each describing * 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_ */ diff --git a/jsmn_test.c b/jsmn_test.c index a6e0429..0f70673 100644 --- a/jsmn_test.c +++ b/jsmn_test.c @@ -1,4 +1,6 @@ #include +#include +#include #include "jsmn.c" @@ -30,28 +32,45 @@ static void test(int (*func)(void), const char *name) { && (t).end == tok_end \ && (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) \ 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; jsmn_parser p; 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(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT)); check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING)); 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; } +int test_primitive() { + jsmn_parser p; + jsmntok_t tokens[10]; + const char *js; +} + int main() { - test(test_primitive, "test primitive values"); + test(test_simple, "general test for a simple JSON string"); return 0; } +