added tests for primitive types, primitive types now can be stored outside the objects without braces

master
Serge A. Zaitsev 13 years ago
parent 443be365f6
commit 929e233756

@ -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 '[':

@ -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;
}

Loading…
Cancel
Save