diff --git a/jsmn.c b/jsmn.c index bbe5c06..1b918f5 100644 --- a/jsmn.c +++ b/jsmn.c @@ -166,6 +166,9 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, token = &tokens[parser->toknext - 1]; for (;;) { if (token->start != -1 && token->end == -1) { + if (token->type != type) { + return JSMN_ERROR_INVAL; + } token->end = parser->pos + 1; parser->toksuper = token->parent; break; diff --git a/jsmn_test.c b/jsmn_test.c index 4c9593f..308faf6 100644 --- a/jsmn_test.c +++ b/jsmn_test.c @@ -318,6 +318,36 @@ int test_array_nomem() { return 0; } +int test_objects_arrays() { + int i; + int r; + jsmn_parser p; + jsmntok_t tokens[10]; + const char *js; + + js = "[10}"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tokens, 10); + check(r == JSMN_ERROR_INVAL); + + js = "[10]"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tokens, 10); + check(r == JSMN_SUCCESS); + + js = "{\"a\": 1]"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tokens, 10); + check(r == JSMN_ERROR_INVAL); + + js = "{\"a\": 1}"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tokens, 10); + check(r == JSMN_SUCCESS); + + return 0; +} + int main() { test(test_empty, "general test for a empty JSON objects/arrays"); test(test_simple, "general test for a simple JSON string"); @@ -327,6 +357,7 @@ int main() { test(test_partial_array, "test partial array reading"); test(test_array_nomem, "test array reading with a smaller number of tokens"); test(test_unquoted_keys, "test unquoted keys (like in JavaScript)"); + test(test_objects_arrays, "test objects and arrays"); printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed); return 0; }