google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
23.28k stars 3.25k forks source link

Why the need for a Root #65

Closed lbensaad closed 10 years ago

lbensaad commented 10 years ago

What is the reason of using a root type. If i have Request and Response tables to be sent between two parties, so why we could not create them separately. A buffer could contain either a Request or a Response. In fact any message(Table) should be able to a root in a buffer.

ghost commented 10 years ago

Any table can be a root in a buffer. The root type is mostly for use by JSON, which doesn't specify any types, so we need to know where to start. The generated GetRoot functions are merely convenience.

lbensaad commented 10 years ago

Ok, thanks. But in Java,

  1. If i have a ByteBuffer of a non root table how do read that using FlatBuffersBuilder. The only way I know is to use the __init() function, is there another way?
  2. Is there a way to know the type of the table from the binary data?
  3. How to use the addXXXType function, in your JavaTest.java you used: Monster.addTestType(fbb, (byte)1); how do you get the type of a table, in this case 1 which it is hard-coded ?
ghost commented 10 years ago

1) a commit was pushed yesterday that adds GetRootAs functions for all tables, not just the root_type.

2) generally no. this is a strongly types system, meaning you need to know the kind of buffer you're dealing with. If you want to use this in a context where you want to have multiple different root types, you have these options: a) make your root type a table that contains a union of all possible sub-roots. b) prefix flatbuffers with your own file header c) use flatbuffer's built-in file indentification feature, which hasn't been ported to Java yet. I'll get to that.

3) That's a bug, the 1 should actually read: Any.Monster. I'll fix that.

lbensaad commented 10 years ago

Thanks 1) But i think the name should be just "get" or "init" instead of getRootAs

ghost commented 10 years ago

getRootAs is a bit more descriptive and less likely to clash with other identifiers. I think I'll keep it for now.

To further my response to your issue #2, the built-in file_identifier functionality is now also available in Java. See the Schema docs, Java docs, and Java code.

let4be commented 8 years ago

So... How do I access non root_type buffers, can't find anything like Get*\ in c++ generated files and the same problem in go lang. generated code has huge differencies between languages and it's really hard to work with

let4be commented 8 years ago

Tried both latest master and latest release versions

ghost commented 8 years ago

In C++ there is the GetRoot templated function that works with any table type. Go appears to still be pending on: https://github.com/google/flatbuffers/pull/363

gaurimaheshwari commented 6 years ago

please explain me how to build flatc executable?

aardappel commented 6 years ago

@gaurimaheshwari : https://google.github.io/flatbuffers/flatbuffers_guide_building.html