![]() |
FlatBuffers
An open source project by FPL.
|
Before diving into the FlatBuffers usage in JavaScript, it should be noted that the Tutorial page has a complete guide to general FlatBuffers usage in all of the supported languages (including JavaScript). This page is specifically designed to cover the nuances of FlatBuffers usage in JavaScript.
You should also have read the Building documentation to build flatc
and should be familiar with Using the schema compiler and Writing a schema.
The code for the FlatBuffers JavaScript library can be found at flatbuffers/js
. You can browse the library code on the FlatBuffers GitHub page.
The code to test the JavaScript library can be found at flatbuffers/tests
. The test code itself is located in JavaScriptTest.js.
To run the tests, use the JavaScriptTest.sh shell script.
Note: The JavaScript test file requires Node.js.
Note: See Tutorial for a more in-depth example of how to use FlatBuffers in JavaScript.
FlatBuffers supports both reading and writing FlatBuffers in JavaScript.
To use FlatBuffers in your own code, first generate JavaScript classes from your schema with the --js
option to flatc
. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.
For example, here is how you would read a FlatBuffer binary file in Javascript: First, include the library and generated code. Then read the file into an Uint8Array
. Make a flatbuffers.ByteBuffer
out of the Uint8Array
, and pass the ByteBuffer to the getRootAsMonster
function.
Note: Both JavaScript module loaders (e.g. Node.js) and browser-based HTML/JavaScript code segments are shown below in the following snippet:
Now you can access values like this:
There currently is no support for parsing text (Schema's and JSON) directly from JavaScript.