Generate flatbuffers directly from node.
const flatc = require("flatbuffers-addon");
const buffer = flatc.binary(options);
type: String|Buffer
if schema
is a String and schema_contents
is null
or undefined
, schema
will be treated as schema_contents
.
otherwise, it will be treated as the schema file path.
type: String|Buffer
The schema contents.
If the schema_contents
is a schema binary, the schema
file path must end with .bfbs
.
type: bool
default: false
Serialize schemas instead of JSON
type: String|Buffer|Object
if json
is a String and json_contents
is null
or undefined
, json
will be treated as json_contents
.
if json
is a Buffer or an Object, json
will be treated as json_contents
.
otherwise json
must be a String and will be treated as the path to the JSON.
type: String|Buffer|Object
The JSON to serialize
type: [String]
Include directories for schemas
type: String|Buffer
Specify a schema the following schema
should be an evolution of.
if conform
is a String and conform_contents
is null
or undefined
, conform
will be treated as conform_contents
.
type: String|Buffer
Conform schema contents.
type: [String]
Include directories for conform schemas
NOT TESTED
type: bool
default: false
field names must be / will be quoted, no trailing commas in tables/vectors.
type: bool
default: false
Allow scalar fields to be null
NOT TESTED
type: bool
default: false
Pass non-UTF-8 input through parser
type: bool
default: false
Allow fields in JSON that are not defined in the schema. These fields will be discared when generating binaries.
NOT TESTED
type: bool
default: false
Input binaries are size prefixed buffers.
NOT TESTED
type: bool
default: false
Input is a .proto, translate to .fbs.
NOT TESTED
type: bool
default: false
Translate .proto oneofs to flatbuffer unions.
NOT TESTED
type: bool
default: false
Add doc comments to the binary schema files.
NOT TESTED
type: bool
default: false
Add builtin attributes to the binary schema files.
type: bool
default: false
If false, don't serialize values equal to the default, therefore reducing size of the binary output.
const flatc = require("flatbuffers-addon");
const code = flatc.js(options);
type: String|Buffer
if schema
is a String and schema_contents
is null
or undefined
, schema
will be treated as schema_contents
.
otherwise, it will be treated as the schema file path.
type: String|Buffer
The schema contents.
If the schema_contents
is a schema binary, the schema
file path must end with .bfbs
.
type: [String]
Include directories for schemas
type: String|Buffer
Specify a schema the following schema
should be an evolution of.
if conform
is a String and conform_contents
is null
or undefined
, conform
will be treated as conform_contents
.
type: String|Buffer
Conform schema contents.
type: [String]
Include directories for conform schemas
NOT TESTED
type: String
ts
to generate TypeScript code.
NOT TESTED
type: bool
default: false
Pass non-UTF-8 input through parser
type: bool
default: false
Generate accessors that can mutate buffers in-place.
NOT TESTED
type: bool
default: false
Generate not just code for the current schema files, but for all files it includes as well. If the language uses a single file for output (by default the case for C++ and JS), all code will end up in this one file.
type: bool
default: false
Removes Node.js style export lines in JS.
NOT TESTED
type: bool
default: false
Uses goog.exports* for closure compiler exporting in JS.
NOT TESTED
type: bool
default: false
Uses ECMAScript 6 export style lines in JS.
NOT TESTED
type: bool
default: false
Keep original prefix of schema include statement.
NOT TESTED
type: bool
default: false
Don't include flatbuffers import statement for TypeScript.
NOT TESTED
type: bool
default: true
re-export imported dependencies for TypeScript
NOT TESTED
type: bool
default: true
Use short function names for JS and TypeScript.
const { flatbuffers } = require("flatbuffers");
const flatc = require("flatbuffers-addon");
const schema = `
namespace some.nested.namespace;
file_extension "dat";
table Book {
id:string (id: 0);
title:string (id: 1);
authors:[string] (id: 2);
release:ulong (id: 3);
genres: [ulong] (id: 4);
}
table Library {
name:string (id: 0);
books: [Book] (id: 1);
}
root_type Library;
`;
const js = flatc.js({
schema
});
const library = {
name: "BookShop 0",
books: [{
id: "book-0",
title: "Book 0",
authors: ["Author 0"]
}]
};
const buffer = flatc.binary({
schema,
json: library
});
const deserialized = ((code, binary) => {
// Evalute generated js code
const sandbox = {};
(new Function(code)).call(sandbox);
// @see https://google.github.io/flatbuffers/flatbuffers_guide_use_javascript.html
const bytes = new Uint8Array(binary);
const buf = new flatbuffers.ByteBuffer(bytes);
// Deserialized flatbuffers binary data
const Library = sandbox.some.nested.namespace.Library;
return Library.getRootAsLibrary(buf);
})(js, buffer);
console.log(deserialized.name() === "BookShop 0");
console.log(deserialized.books(0).title() === "Book 0");