#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "monster_generated.h"
using namespace MyGame::Sample;
int main(int , const char * []) {
std::string schema_file;
std::string json_file;
std::string bfbs_file;
bool ok =
flatbuffers::LoadFile("tests/monster_test.fbs", false, &schema_file) &&
flatbuffers::LoadFile("tests/monsterdata_test.golden", false,
&json_file) &&
flatbuffers::LoadFile("tests/monster_test.bfbs", true, &bfbs_file);
if (!ok) {
printf("couldn't load files!\n");
return 1;
}
const char *include_directories[] = { "samples", "tests",
"tests/include_test", nullptr };
flatbuffers::Parser parser1;
ok = parser1.Parse(schema_file.c_str(), include_directories);
assert(ok);
flatbuffers::Parser parser2;
ok = parser2.Deserialize((uint8_t *)bfbs_file.c_str(), bfbs_file.length());
assert(ok);
ok = parser1.Parse(json_file.c_str(), include_directories);
assert(ok);
ok = parser2.Parse(json_file.c_str(), include_directories);
assert(ok);
std::string jsongen1;
if (!GenerateText(parser1, parser1.builder_.GetBufferPointer(), &jsongen1)) {
printf("Couldn't serialize parsed data to JSON!\n");
return 1;
}
std::string jsongen2;
if (!GenerateText(parser2, parser2.builder_.GetBufferPointer(), &jsongen2)) {
printf("Couldn't serialize parsed data to JSON!\n");
return 1;
}
if (jsongen1 != jsongen2) {
printf("%s----------------\n%s", jsongen1.c_str(), jsongen2.c_str());
}
printf("The FlatBuffer has been parsed from JSON successfully.\n");
}