FlatBuffers.jl Documentation
Usage
FlatBuffers.jl provides native Julia support for reading and writing binary structures following the google flatbuffer schema (see here for a more in-depth reveiw of the binary format).
The typical language support for flatbuffers involves utilizing the flatc
compiler to translate a flatbuffer schema file (.fbs) into a langugage-specific set of types/classes and methods. See here for the official guide on writing schemas.
Currently in Julia, the flatc
compiler isn't supported, but FlatBuffers.jl provides a native implementation of reading/writing the binary format directly with native Julia types. What does this mean exactly? Basically you can take a schema like:
namespace example;
table SimpleType {
x: int = 1;
}
root_type SimpleType;
and do a straightforward Julia translation like:
module Example
using FlatBuffers
type SimpleType
x::Int32
end
@default SimpleType x=1
end
A couple of things to point out:
using FlatBuffers
was included near the top to bring in the FlatBuffers module; this defines the necessary FlatBuffers.jl machinery for making the schema definitions easierint
translates to a JuliaInt32
, see more info on flatbuffer types hereA default value for the
x
field inSimpleType
was declared after the type with the@default
macroNo
root_type
definition is necessary in Julia; basically any type defined withtype
(i.e. not abstract or immutable) can be a valid root table type in Julia.
So let's see how we can actually use a flatbuffer in Julia:
using FlatBuffers, Example # the schema module we defined above
val = Example.SimpleType(2) # create an instance of our type
flatbuffer = FlatBuffers.build!(val) # start and build a flatbuffer for our SimpleType
val2 = FlatBuffers.read(flatbuffer) # now we can deserialize the value from our flatbuffer, `val2` == `val`
For more involved examples, see the test suite here.
Reference
Documentation is included inline for each type/method, these can be accessed at the REPL by type ?foo
where foo
is the name of the type or method you'd like more information on.
List of types/methods:
FlatBuffers.Table{T}
: type for deserializing a Julia typeT
from a flatbufferFlatBuffers.Builder{T}
: type for serializing a Julia typeT
to a flatbufferFlatBuffers.read
: performs the actual deserializing on aFlatBuffer.Table
FlatBuffers.build!
: performs the actual serializing on aFlatBuffer.Builder
@align T size_in_bytes
: convenience macro for forcing a flatbuffer alignment on the Julia typeT
tosize_in_bytes
@default T field1=val1 field2=val2 ...
: convenience macro for defining default field values for Julia typeT
@union T Union{T1,T2,...}
: convenience macro for defining a flatbuffer union typeT
@struct immutable T fields... end
: convenience macro for defining flatbuffer struct types, ensuring any necessary padding gets added to the type definition