Skip to content

Parsing problem with new reader #511

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

Closed
cmaeckel opened this issue Aug 6, 2016 · 4 comments
Closed

Parsing problem with new reader #511

cmaeckel opened this issue Aug 6, 2016 · 4 comments

Comments

@cmaeckel
Copy link

cmaeckel commented Aug 6, 2016

The parser is accepting "{}foobar" as valid JSON. The code fragment is:

Json::CharReaderBuilder reader;
reader.strictMode( &reader.settings_ );
std::istringstream chrStream( "{}foobar" );
Json::Value root;
std::string errs;
bool result( Json::parseFromStream( reader, chrStream, &root, &errs ) );
std::cout << "Result " << result << " errs " << errs << std::endl;

And result comes back as true in this case and writing out the resulting JSON returns "{}". Or am I doing something wrong?

@cdunn2001
Copy link
Contributor

Well, we could add a setting to strict-mode. How about noTrailing? If there are any unconsumed characters (including whitespace), then we fail. We would accept leading whitespace because that's harder to test.

@cmaeckel
Copy link
Author

cmaeckel commented Aug 6, 2016

For my purposes leading or trailing white space is neither good or bad. So a noTrailing option would work great for me.

@cmaeckel
Copy link
Author

cmaeckel commented Aug 8, 2016

This changes works well for me since I don't mind trailing white space.

--- /Before/json_reader.cpp 2016-08-08 16:12:28.000 -0500
+++ /After/json_reader.cpp  2016-08-08 16:13:32.000 -0500
@@ -1068,7 +1068,7 @@
   Token token;
   skipCommentTokens(token);
   if (features_.failIfExtra_) {
-    if (token.type_ != tokenError && token.type_ != tokenEndOfStream) {
+    if ((features_.strictRoot_ || token.type_ != tokenError) && token.type_ != tokenEndOfStream) {
       addError("Extra non-whitespace after JSON value.", token);
       return false;
     }

@cdunn2001
Copy link
Contributor

Ok. I doubt this will break anyone as you've written it, but if it does, then we will have to use a new setting for this.

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