serialize and deserialize from flatbuffers to and from bytearray

5,088 views
Skip to first unread message

paul.ro...@googlemail.com

unread,
Oct 20, 2014, 11:23:59 AM10/20/14
to flatb...@googlegroups.com
hi group community :)

i want create a flatbuffers object in java which i can serialize to a byte array,
and later deserialize back in the java object. i work the first time with flatbuffers
and i can't initialised a java object. my way step by step:
1. write a scheme
2. compile this with the flatbuffers compiler into a java class
3. import the flatbuffers class from github
4. try to build a java object (!)

(!)
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
int str = fbb.createString("Dummy");
Monster.startPerson(fbb);
Monster.addName(fbb, str);
int p = Person.endPerson(fbb);

Maybe someone can post a simple example to creating a flatbuffers object, and how to serialize and deserialize to and from bytearray?

Hope for an answer and best regards,
Paul

Wouter van Oortmerssen

unread,
Oct 22, 2014, 9:17:14 PM10/22/14
to paul.ro...@googlemail.com, flatb...@googlegroups.com
That looks correct. Now you just need to finish the buffer:

fbb.finish(p);

(assuming Person is your root type). If you want a byte array instead of a ByteBuffer, you can just call fbb.sizedByteArray();



--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

paul.ro...@googlemail.com

unread,
Oct 23, 2014, 7:01:00 AM10/23/14
to flatb...@googlegroups.com
thanks a lot, it works :)
Reply all
Reply to author
Forward
0 new messages