From 929e2337562c21d17b4ee31b1db90ccf0fa09d8d Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Wed, 1 Feb 2012 16:03:36 +0200 Subject: [PATCH] added tests for primitive types, primitive types now can be stored outside the objects without braces --- jsmn.c | 21 ++++++++++++++------- jsmn_test.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/jsmn.c b/jsmn.c index dd3a33c..22757b2 100644 --- a/jsmn.c +++ b/jsmn.c @@ -42,20 +42,26 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, switch (js[parser->pos]) { case '\t' : case '\r' : case '\n' : case ' ' : case ',' : case ']' : case '}' : - token = jsmn_alloc_token(parser, tokens, num_tokens); - if (token == NULL) - return JSMN_ERROR_NOMEM; - jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); - parser->pos--; - return JSMN_SUCCESS; + goto found; } if (js[parser->pos] < 32 || js[parser->pos] >= 127) { parser->pos = start; return JSMN_ERROR_INVAL; } } + /* TODO: CHECK THIS ONLY WHEN IN JSON STRICT MODE */ +#if 0 parser->pos = start; return JSMN_ERROR_PART; +#endif + +found: + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) + return JSMN_ERROR_NOMEM; + jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); + parser->pos--; + return JSMN_SUCCESS; } /** @@ -112,7 +118,6 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, unsigned int num_tokens) { int r; int i; - jsmntype_t type; jsmntok_t *token; /* initialize the rest of tokens (they could be reallocated) */ @@ -122,6 +127,8 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, for (; js[parser->pos] != '\0'; parser->pos++) { char c; + jsmntype_t type; + c = js[parser->pos]; switch (c) { case '{': case '[': diff --git a/jsmn_test.c b/jsmn_test.c index 7c7e310..6a0b340 100644 --- a/jsmn_test.c +++ b/jsmn_test.c @@ -64,15 +64,66 @@ int test_simple() { } int test_primitive() { + int r; jsmn_parser p; - jsmntok_t tokens[10]; + jsmntok_t tok[10]; const char *js; + + js = "\"boolVar\" : true"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "boolVar")); + check(TOKEN_STIRNG(js, tok[1], "true")); + + js = "\"boolVar\" : false"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "boolVar")); + check(TOKEN_STIRNG(js, tok[1], "false")); + + js = "\"intVar\" : 12345"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "intVar")); + check(TOKEN_STIRNG(js, tok[1], "12345")); + + js = "\"floatVar\" : 12.345"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "floatVar")); + check(TOKEN_STIRNG(js, tok[1], "12.345")); + + js = "\"nullVar\" : null"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "nullVar")); + check(TOKEN_STIRNG(js, tok[1], "null")); + + js = "\"strVar\" : \"hello world\""; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_STRING); + check(TOKEN_STIRNG(js, tok[0], "strVar")); + check(TOKEN_STIRNG(js, tok[1], "hello world")); + return 0; } int main() { test(test_simple, "general test for a simple JSON string"); test(test_primitive, "test primitive JSON data types"); + printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed); return 0; }