1 Use in Lobster {#flatbuffers_guide_use_lobster} 2 ============== 3 4 ## Before you get started 5 6 Before diving into the FlatBuffers usage in Lobster, it should be noted that the 7 [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to general 8 FlatBuffers usage in all of the supported languages (including Lobster). This 9 page is designed to cover the nuances of FlatBuffers usage, specific to 10 Lobster. 11 12 You should also have read the [Building](@ref flatbuffers_guide_building) 13 documentation to build `flatc` and should be familiar with 14 [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and 15 [Writing a schema](@ref flatbuffers_guide_writing_schema). 16 17 ## FlatBuffers Lobster library code location 18 19 The code for the FlatBuffers Lobster library can be found at 20 `flatbuffers/lobster`. You can browse the library code on the 21 [FlatBuffers GitHub page](https://github.com/google/flatbuffers/tree/master/ 22 lobster). 23 24 ## Testing the FlatBuffers Lobster library 25 26 The code to test the Lobster library can be found at `flatbuffers/tests`. 27 The test code itself is located in [lobstertest.lobster](https://github.com/google/ 28 flatbuffers/blob/master/tests/lobstertest.lobster). 29 30 To run the tests, run `lobster lobstertest.lobster`. To obtain Lobster itself, 31 go to the [Lobster homepage](http://strlen.com/lobster) or 32 [github](https://github.com/aardappel/lobster) to learn how to build it for your 33 platform. 34 35 ## Using the FlatBuffers Lobster library 36 37 *Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth 38 example of how to use FlatBuffers in Lobster.* 39 40 There is support for both reading and writing FlatBuffers in Lobster. 41 42 To use FlatBuffers in your own code, first generate Lobster classes from your 43 schema with the `--lobster` option to `flatc`. Then you can include both 44 FlatBuffers and the generated code to read or write a FlatBuffer. 45 46 For example, here is how you would read a FlatBuffer binary file in Lobster: 47 First, import the library and the generated code. Then read a FlatBuffer binary 48 file into a string, which you pass to the `GetRootAsMonster` function: 49 50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lobster} 51 include "monster_generated.lobster" 52 53 let fb = read_file("monsterdata_test.mon") 54 assert fb 55 let monster = MyGame_Example_GetRootAsMonster(fb) 56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 57 58 Now you can access values like this: 59 60 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.lobster} 61 let hp = monster.hp 62 let pos = monster.pos 63 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 64 65 As you can see, even though `hp` and `pos` are functions that access FlatBuffer 66 data in-place in the string buffer, they appear as field accesses. 67 68 ## Speed 69 70 Using FlatBuffers in Lobster should be relatively fast, as the implementation 71 makes use of native support for writing binary values, and access of vtables. 72 Both generated code and the runtime library are therefore small and fast. 73 74 Actual speed will depend on wether you use Lobster as bytecode VM or compiled to 75 C++. 76 77 ## Text Parsing 78 79 Lobster has full support for parsing JSON into FlatBuffers, or generating 80 JSON from FlatBuffers. See `samples/sample_test.lobster` for an example. 81 82 This uses the C++ parser and generator underneath, so should be both fast and 83 conformant. 84 85 <br> 86