zserge / jsmn

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
MIT License
3.65k stars 778 forks source link

Multiple root elements #49

Open zserge opened 8 years ago

zserge commented 8 years ago

Wouldn't it make sense to return an error if multiple root elements are found and JSMN_STRICT is defined? e.g. "{}{}" is not valid JSON and jsmn could/should return an error. This code would do the trick:

                        }
                        token->end = parser->pos + 1;
                        parser->toksuper = token->parent;
+                       if (token->parent == -1) {
+#ifdef JSMN_STRICT
+                           // ensure that no junk data is present
+                           for (; js[parser->pos+1] != '\0'; parser->pos++) {
+                               char c;
+                               c = js[parser->pos+1];
+                               if (!isspace(c)) {
+                                   return JSMN_ERROR_JUNK;
+                               }
+                           }
+#endif
+                           break;
+                       }
                        break;
                    }
                    if (token->parent == -1) {

(Note: I hesitate to create a pull request, as I never used HG as well as Bitbucket so far. But if you're willing to merge this and you want to have a pull request, I'll create one of course)