Added option to not requires an EoF token when parsing JSON (#7620)

Previously when parsing a JSON representation of a Flatbuffer, the
parser required that the input string contain one and only one root
table. This change adds a flag that removes that requirement, so that
if a Flatbuffer table is embedded in some larger string the parser will
simply stop parsing once it reaches the end of the root table, and does
not validate that it has reached the end of the string.

This change also adds a BytesConsumed function, which returns the number
of bytes the parser consumed. This is useful if the table embedded in
some larger string that is being parsed, and that outer parser needs to
know how many bytes the table was so that it can step over it.
This commit is contained in:
Alex Ames
2022-11-03 08:57:46 -07:00
committed by GitHub
parent 15f32c6907
commit a4ff275d9b
3 changed files with 58 additions and 3 deletions

View File

@@ -3264,6 +3264,10 @@ bool Parser::ParseJson(const char *json, const char *json_filename) {
return done;
}
std::ptrdiff_t Parser::BytesConsumed() const {
return std::distance(source_, cursor_);
}
CheckedError Parser::StartParseFile(const char *source,
const char *source_filename) {
file_being_parsed_ = source_filename ? source_filename : "";
@@ -3601,9 +3605,11 @@ CheckedError Parser::DoParseJson() {
: nullptr);
}
}
// Check that JSON file doesn't contain more objects or IDL directives.
// Comments after JSON are allowed.
EXPECT(kTokenEof);
if (opts.require_json_eof) {
// Check that JSON file doesn't contain more objects or IDL directives.
// Comments after JSON are allowed.
EXPECT(kTokenEof);
}
return NoError();
}