diff --git a/docs/html/md__java_usage.html b/docs/html/md__java_usage.html index fa61ef0c2..c67f5692a 100644 --- a/docs/html/md__java_usage.html +++ b/docs/html/md__java_usage.html @@ -54,7 +54,7 @@ $(document).ready(function(){initNavTree('md__java_usage.html','');});
FlatBuffers supports reading and writing binary FlatBuffers in Java and C#. Generate code for Java with the -j option to flatc, or for C# with -n (think .Net).
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 camelCase Java call will be CamelCase in C#.
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 explained in a section below.
See javaTest.java for an example. Essentially, you read a FlatBuffer binary file into a byte[], which you then turn into a ByteBuffer, which you pass to the getRootAsMyRootType function:
To finish the buffer, call:
The buffer is now ready to be transmitted. It is contained in the ByteBuffer which you can obtain from fbb.dataBuffer(). Importantly, the valid data does not start from offset 0 in this buffer, but from fbb.dataBuffer().position() (this is because the data was built backwards in memory). It ends at fbb.capacity().
C# code works almost identically to Java, with only a few minor differences. You can see an example of C# code in tests/FlatBuffers.Test/FlatBuffersExampleTests.cs.
First of all, naming follows standard C# style with PascalCasing identifiers, e.g. GetRootAsMyRootType. 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 Get, e.g.:
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.
|
+ FlatBuffers
+
+ |
+
There's experimental support for reading FlatBuffers in Python. Generate code for Python with the -p option to flatc.
See py_test.py for an example. You import the generated code, read a FlatBuffer binary file into a bytearray, which you pass to the GetRootAsMonster function:
Now you can access values like this:
+To access vectors you pass an extra index to the vector field accessor. Then a second method with the same name suffixed by Length let's you know the number of elements you can access:
You can also construct these buffers in Python using the functions found in the generated code, and the FlatBufferBuilder class:
+Create strings:
+Create a table with a struct contained therein:
+Unlike C++, Python does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the Vec3 is an inline component of Monster, it has to be created right where it is added, whereas the name and the inventory are not inline, and must 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 a and a nested struct field b (which has fields c and d), then the arguments will be a, c and d.
Vectors also use this start/end pattern to allow vectors of both scalar types and structs:
+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 Prepend call for the type, or PrependUOffsetT for offsets. You then pass inv to the corresponding Add call when you construct the table containing it afterwards.
There are Prepend functions for all the scalar types. You use PrependUOffset for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate create function in-line, as shown above in the Monster example.
Once you're done constructing a buffer, you call Finish with the root object offset (mon 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 GetRootAsMonster above), the second argument, instead of 0 would thus also be Head().
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.
+