This is a standalone PyPI package that provides a thin pybind11 wrapper around
flatbuffers::Parser and GenerateText:
- load
.fbs/.bfbs - JSON -> FlatBuffer binary
- FlatBuffer binary -> JSON
python -m pip install -U ark-fbscpp/: C++ binding source (pybind11)src/ark_fbs/: Python package wrapper + typing stubsthird_party/flatbuffers/: your fork of FlatBuffers as a git submodule (recommended)third_party/nlohmann_json/: single-headernlohmann/json.hpp(required by your FlatBuffers fork)
uv sync --group dev
uv pip install -e .import ark_fbs
s = ark_fbs.Schema.from_fbs_text(
"namespace t; table A { x:int; } root_type A;"
)
b = s.json_to_binary('{"x":1}')
print(s.binary_to_json(b))import ark_fbs
schema = ark_fbs.Schema.from_fbs_file(
"schema.fbs",
include_paths=["./includes", "./third_party/schemas"],
)
data = schema.json_to_binary('{"x": 123}')
print(schema.binary_to_json(data))import ark_fbs
opts = ark_fbs.Options( # keyword-only
strict_json=True,
natural_utf8=True,
defaults_json=True,
size_prefixed=False,
output_enum_identifiers=True,
)
schema = ark_fbs.Schema.from_fbs_text(
"namespace t; table A { x:int; } root_type A;",
options=opts,
root_type_override="A",
)import ark_fbs
schema = ark_fbs.Schema.from_fbs_text("namespace t; table A { x:int; } root_type A;")
bfbs = schema.serialize_schema_bfbs()
schema2 = ark_fbs.Schema.from_bfbs(bfbs)
print(schema2.binary_to_json(schema2.json_to_binary('{"x": 1}')))Options(...) controls how schemas are parsed and how JSON/text is emitted.
Constructor (keyword-only):
Options(*, strict_json: bool = True, natural_utf8: bool = True, defaults_json: bool = True, size_prefixed: bool = False, output_enum_identifiers: bool = True)
Fields (all are bool, same defaults as constructor):
strict_json(defaultTrue): Require strict JSON input when parsing JSON.natural_utf8(defaultTrue): Emit/interpret UTF-8 in a “natural” way (FlatBuffers option).defaults_json(defaultTrue): Include default scalar values in emitted JSON.size_prefixed(defaultFalse): Treat binary buffers as size-prefixed.output_enum_identifiers(defaultTrue): Output enum identifiers rather than numeric values.
Schema wraps a flatbuffers::Parser instance.
-
Schema.from_fbs_file(schema_path: str, include_paths: list[str] = [], options: Options = Options(), root_type_override: str = "") -> Schema- schema_path: Path to the
.fbsfile. - include_paths: Extra include directories for
include "foo.fbs"resolution. - options: Parsing/JSON options.
- root_type_override: If non-empty, forces the root type (overrides
root_typein schema). - Raises:
ValueErrorif the schema file cannot be loaded, parsing fails, orroot_type_overrideis unknown.
- schema_path: Path to the
-
Schema.from_fbs_text(schema_text: str, include_paths: list[str] = [], options: Options = Options(), source_filename: str = "", root_type_override: str = "") -> Schema- schema_text: The schema content (as text).
- source_filename: Optional filename used in error messages and for include resolution context.
- Raises:
ValueErroron parse errors or unknown root type override.
-
Schema.from_bfbs(bfbs: bytes, options: Options = Options(), root_type_override: str = "") -> Schema- bfbs: Serialized schema bytes (
.bfbs). - Raises:
ValueErrorif deserialization fails orroot_type_overrideis unknown.
- bfbs: Serialized schema bytes (
-
Schema.json_to_binary(json: str) -> bytes- Parses JSON using the loaded schema and returns the FlatBuffer binary.
- Raises:
ValueErrorif JSON parsing fails, or if no root type is set.
-
Schema.binary_to_json(data: bytes) -> str- Converts a FlatBuffer binary (for the schema's root type) to JSON text.
- Raises:
ValueErrorif conversion fails, or if no root type is set.
-
Schema.serialize_schema_bfbs() -> bytes- Serializes the currently loaded schema into
.bfbsbytes. - Raises:
ValueErrorif the schema is not initialized.
- Serializes the currently loaded schema into
Schema.json_to_binary() and Schema.binary_to_json() require a root type. You must either:
- Provide
root_typein the schema (root_type A;), or - Pass
root_type_override="A"when constructing theSchema.
CI uses cibuildwheel to produce wheels for multiple CPython versions and
architectures. See .github/workflows/wheels.yml.