<divclass="textblock"><p>FlatBuffers supports reading and writing binary FlatBuffers in Java and C#. Generate code for Java with the <code>-j</code> option to <code>flatc</code>, or for C# with <code>-n</code> (think .Net).</p>
<p>Note that this document is from the perspective of Java. Code for both languages is generated in the same way, with only very subtle differences, for example any <code>camelCase</code> Java call will be <code>CamelCase</code> in C#.</p>
<p>Note that this document is from the perspective of Java. Code for both languages is generated in the same way, with only minor differences. These differences are <ahref="#differences-in-c-sharp">explained in a section below</a>.</p>
<p>See <code>javaTest.java</code> for an example. Essentially, you read a FlatBuffer binary file into a <code>byte[]</code>, which you then turn into a <code>ByteBuffer</code>, which you pass to the <code>getRootAsMyRootType</code> function:</p>
</div><!-- fragment --><p>The buffer is now ready to be transmitted. It is contained in the <code>ByteBuffer</code> which you can obtain from <code>fbb.dataBuffer()</code>. Importantly, the valid data does not start from offset 0 in this buffer, but from <code>fbb.dataBuffer().position()</code> (this is because the data was built backwards in memory). It ends at <code>fbb.capacity()</code>.</p>
<h2>Text Parsing</h2>
<h2>Differences in C-sharp</h2>
<p>C# code works almost identically to Java, with only a few minor differences. You can see an example of C# code in <code>tests/FlatBuffers.Test/FlatBuffersExampleTests.cs</code>.</p>
<p>First of all, naming follows standard C# style with <code>PascalCasing</code> identifiers, e.g. <code>GetRootAsMyRootType</code>. Also, values (except vectors and unions) are available as properties instead of parameterless accessor methods as in Java. The performance-enhancing methods to which you can pass an already created object are prefixed with <code>Get</code>, e.g.:</p>
<p>There currently is no support for parsing text (Schema's and JSON) directly from Java, though you could use the C++ parser through JNI. Please see the C++ documentation for more on text parsing. </p>
<divclass="textblock"><p>There's experimental support for reading FlatBuffers in Python. Generate code for Python with the <code>-p</code> option to <code>flatc</code>.</p>
<p>See <code>py_test.py</code> for an example. You import the generated code, read a FlatBuffer binary file into a <code>bytearray</code>, which you pass to the <code>GetRootAsMonster</code> function:</p>
</div><!-- fragment --><p>To access vectors you pass an extra index to the vector field accessor. Then a second method with the same name suffixed by <code>Length</code> let's you know the number of elements you can access:</p>
<divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="keywordflow">for</span> i <spanclass="keywordflow">in</span> xrange(monster.InventoryLength()):</div>
<divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span>  monster.Inventory(i) <spanclass="comment"># do something here</span></div>
</div><!-- fragment --><p>You can also construct these buffers in Python using the functions found in the generated code, and the FlatBufferBuilder class:</p>
</div><!-- fragment --><p>Unlike C++, Python does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the <code>Vec3</code> is an inline component of <code>Monster</code>, it has to be created right where it is added, whereas the name and the inventory are not inline, and <b>must</b> be created outside of the table creation sequence). Structs do have convenient methods that allow you to construct them in one call. These also have arguments for nested structs, e.g. if a struct has a field <code>a</code> and a nested struct field <code>b</code> (which has fields <code>c</code> and <code>d</code>), then the arguments will be <code>a</code>, <code>c</code> and <code>d</code>.</p>
<p>Vectors also use this start/end pattern to allow vectors of both scalar types and structs:</p>
</div><!-- fragment --><p>The generated method 'StartInventoryVector' is provided as a convenience function which calls 'StartVector' with the correct element size of the vector type which in this case is 'ubyte' or 1 byte per vector element. You pass the number of elements you want to write. You write the elements backwards since the buffer is being constructed back to front. Use the correct <code>Prepend</code> call for the type, or <code>PrependUOffsetT</code> for offsets. You then pass <code>inv</code> to the corresponding <code>Add</code> call when you construct the table containing it afterwards.</p>
<p>There are <code>Prepend</code> functions for all the scalar types. You use <code>PrependUOffset</code> for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate <code>create</code> function in-line, as shown above in the <code>Monster</code> example.</p>
<p>Once you're done constructing a buffer, you call <code>Finish</code> with the root object offset (<code>mon</code> in the example above). Your data now resides in Builder.Bytes. Important to note is that the real data starts at the index indicated by Head(), for Offset() bytes (this is because the buffer is constructed backwards). If you wanted to read the buffer right after creating it (using <code>GetRootAsMonster</code> above), the second argument, instead of <code>0</code> would thus also be <code>Head()</code>.</p>
<h2>Text Parsing</h2>
<p>There currently is no support for parsing text (Schema's and JSON) directly from Python, though you could use the C++ parser through SWIG or ctypes. Please see the C++ documentation for more on text parsing. </p>
<trid="row_3_"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__cpp_usage.html"target="_self">Use in C++</a></td><tdclass="desc"></td></tr>
<trid="row_4_"class="even"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__go_usage.html"target="_self">Use in Go</a></td><tdclass="desc"></td></tr>
<trid="row_5_"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__java_usage.html"target="_self">Use in Java/C-sharp</a></td><tdclass="desc"></td></tr>
<trid="row_7_"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__white_paper.html"target="_self">FlatBuffers white paper</a></td><tdclass="desc"></td></tr>
<trid="row_9_"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__grammar.html"target="_self">Formal Grammar of the schema language</a></td><tdclass="desc"></td></tr>
<trid="row_6_"class="even"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__python_usage.html"target="_self">Use in Python</a></td><tdclass="desc"></td></tr>
<trid="row_8_"class="even"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__white_paper.html"target="_self">FlatBuffers white paper</a></td><tdclass="desc"></td></tr>
<trid="row_10_"class="even"><tdclass="entry"><spanstyle="width:16px;display:inline-block;"> </span><aclass="el"href="md__grammar.html"target="_self">Formal Grammar of the schema language</a></td><tdclass="desc"></td></tr>