Skip to content

Commit

Permalink
Drop JSON format for flatbuffers custom commands
Browse files Browse the repository at this point in the history
  To be replaced with protobuf in the future
  • Loading branch information
rbx committed Oct 30, 2023
1 parent eefc1ee commit 7adcaf5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 52 deletions.
41 changes: 5 additions & 36 deletions odc/cc/CustomCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace odc::cc
return typeToFBCmd.at(static_cast<int>(type));
}

string Cmds::Serialize(const Format type) const
string Cmds::Serialize() const
{
flatbuffers::FlatBufferBuilder fbb;
vector<flatbuffers::Offset<FBCommand>> commandOffsets;
Expand Down Expand Up @@ -348,49 +348,18 @@ namespace odc::cc
auto cmds = CreateFBCommands(fbb, commands);
fbb.Finish(cmds);

if (type == Format::Binary)
{
return string(reinterpret_cast<char*>(fbb.GetBufferPointer()), fbb.GetSize());
}
else
{ // Type == Format::JSON
flatbuffers::Parser parser;
if (!parser.Parse(customCommandsFormatDefFbs))
{
throw CommandFormatError("Serialize couldn't parse commands format");
}
std::string json;
if (!flatbuffers::GenerateText(parser, fbb.GetBufferPointer(), &json))
{
throw CommandFormatError("Serialize couldn't serialize parsed data to JSON!");
}
return json;
}
return string(reinterpret_cast<char*>(fbb.GetBufferPointer()), fbb.GetSize());
}

void Cmds::Deserialize(const string& str, const Format type)
void Cmds::Deserialize(const string& str)
{
fCmds.clear();

const flatbuffers::Vector<flatbuffers::Offset<FBCommand>>* cmds = nullptr;

flatbuffers::Parser parser;
if (type == Format::Binary)
{
cmds = GetFBCommands(const_cast<char*>(str.c_str()))->commands();
}
else
{ // Type == Format::JSON
if (!parser.Parse(customCommandsFormatDefFbs))
{
throw CommandFormatError("Deserialize couldn't parse commands format");
}
if (!parser.Parse(str.c_str()))
{
throw CommandFormatError("Deserialize couldn't parse incoming JSON string");
}
cmds = GetFBCommands(parser.builder_.GetBufferPointer())->commands();
}

cmds = GetFBCommands(const_cast<char*>(str.c_str()))->commands();

for (unsigned int i = 0; i < cmds->size(); ++i)
{
Expand Down
4 changes: 2 additions & 2 deletions odc/cc/CustomCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ namespace odc::cc
fCmds.clear();
}

std::string Serialize(const Format type = Format::Binary) const;
void Deserialize(const std::string&, const Format type = Format::Binary);
std::string Serialize() const;
void Deserialize(const std::string&);

private:
container fCmds;
Expand Down
3 changes: 1 addition & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ odc_add_boost_tests(SUITE odc
odc_add_boost_tests(SUITE cc
TESTS
format/construction
format/serialization_binary
format/serialization_json
format/serialization

DEPS ODC::cc

Expand Down
13 changes: 1 addition & 12 deletions tests/cc-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void checkCommands(Cmds& cmds)
BOOST_TEST(count == 15);
}

BOOST_AUTO_TEST_CASE(serialization_binary)
BOOST_AUTO_TEST_CASE(serialization)
{
Cmds outCmds;
fillCommands(outCmds);
Expand All @@ -233,17 +233,6 @@ BOOST_AUTO_TEST_CASE(serialization_binary)
checkCommands(inCmds);
}

BOOST_AUTO_TEST_CASE(serialization_json)
{
Cmds outCmds;
fillCommands(outCmds);
std::string buffer(outCmds.Serialize(Format::JSON));

Cmds inCmds;
inCmds.Deserialize(buffer, Format::JSON);
checkCommands(inCmds);
}

BOOST_AUTO_TEST_SUITE_END()

int main(int argc, char* argv[]) { return boost::unit_test::unit_test_main(init_unit_test, argc, argv); }

0 comments on commit 7adcaf5

Please sign in to comment.