在通过扁平缓冲区将JSon解析为结构之前,我正在尝试设置一个新的自定义根。
对应的FSB已经有了一个root_type,我只想覆盖它一次,以便能够将它解析成一个结构。
SetRootType("NonRootStructInFbsT")失败
API的文档说,这可以用来覆盖当前的root,这正是我想要做的。
std::string schemaText;
std::string schemaFile("MySchema.fbs");
if(not flatbuffers::FileExists(schemaFile.c_str())) {
error("Schema file inaccessible: ", schemaFile);
return nullptr;
}
if(not flatbuffers::LoadFile(schemaFile.c_str(), false, &schemaText)) {
error(TAG, "Failed to load schema file: ", schemaFile);
return nullptr;
}
info("Read schema file: ", schemaText.size(), schemaText);
flatbuffers::Parser parser;
if(not parser.SetRootType("NonRootStructInFbsT")) {
error("Unable to set root type: ", customRoot);
return nullptr;
}
info("Set the root type: ", customRoot);我总是收到错误消息Unable to set root type: NonRootStructInFbsT
发布于 2019-09-11 22:17:51
FlatBuffer的根只能是table,因此root_type和SetRootType将拒绝任何其他名称,如struct或union。
此外,名称以T结尾这一事实似乎是指"object API“类型。这些都是纯粹在生成的代码中的名称,您需要提供它们在模式中的名称。
发布于 2021-10-29 07:08:24
发布的代码缺少对具有架构内容的flatbuffers::Parser::Parse的调用。你的代码:
std::string schemaText;
std::string schemaFile("MySchema.fbs");
if(not flatbuffers::FileExists(schemaFile.c_str())) {
error("Schema file inaccessible: ", schemaFile);
return nullptr;
}
if(not flatbuffers::LoadFile(schemaFile.c_str(), false, &schemaText)) {
error(TAG, "Failed to load schema file: ", schemaFile);
return nullptr;
}
info("Read schema file: ", schemaText.size(), schemaText);
flatbuffers::Parser parser;一切都没问题,但是这里我们有schemaText模式文件的内容和带有默认选项的空parser。设置根类型还为时过早。我们应该用这样的方式将模式读取到parser:
if(not parser.Parse(schemaText)) {
error(TAG, "Defective schema: ", parser.error_);
return nullptr;
}在此之后,如果我们到达此处,我们有带模式的parser,因此在该模式中,我们还可以选择根类型:
if(not parser.SetRootType("NonRootStructInFbsT")) {
error("Unable to set root type: ", customRoot);
return nullptr;
}
info("Set the root type: ", customRoot);请注意,parser.error_提供了有关您在使用parser期间遇到的任何错误的详细信息。
https://stackoverflow.com/questions/57883644
复制相似问题