What is flatbuffers?
FlatBuffers is an efficient cross-platform serialization library for C++, C#, C, Go, Java, JavaScript, TypeScript, PHP, and Python. It allows you to create and access serialized data without parsing/unpacking it, making it ideal for performance-critical applications.
What are flatbuffers's main functionalities?
Creating a FlatBuffer
This code demonstrates how to create a FlatBuffer with a string and a vector of strings. The buffer is then finished and converted to a Uint8Array.
const flatbuffers = require('flatbuffers');
const builder = new flatbuffers.Builder(1024);
// Create a string
const str = builder.createString('Hello, FlatBuffers!');
// Create a vector of strings
const strings = builder.createVectorOfStrings(['foo', 'bar', 'baz']);
// Finish the buffer
builder.finish(strings);
// Get the buffer
const buf = builder.asUint8Array();
console.log(buf);
Reading a FlatBuffer
This code demonstrates how to read a FlatBuffer. It assumes that the buffer contains data for a 'Monster' object, and it accesses the 'name' and 'hp' fields of the object.
const flatbuffers = require('flatbuffers');
const MyGame = require('./mygame_generated'); // Generated code
// Assume buf is a Uint8Array containing the FlatBuffer data
const buf = new Uint8Array([/* FlatBuffer data */]);
// Get the root object
const bb = new flatbuffers.ByteBuffer(buf);
const root = MyGame.Example.Monster.getRootAsMonster(bb);
// Access data
console.log(root.name());
console.log(root.hp());
Other packages similar to flatbuffers
protobufjs
Protobuf.js is a pure JavaScript implementation of Protocol Buffers, a language-neutral, platform-neutral, extensible mechanism for serializing structured data. Compared to FlatBuffers, Protobuf.js requires a parsing step to access data, which can be less efficient in performance-critical applications.
msgpack
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON but it's faster and smaller. Compared to FlatBuffers, MessagePack is simpler but may not offer the same level of performance for complex data structures.
avsc
Avro is a data serialization system that provides rich data structures and a compact, fast, binary data format. The 'avsc' package is a pure JavaScript implementation of Avro. Compared to FlatBuffers, Avro is more focused on schema evolution and data interchange, while FlatBuffers is optimized for performance.
FlatBuffers
FlatBuffers is an efficient cross platform serialization library for games and
other memory constrained apps. It allows you to directly access serialized data without
unpacking/parsing it first, while still having great forwards/backwards compatibility.
Go to our landing page to browse our documentation.
Supported operating systems
- Android
- Windows
- MacOS X
- Linux
Supported programming languages
- C++
- C#
- C
- Go
- Java
- JavaScript
- PHP
- Python
and many more in progress...
Contribution
To contribute to this project, see CONTRIBUTING.
Integration
For applications on Google Play that integrate this tool, usage is tracked.
This tracking is done automatically using the embedded version string
(flatbuffer_version_string
), and helps us continue to optimize it. Aside from
consuming a few extra bytes in your application binary, it shouldn't affect
your application at all. We use this information to let us know if FlatBuffers
is useful and if we should continue to invest in it. Since this is open
source, you are free to remove the version string but we would appreciate if
you would leave it in.
Licensing
Flatbuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.