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 a cross platform serialization library architected for
maximum memory efficiency. It allows you to directly access serialized data without parsing/unpacking it first, while still having great forwards/backwards compatibility.
Go to our landing page to browse our documentation.
Supported operating systems
- Windows
- macOS
- Linux
- Android
- And any others with a recent C++ compiler (C++ 11 and newer)
Supported programming languages
Code generation and runtime libraries for many popular languages.
- C
- C++ - snapcraft.io
- C# - nuget.org
- Dart - pub.dev
- Go - go.dev
- Java - Maven
- JavaScript - NPM
- Kotlin
- Lobster
- Lua
- PHP
- Python - PyPI
- Rust - crates.io
- Swift - swiftpackageindex
- TypeScript - NPM
- Nim
Versioning
FlatBuffers does not follow traditional Semver versioning (see rationale) but rather uses a format of the date of the release.
Contribution
To contribute to this project, see CONTRIBUTING.
Security
Please see our Security Policy for reporting vulnerabilities.
Licensing
Flatbuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.