Skip to content

Commit e4c2ab3

Browse files
committed
docs: update README with installation instructions and API reference
- Added detailed installation instructions for both PyPI and source installation. - Included prerequisites for building from source. - Expanded API reference section with examples for `ark_fbs.Schema` and `ark_fbs.Options` usage. - Added examples for loading schemas from `.fbs` and `.bfbs` files.
1 parent fefe83a commit e4c2ab3

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,50 @@ This is a standalone PyPI package that provides a thin `pybind11` wrapper around
77
- JSON -> FlatBuffer binary
88
- FlatBuffer binary -> JSON
99

10+
## Installation
11+
12+
### Install from PyPI
13+
14+
```bash
15+
python -m pip install -U ark-fbs
16+
```
17+
18+
### Install from source
19+
20+
This repo vendors FlatBuffers source (recommended) and requires an extra header-only
21+
dependency for your FlatBuffers fork.
22+
23+
Prerequisites:
24+
25+
- Python \(\ge\) 3.10
26+
- A C++ toolchain (C++14) + CMake \(\ge\) 3.32
27+
- Git (to fetch submodules)
28+
29+
```bash
30+
git clone --recurse-submodules <your-repo-url>
31+
cd ark_fbs
32+
33+
# Ensure the expected deps exist
34+
# - third_party/flatbuffers/ (git submodule)
35+
# - third_party/nlohmann_json/nlohmann/json.hpp
36+
37+
python -m pip install -U pip
38+
python -m pip install -U build
39+
python -m pip install .
40+
```
41+
42+
If you don't want to use the vendored FlatBuffers source, you can build against a
43+
system/externally provided FlatBuffers by configuring CMake with:
44+
45+
- `ARK_FBS_VENDOR_FLATBUFFERS=OFF` (then `find_package(Flatbuffers REQUIRED)` is used), or
46+
- `ARK_FBS_FLATBUFFERS_SOURCE_DIR=/path/to/flatbuffers` (when vendor mode is ON)
47+
1048
## Repo layout
1149

1250
- `cpp/`: C++ binding source (pybind11)
1351
- `src/ark_fbs/`: Python package wrapper + typing stubs
1452
- `third_party/flatbuffers/`: **your fork** of FlatBuffers as a git submodule (recommended)
53+
- `third_party/nlohmann_json/`: single-header `nlohmann/json.hpp` (required by your FlatBuffers fork)
1554

1655
## Local build (editable)
1756

@@ -32,6 +71,105 @@ b = s.json_to_binary('{"x":1}')
3271
print(s.binary_to_json(b))
3372
```
3473

74+
### Load from a `.fbs` file (with include paths)
75+
76+
```python
77+
import ark_fbs
78+
79+
schema = ark_fbs.Schema.from_fbs_file(
80+
"schema.fbs",
81+
include_paths=["./includes", "./third_party/schemas"],
82+
)
83+
data = schema.json_to_binary('{"x": 123}')
84+
print(schema.binary_to_json(data))
85+
```
86+
87+
### Options and root type override
88+
89+
```python
90+
import ark_fbs
91+
92+
opts = ark_fbs.Options()
93+
opts.strict_json = True
94+
opts.defaults_json = True
95+
opts.size_prefixed = False
96+
97+
schema = ark_fbs.Schema.from_fbs_text(
98+
"namespace t; table A { x:int; } root_type A;",
99+
options=opts,
100+
root_type_override="A",
101+
)
102+
```
103+
104+
### Load from `.bfbs` (serialized schema)
105+
106+
```python
107+
import ark_fbs
108+
109+
schema = ark_fbs.Schema.from_fbs_text("namespace t; table A { x:int; } root_type A;")
110+
bfbs = schema.serialize_schema_bfbs()
111+
112+
schema2 = ark_fbs.Schema.from_bfbs(bfbs)
113+
print(schema2.binary_to_json(schema2.json_to_binary('{"x": 1}')))
114+
```
115+
116+
## API Reference
117+
118+
### `ark_fbs.Options`
119+
120+
`Options()` controls how schemas are parsed and how JSON/text is emitted.
121+
122+
Fields (all are `bool`):
123+
124+
- `strict_json` (default `True`): Require strict JSON input when parsing JSON.
125+
- `natural_utf8` (default `True`): Emit/interpret UTF-8 in a “natural” way (FlatBuffers option).
126+
- `defaults_json` (default `True`): Include default scalar values in emitted JSON.
127+
- `size_prefixed` (default `False`): Treat binary buffers as size-prefixed.
128+
- `output_enum_identifiers` (default `False`): Output enum identifiers rather than numeric values.
129+
130+
### `ark_fbs.Schema`
131+
132+
`Schema` wraps a `flatbuffers::Parser` instance.
133+
134+
#### Constructors
135+
136+
- `Schema.from_fbs_file(schema_path: str, include_paths: list[str] = [], options: Options = Options(), root_type_override: str = "") -> Schema`
137+
- **schema_path**: Path to the `.fbs` file.
138+
- **include_paths**: Extra include directories for `include "foo.fbs"` resolution.
139+
- **options**: Parsing/JSON options.
140+
- **root_type_override**: If non-empty, forces the root type (overrides `root_type` in schema).
141+
- **Raises**: `ValueError` if the schema file cannot be loaded, parsing fails, or `root_type_override` is unknown.
142+
143+
- `Schema.from_fbs_text(schema_text: str, include_paths: list[str] = [], options: Options = Options(), source_filename: str = "", root_type_override: str = "") -> Schema`
144+
- **schema_text**: The schema content (as text).
145+
- **source_filename**: Optional filename used in error messages and for include resolution context.
146+
- **Raises**: `ValueError` on parse errors or unknown root type override.
147+
148+
- `Schema.from_bfbs(bfbs: bytes, options: Options = Options(), root_type_override: str = "") -> Schema`
149+
- **bfbs**: Serialized schema bytes (`.bfbs`).
150+
- **Raises**: `ValueError` if deserialization fails or `root_type_override` is unknown.
151+
152+
#### Methods
153+
154+
- `Schema.json_to_binary(json: str) -> bytes`
155+
- Parses JSON using the loaded schema and returns the FlatBuffer binary.
156+
- **Raises**: `ValueError` if JSON parsing fails, or if no root type is set.
157+
158+
- `Schema.binary_to_json(data: bytes) -> str`
159+
- Converts a FlatBuffer binary (for the schema's root type) to JSON text.
160+
- **Raises**: `ValueError` if conversion fails, or if no root type is set.
161+
162+
- `Schema.serialize_schema_bfbs() -> bytes`
163+
- Serializes the currently loaded schema into `.bfbs` bytes.
164+
- **Raises**: `ValueError` if the schema is not initialized.
165+
166+
#### Root type behavior
167+
168+
`Schema.json_to_binary()` and `Schema.binary_to_json()` require a root type. You must either:
169+
170+
- Provide `root_type` in the schema (`root_type A;`), or
171+
- Pass `root_type_override="A"` when constructing the `Schema`.
172+
35173
### Wheels / PyPI
36174

37175
CI uses `cibuildwheel` to produce wheels for multiple CPython versions and

0 commit comments

Comments
 (0)