how to write a generic toString() method?

938 views
Skip to first unread message

Shahbaz Chaudhary

unread,
Sep 30, 2015, 2:43:20 PM9/30/15
to FlatBuffers
Given that the definition file has complete information about attributes of tables, is there a way for me to access that data through the FB api?

I have a dozen or so events defined in the schema and I'm using it to read and write data. However, I often need to print the name of each event or list all attributes of each event. I need to do this for debugging or other purposes. What is the best way to do this?

The docs say that the c++ api allows reflection. I am (currently) using c++, however, as a java developer, I associate reflection with a slow, hackey solution.  Besides, reflection only seems to be allowed in the c++ api. 

Given that the schema already has all this information, isn't there a way for me to do:
event.getName() or
event.getAttributes()[0] ...

Wouter van Oortmerssen

unread,
Sep 30, 2015, 5:06:13 PM9/30/15
to Shahbaz Chaudhary, FlatBuffers
Reflection is slow(er), but not necessarily hacky, it is just a different way of doing things.

All code generated for current languages strip away the "dynamicness" of having runtime information for maximum speed, so no, they don't have "getFields" style calls. Even if you had such a call, once you know about a field, being able to access it would require further code, which is exactly what the reflection code currently does.

So for debug prints, you currently have two options: idl_gen_text.cpp can turn a FlatBuffer into a JSON string, and reflection.h can also turn individual parts of FlatBuffers into strings. I know these are both C++, but since the input is a FlatBuffer and the output is a string, this makes for a very simple JNI interface.

Beyond that, the reflection code is based on something that is available in all languages: you can turn a schema into a FlatBuffer, which can then be read by any language. From this meta-schema generated code you can then find out what fields a table has, etc. What is currently missing in Java is to turn that knowledge into field-accesses, much like reflection.h.
 

--
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.

Shahbaz Chaudhary

unread,
Sep 30, 2015, 9:01:56 PM9/30/15
to FlatBuffers, shah...@gmail.com
Are there examples which show how to us reflection and how to generate json? Our existing code actually generates json so I'm curious to know if I can use FB's built in json functionality!

Wouter van Oortmerssen

unread,
Oct 7, 2015, 3:18:30 PM10/7/15
to Shahbaz Chaudhary, FlatBuffers
On using reflection: see the "Reflection" section in https://google.github.io/flatbuffers/md__cpp_usage.html and check the comments in reflection.h, and test.cpp:ReflectionTest(), that should give you an impression how it can be used.

On using the JSON parser: see "Making your program capable of loading text directly" in https://google.github.io/flatbuffers/md__cpp_usage.html as well.


Reply all
Reply to author
Forward
0 new messages