Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json-c retuns OK when Invalid json string is passed #503

Closed
roopadanganakal opened this issue Aug 22, 2019 · 1 comment
Closed

json-c retuns OK when Invalid json string is passed #503

roopadanganakal opened this issue Aug 22, 2019 · 1 comment

Comments

@roopadanganakal
Copy link

roopadanganakal commented Aug 22, 2019

JSON String = { "run_time":"10:00", "ps2":0, "ps3":0}} //Extra "}" curly brace at the end.

I have a JSON string something like above with two curly braces }} at the end. This is invalid json string. But I don't see any error when json_tokener_parse or json_tokener_parse_ex is called with correct str and stringlen parameters and JSON_TOKENER_STRICT flag is set. Please help!

When I check "jerr" it does not through any error. How to validate the json string using json-c ?

json = json_tokener_parse(str);
if( json == NULL ) { printf ("Invalid JSON StringError"); }
tok = json_tokener_new();
json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
json = json_tokener_parse_ex(tok, str, stringlen);
jerr = json_tokener_get_error(tok);
if (jerr != json_tokener_success ){
printf("Err Json Is Invalid string \n ");
fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
}

@hawicz
Copy link
Member

hawicz commented Aug 24, 2019

This is working as intended, it parsed a valid object and stopped. You can check tok->char_offset and handle trailing characters as you desire.
See the docs for more details: http://json-c.github.io/json-c/json-c-0.13/doc/html/json__tokener_8h.html#a61679f178111963a9ffa3c8179553f7a

@hawicz hawicz closed this as completed Aug 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants