google / flatbuffers

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

Nesting of structs inside a table in Java #4194

Closed pam2014 closed 7 years ago

pam2014 commented 7 years ago

Ques 1: Is it possible to nest structs inside a table? More specifically something like

struct vertex { x:double; y:double; }

table rect { vertices:[vertex] }

Tutorial states that structs are stored in-line similar to scalars. In case of nested structs, are the offsets required to be collected into an array (similar to what is done for nesting of tables)?

If yes, I see a problem in the generated Java files, that for structs CreateXXXVector(..) is not generated. Then how to achieve packing?

I have tried in an alternative fashion to loop a call to rect.addVertices(builder, vertex.createVertex(......)); However with this method, the deserialization seems to come unstuck. Calls to rect.verticesLength() returns garbage value.

Can someone shed some light on this?

Thanks, -Pam

aardappel commented 7 years ago

Yes, this is possible, we just don't have a good example for it in the docs, which we should fix.

The gist of it is (haven't checked this code):

fbb.StartVector(16, numelems, 8);  // FIXME: should not require these.
for(..) vertex.createVertex(......);
vec = EndVector();
pam2014 commented 7 years ago

Thanks. I did resolve my problem. I also realized that while the tutorial states that during nesting, the offsets should be taken into a temporary array, this step is not required for structs.

aardappel commented 7 years ago

This now finally has been properly documented: https://github.com/google/flatbuffers/commit/262e1d7bf962dc796e47b2186bad6944515ac833