From 13d0594b4c8cd6677ddb43ecdbd2d7d3300b6208 Mon Sep 17 00:00:00 2001 From: Nalinichandra Penke Date: Tue, 22 Dec 2015 00:02:19 -0800 Subject: [PATCH 1/2] Fix #2775: Add parser option to skip unknown JSON fields --- include/flatbuffers/idl.h | 7 ++- src/idl_parser.cpp | 103 ++++++++++++++++++++++++++++++++------ tests/test.cpp | 21 ++++++++ 3 files changed, 116 insertions(+), 15 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 118a74dd826..134c46f5a40 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -320,6 +320,7 @@ struct IDLOptions { bool one_file; bool proto_mode; bool generate_all; + bool skip_unexpected_fields_in_json; // Possible options for the more general generator below. enum Language { kJava, kCSharp, kGo, kMAX }; @@ -337,6 +338,7 @@ struct IDLOptions { one_file(false), proto_mode(false), generate_all(false), + skip_unexpected_fields_in_json(false), lang(IDLOptions::kJava) {} }; @@ -477,7 +479,10 @@ class Parser { CHECKED_ERROR ParseProtoDecl(); CHECKED_ERROR ParseProtoCurliesOrIdent(); CHECKED_ERROR ParseTypeFromProtoType(Type *type); - + CHECKED_ERROR SkipAnyJsonValue(); + CHECKED_ERROR SkipJsonObject(); + CHECKED_ERROR SkipJsonArray(); + CHECKED_ERROR SkipJsonString(); CHECKED_ERROR DoParse(const char *_source, const char **include_paths, const char *source_filename); diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 6a013c90f27..d84ffddd60c 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -673,24 +673,33 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value, EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier); } auto field = struct_def.fields.Lookup(name); - if (!field) return Error("unknown field: " + name); - EXPECT(':'); - Value val = field->value; - ECHECK(ParseAnyValue(val, field, fieldn)); - size_t i = field_stack_.size(); - // Hardcoded insertion-sort with error-check. - // If fields are specified in order, then this loop exits immediately. - for (; i > field_stack_.size() - fieldn; i--) { - auto existing_field = field_stack_[i - 1].second; - if (existing_field == field) - return Error("field set more than once: " + field->name); - if (existing_field->value.offset < field->value.offset) break; + if (!field) { + if (!opts.skip_unexpected_fields_in_json) { + return Error("unknown field: " + name); + } else { + EXPECT(':'); + ECHECK(SkipAnyJsonValue()); + } + } else { + EXPECT(':'); + Value val = field->value; + ECHECK(ParseAnyValue(val, field, fieldn)); + size_t i = field_stack_.size(); + // Hardcoded insertion-sort with error-check. + // If fields are specified in order, then this loop exits immediately. + for (; i > field_stack_.size() - fieldn; i--) { + auto existing_field = field_stack_[i - 1].second; + if (existing_field == field) + return Error("field set more than once: " + field->name); + if (existing_field->value.offset < field->value.offset) break; + } + field_stack_.insert(field_stack_.begin() + i, std::make_pair(val, field)); + fieldn++; } - field_stack_.insert(field_stack_.begin() + i, std::make_pair(val, field)); - fieldn++; if (Is('}')) { NEXT(); break; } EXPECT(','); } + if (struct_def.fixed && fieldn != struct_def.fields.vec.size()) return Error("struct: wrong number of initializers: " + struct_def.name); @@ -1480,6 +1489,72 @@ CheckedError Parser::ParseTypeFromProtoType(Type *type) { return NoError(); } +CheckedError Parser::SkipAnyJsonValue() { + switch (token_) { + case '{': + ECHECK(SkipJsonObject()); + break; + case kTokenStringConstant: + ECHECK(SkipJsonString()); + break; + case '[': + ECHECK(SkipJsonArray()); + break; + case kTokenIntegerConstant: + EXPECT(kTokenIntegerConstant); + break; + case kTokenFloatConstant: + EXPECT(kTokenFloatConstant); + break; + default: + return Error(std::string("Unexpected token:") + std::string(1, token_)); + } + return NoError(); +} + +CheckedError Parser::SkipJsonObject() { + EXPECT('{'); + size_t fieldn = 0; + + while (true) { + if ((!opts.strict_json || !fieldn) && Is('}')) break; + + if (!Is(kTokenStringConstant)) + EXPECT(opts.strict_json ? kTokenStringConstant : kTokenIdentifier); + + EXPECT(':'); + ECHECK(SkipAnyJsonValue()); + fieldn++; + + if (Is('}')) break; + EXPECT(','); + } + + NEXT(); + return NoError(); +} + +CheckedError Parser::SkipJsonArray() { + EXPECT('['); + + while (true) { + if (Is(']')) break; + + ECHECK(SkipAnyJsonValue()); + + if (Is(']')) break; + EXPECT(','); + } + + NEXT(); + return NoError(); +} + +CheckedError Parser::SkipJsonString() { + EXPECT(kTokenStringConstant); + return NoError(); +} + bool Parser::Parse(const char *source, const char **include_paths, const char *source_filename) { return !DoParse(source, include_paths, source_filename).Check(); diff --git a/tests/test.cpp b/tests/test.cpp index d009438ea78..83cdaef94a5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -813,6 +813,26 @@ void UnicodeTest() { "\\u5225\\u30B5\\u30A4\\u30C8\\x01\\x80\"}", true); } +void UnknownFieldsTest() { + flatbuffers::IDLOptions opts; + opts.skip_unexpected_fields_in_json = true; + flatbuffers::Parser parser(opts); + + TEST_EQ(parser.Parse("table T { str:string; i:int;}" + "root_type T;" + "{ str:\"test\"," + "unknown_int:10," + "unknown_float:1.0," + "unknown_array: [ 1, 2, 3, 4]," + "unknown_object: { i: 10 }," + "i:10}"), true); + + std::string jsongen; + parser.opts.indent_step = -1; + GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + TEST_EQ(jsongen == "{str: \"test\",i: 10}", true); +} + int main(int /*argc*/, const char * /*argv*/[]) { // Run our various test suites: @@ -837,6 +857,7 @@ int main(int /*argc*/, const char * /*argv*/[]) { ScientificTest(); EnumStringsTest(); UnicodeTest(); + UnknownFieldsTest(); if (!testing_fails) { TEST_OUTPUT_LINE("ALL TESTS PASSED"); From f243109524eb7a62cd2556e2ffc7e70920427973 Mon Sep 17 00:00:00 2001 From: Nalinichandra Penke Date: Tue, 22 Dec 2015 00:13:22 -0800 Subject: [PATCH 2/2] Fix #2775: Add option to flatc to skip unknown fields in JSON --- src/flatc.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index ead3a7c2973..48d98ce41ae 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -100,12 +100,15 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " no trailing commas in tables/vectors.\n" " --defaults-json Output fields whose value is the default when\n" " writing JSON\n" + " --unknown-json Allow fields in JSON that are not defined in the\n" + " schema. These fields will be discared when generating\n" + " binaries.\n" " --no-prefix Don\'t prefix enum values with the enum type in C++.\n" " --scoped-enums Use C++11 style scoped and strongly typed enums.\n" " also implies --no-prefix.\n" " --gen-includes (deprecated), this is the default behavior.\n" " If the original behavior is required (no include\n" - " statements) use --no-includes.\n" + " statements) use --no-includes.\n" " --no-includes Don\'t generate include statements for included\n" " schemas the generated file depends on (C++).\n" " --gen-mutable Generate accessors that can mutate buffers in-place.\n" @@ -155,6 +158,8 @@ int main(int argc, const char *argv[]) { opts.skip_js_exports = true; } else if(arg == "--defaults-json") { opts.output_default_scalars_in_json = true; + } else if (arg == "--unknown-json") { + opts.skip_unexpected_fields_in_json = true; } else if(arg == "--no-prefix") { opts.prefixed_enums = false; } else if(arg == "--scoped-enums") {