This is a fork of the Google Flatbuffers Library with the following features added:
- A
--preserve-caseflag to prevent IDL name mangling - JSON Schema schema import/export (
--jsonschema,*.schema.json) - Optional lossless JSON Schema round-tripping via
--jsonschema-xflatbuffersmetadata
This fork adds a few features to the flatc compiler intended to treat JSON Schema as a first-class schema format, while still flowing through the same FlatBuffers schema IR (see reflection/reflection.fbs).
By default, many language generators apply case conversions to schema identifiers (for example converting snake_case field names into camelCase accessors). The --preserve-case flag disables this name mangling for identifiers coming from the schema, and emits names “as written” instead.
Example:
flatc --cpp --preserve-case schema.fbsNotes:
- This is currently supported for these generators: C++, Go, Java, Rust, Dart, Python, TypeScript, PHP, and JSON Schema (see
src/flatc.cpp). - Implementation:
src/flatc.cpp,src/util.cpp. - Tests:
tests/GoTest.sh,tests/PHPTest.sh,tests/PythonTest.sh,tests/JsonSchemaTest.sh.
- Export a FlatBuffers schema (
.fbs) to JSON Schema (.schema.json). - Import a JSON Schema (
.schema.json) as a schema input (as if it were an IDL), map it into FlatBuffers’ schema IR, and run the normal FlatBuffers code generators.
Implementation:
- JSON Schema generator:
src/idl_gen_json_schema.cpp - JSON Schema importer/parser:
src/idl_parser.cpp(Parser::DoParseJsonSchema) - CLI wiring +
.schema.jsoninput detection:src/flatc.cpp - Additional docs:
docs/source/json_schema.md,docs/source/flatc.md
Generate *.schema.json from *.fbs:
flatc --jsonschema -o out_dir schema.fbsThis produces out_dir/schema.schema.json.
Any file ending in .schema.json can be used anywhere flatc expects a schema file:
flatc --cpp -o out_dir schema.schema.jsonRoot selection:
- If the schema root contains a
$refto a definition, that definition becomes the FlatBuffers root type. - Otherwise you can specify/override the root with
--root-type(seesrc/flatc.cppanddocs/source/flatc.md).
The importer is intentionally permissive and ignores unknown JSON Schema/OpenAPI keywords while making “sane defaults” to treat the input as an IDL.
Supported input shapes:
- Schema definitions under
definitions,$defs, or OpenAPIcomponents.schemas(see fixtures undertests/jsonschema_import/inputs). $refresolution for#/definitions/...,#/$defs/..., and#/components/schemas/....
Type/shape mapping (when x-flatbuffers is not present):
type: "object"→ FlatBuffers table by default; may infer a struct if the definition contains fixed-length arrays and is otherwise “struct-safe” (seesrc/idl_parser.cpp).type: "array"→ FlatBuffers vector; ifminItems == maxItemsit may become a fixed-length array (and will fall back to a vector withminItems/maxItemspreserved if a fixed array would be illegal in FlatBuffers).type: "integer"→ a concrete FlatBuffers integer scalar inferred from numeric range (minimum/maximum) when provided;formatofint32/int64/uint32/uint64overrides inference.type: "number"→floatby default;formatoffloat/doubleoverrides.type: "string"→ FlatBuffersstring.- String
enum: ["A", "B", ...]on a field → generates a FlatBuffers enum for that field. anyOf: [{ "$ref": ... }, ...]on a field → FlatBuffers union. If the input follows the FlatBuffers JSON/JSON-Schema union convention (a value field plus a sibling<name>_typefield), the importer will link them (seesrc/idl_parser.cpp).
JSON Schema/OpenAPI keyword preservation:
To keep the generated JSON Schema close to the original, the importer preserves a subset of JSON Schema/OpenAPI keywords (either as FlatBuffers doc flags, or as jsonschema_* attributes so they survive through the schema IR), and the JSON Schema generator re-emits them:
- Definitions + fields:
description - Fields:
deprecated - Objects:
required,additionalProperties - Arrays:
minItems,maxItems,uniqueItems - Strings:
format,minLength,maxLength,readOnly - Numbers/integers:
minimum,maximum,exclusiveMinimum,exclusiveMaximum
JSON Schema cannot represent some FlatBuffers semantics (for example: struct vs table, exact scalar widths, union details, field ids, and presence rules). To enable lossless round-trips, the JSON Schema generator can emit an optional vendor extension:
flatc --jsonschema --jsonschema-xflatbuffers -o out_dir schema.fbsThis emits x-flatbuffers metadata objects at the schema root, at each definition, and at each field. Because this uses the standard vendor extension mechanism (x-...), most JSON Schema tooling ignores it and continues to work normally (for example QuickType and similar code generators).
At a high level:
- Root metadata:
root_type, plus optionalfile_identifierandfile_extension. - Definition metadata: enum/union kind + values; struct/table kind + (struct-only)
minalign/bytesize. - Field metadata: exact FlatBuffers type (including union/enum refs), plus presence and selected field attributes (for example
id,deprecated,key).
The allowed keys/values for the x-flatbuffers vendor extension are described by the meta-schema:
This fork uses golden JSON Schemas and round-trip tests to ensure stability:
- Generation + round-trip for FlatBuffers-emitted JSON Schema (with and without
x-flatbuffers):tests/JsonSchemaTest.sh - Import “wild” JSON Schema / OpenAPI fixtures and ensure stable regeneration:
tests/JsonSchemaImportTest.sh- Inputs:
tests/jsonschema_import/inputs - Goldens:
tests/jsonschema_import/goldens
- Inputs:
Typical local run:
cmake -B build -S .
cmake --build build --target flatc -j
tests/JsonSchemaTest.sh
tests/JsonSchemaImportTest.shFlatBuffers 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.
-
Build the compiler for flatbuffers (
flatc)Use
cmaketo create the build files for your platform and then perform the compilation (Linux example).cmake -G "Unix Makefiles" make -j -
Define your flatbuffer schema (
.fbs)Write the schema to define the data you want to serialize. See monster.fbs for an example.
-
Generate code for your language(s)
Use the
flatccompiler to take your schema and generate language-specific code:./flatc --cpp --rust monster.fbsWhich generates
monster_generated.handmonster_generated.rsfiles. -
Serialize data
Use the generated code, as well as the
FlatBufferBuilderto construct your serialized buffer. (C++example) -
Transmit/store/save Buffer
Use your serialized buffer however you want. Send it to someone, save it for later, etc...
-
Read the data
Use the generated accessors to read the data from the serialized buffer.
It doesn't need to be the same language/schema version, FlatBuffers ensures the data is readable across languages and schema versions. See the
Rustexample reading the data written byC++.
Go to our landing page to browse our documentation.
- Windows
- macOS
- Linux
- Android
- And any others with a recent C++ compiler (C++ 11 and newer)
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
FlatBuffers does not follow traditional SemVer versioning (see rationale) but rather uses a format of the date of the release.
- FlatBuffers Issues Tracker to submit an issue.
- stackoverflow.com with
flatbufferstag for any questions regarding FlatBuffers.
To contribute to this project, see CONTRIBUTING.
Please see our Security Policy for reporting vulnerabilities.
Flatbuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.