How to get the schema for a dynamically constructed flatbuffer?

632 views
Skip to first unread message

nostrom...@gmail.com

unread,
Mar 30, 2017, 11:43:45 AM3/30/17
to FlatBuffers
Hi all --

In test.cpp, GetSchema() is used to retrieve the schema from a .bfbs file (which contains schema data).

However, I've constructed an in-memory flatbuffer with a builder. When I dump the flatbuffer, schema data is not present (as expected). Is there anyway to get the schema for my flatbuffer?

I suspect that I could make a dummy .bfbs file to load the schema from, but that seems terribly hacky. Ideally, I'd like to pull the schema from a .fbs file.

Any suggestions?

Thanks!

mikkelfj

unread,
Mar 31, 2017, 12:55:26 PM3/31/17
to FlatBuffers


On Thursday, March 30, 2017 at 5:43:45 PM UTC+2, nostrom...@gmail.com wrote:
I suspect that I could make a dummy .bfbs file to load the schema from, but that seems terribly hacky. Ideally, I'd like to pull the schema from a .fbs file.

I am not at all sure I follow what you a trying to do. But to get a bfbs binary schema from a text fbs schema at runtime without calling the flatc compiler, there are probably two options:

One is to figure out what flatc is doing internally and extract parts of the source - it has been done before with the schema parser.

The other is to use libflatcc (I wrote), which takes in in-memory text buffer and generates either C generated code, or a .bfbs schema file.
This example tests a very convoluted fbs schema in memory. You will have to set different options to produce .bfbs files, and it hasn't been tested, but it is designed to support such scenarios.

mikkelfj

unread,
Mar 31, 2017, 1:04:56 PM3/31/17
to FlatBuffers
see also

https://github.com/dvidelabs/flatcc/blob/master/include/flatcc/flatcc.h#L238
void *flatcc_generate_binary_schema(flatcc_context_t ctx, size_t *size);

Wouter van Oortmerssen

unread,
Apr 3, 2017, 4:09:38 PM4/3/17
to mikkelfj, FlatBuffers
Are you using reflection functionality? If not, you don't need the binary schema at all.

If you, you do need a valid binary schema. You can compile it with flatc. You can also generate one at runtime by loading the schema with the Parser, and then calling Parser::Serialize().

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages