mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-08 06:05:17 +00:00
Json : Add --size-prefixed option to flatc (#4645)
to be able to convert to json size prefixed buffers.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
cc158e7009
commit
e78825e7a0
@@ -100,6 +100,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
|
||||
" (default is \"github.com/google/flatbuffers/go\")\n"
|
||||
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||
" This may crash flatc given a mismatched schema.\n"
|
||||
" --size-prefixed Input binaries are size prefixed buffers.\n"
|
||||
" --proto Input is a .proto, translate to .fbs.\n"
|
||||
" --oneof-union Translate .proto oneofs to flatbuffer unions.\n"
|
||||
" --grpc Generate GRPC interfaces for the specified languages\n"
|
||||
@@ -233,6 +234,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
||||
opts.one_file = true;
|
||||
} else if (arg == "--raw-binary") {
|
||||
raw_binary = true;
|
||||
} else if (arg == "--size-prefixed") {
|
||||
opts.size_prefixed = true;
|
||||
} else if (arg == "--") { // Separator between text and binary inputs.
|
||||
binary_files_from = filenames.size();
|
||||
} else if (arg == "--proto") {
|
||||
@@ -325,7 +328,7 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
||||
"\" matches the schema, use --raw-binary to read this file"
|
||||
" anyway.");
|
||||
} else if (!flatbuffers::BufferHasIdentifier(
|
||||
contents.c_str(), parser->file_identifier_.c_str())) {
|
||||
contents.c_str(), parser->file_identifier_.c_str(), opts.size_prefixed)) {
|
||||
Error("binary \"" + filename +
|
||||
"\" does not have expected file_identifier \"" +
|
||||
parser->file_identifier_ +
|
||||
|
||||
@@ -263,8 +263,9 @@ bool GenerateText(const Parser &parser, const void *flatbuffer,
|
||||
std::string &text = *_text;
|
||||
assert(parser.root_struct_def_); // call SetRootType()
|
||||
text.reserve(1024); // Reduce amount of inevitable reallocs.
|
||||
if (!GenStruct(*parser.root_struct_def_, GetRoot<Table>(flatbuffer), 0,
|
||||
parser.opts, _text)) {
|
||||
auto root = parser.opts.size_prefixed ?
|
||||
GetSizePrefixedRoot<Table>(flatbuffer) : GetRoot<Table>(flatbuffer);
|
||||
if (!GenStruct(*parser.root_struct_def_, root, 0, parser.opts, _text)) {
|
||||
return false;
|
||||
}
|
||||
text += NewLine(parser.opts);
|
||||
|
||||
@@ -2412,9 +2412,15 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
|
||||
}
|
||||
uoffset_t toff;
|
||||
ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
|
||||
builder_.Finish(Offset<Table>(toff), file_identifier_.length()
|
||||
? file_identifier_.c_str()
|
||||
: nullptr);
|
||||
if (opts.size_prefixed) {
|
||||
builder_.FinishSizePrefixed(Offset<Table>(toff), file_identifier_.length()
|
||||
? file_identifier_.c_str()
|
||||
: nullptr);
|
||||
} else {
|
||||
builder_.Finish(Offset<Table>(toff), file_identifier_.length()
|
||||
? file_identifier_.c_str()
|
||||
: nullptr);
|
||||
}
|
||||
} else if (IsIdent("enum")) {
|
||||
ECHECK(ParseEnum(false, nullptr));
|
||||
} else if (IsIdent("union")) {
|
||||
@@ -2521,7 +2527,11 @@ void Parser::Serialize() {
|
||||
builder_.CreateString(file_identifier_),
|
||||
builder_.CreateString(file_extension_),
|
||||
root_struct_def_ ? root_struct_def_->serialized_location : 0);
|
||||
builder_.Finish(schema_offset, reflection::SchemaIdentifier());
|
||||
if (opts.size_prefixed) {
|
||||
builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
|
||||
} else {
|
||||
builder_.Finish(schema_offset, reflection::SchemaIdentifier());
|
||||
}
|
||||
}
|
||||
|
||||
Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,
|
||||
|
||||
Reference in New Issue
Block a user