From ab716ee41dc3b0f0c645d9492ea0c2b41bea64b3 Mon Sep 17 00:00:00 2001 From: Adam Oleksy Date: Wed, 26 Apr 2023 07:37:06 +0200 Subject: [PATCH] Make JSON supporting advanced union features (#7869) This change allows user to decode binary with given schema to JSON representation when schema defines union with struct. Co-authored-by: Derek Bailey --- src/idl_parser.cpp | 3 ++- tests/json_test.cpp | 33 +++++++++++++++++++++++++++++++++ tests/json_test.h | 1 + tests/test.cpp | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 084506460..36497eef2 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -2584,7 +2584,8 @@ bool Parser::SupportsAdvancedUnionFeatures() const { return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs | IDLOptions::kPhp | IDLOptions::kJava | IDLOptions::kCSharp | IDLOptions::kKotlin | - IDLOptions::kBinary | IDLOptions::kSwift | IDLOptions::kNim)) == 0; + IDLOptions::kBinary | IDLOptions::kSwift | IDLOptions::kNim | + IDLOptions::kJson)) == 0; } bool Parser::SupportsAdvancedArrayFeatures() const { diff --git a/tests/json_test.cpp b/tests/json_test.cpp index 2224b1a17..e4249f9ec 100644 --- a/tests/json_test.cpp +++ b/tests/json_test.cpp @@ -170,5 +170,38 @@ void JsonUnsortedArrayTest() { TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("ccc")); } +void JsonUnionStructTest() { + // schema to parse data + auto schema = R"( +struct MyStruct { field: int; } +union UnionWithStruct { MyStruct } +table JsonUnionStructTest { union_with_struct: UnionWithStruct; } +root_type JsonUnionStructTest; +)"; + // source text to parse and expected result of generation text back + auto json_source =R"({ + union_with_struct_type: "MyStruct", + union_with_struct: { + field: 12345 + } +} +)"; + + flatbuffers::Parser parser; + // set output language to JSON, so we assure that is supported + parser.opts.lang_to_generate = IDLOptions::kJson; + // parse schema first, so we assure that output language is supported + // and can use it to parse the data after + TEST_EQ(true, parser.Parse(schema)); + TEST_EQ(true, parser.ParseJson(json_source)); + + // now generate text back from the binary, and compare the two: + std::string json_generated; + auto generate_result = + GenerateText(parser, parser.builder_.GetBufferPointer(), &json_generated); + TEST_EQ(true, generate_result); + TEST_EQ_STR(json_source, json_generated.c_str()); +} + } // namespace tests } // namespace flatbuffers diff --git a/tests/json_test.h b/tests/json_test.h index a2aa6fba5..1c8e8093f 100644 --- a/tests/json_test.h +++ b/tests/json_test.h @@ -11,6 +11,7 @@ void JsonEnumsTest(const std::string& tests_data_path); void JsonOptionalTest(const std::string& tests_data_path, bool default_scalars); void ParseIncorrectMonsterJsonTest(const std::string& tests_data_path); void JsonUnsortedArrayTest(); +void JsonUnionStructTest(); } // namespace tests } // namespace flatbuffers diff --git a/tests/test.cpp b/tests/test.cpp index e59245655..6bc23ed6d 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1557,6 +1557,7 @@ int FlatBufferTests(const std::string &tests_data_path) { ParseIncorrectMonsterJsonTest(tests_data_path); FixedLengthArraySpanTest(tests_data_path); DoNotRequireEofTest(tests_data_path); + JsonUnionStructTest(); #else // Guard against -Wunused-parameter. (void)tests_data_path;