koda-rs
Rust implementation of KODA (Compact Object Data Architecture): a compact, deterministic format for structured data with both a human-friendly text form and a binary wire format compatible with koda-go.
Features
- Parse KODA text into a dynamic
Value(objects, arrays, scalars) - Stringify
Valueback to text (deterministic, sorted keys) - Encode
Valueto binary (Go-compatible, deterministic) - Decode binary to
Value - Streaming parser —
parse_readerfor bounded-memory parsing of large inputs - Structured errors —
KodaErrorwith parse position and encode/decode messages - Deterministic output —
BTreeMapfor object keys; binary layout matches koda-go
Optional Features
| Feature | Description |
|---|---|
parallel |
Use rayon for parallel decoding of large arrays and objects via decode_parallel |
serde |
Serde support: from_str / to_string for any Serialize/Deserialize type |
Quick Start
Add to Cargo.toml:
[]
= "0.1"
Example:
use ;
Data Model
Objects use BTreeMap for deterministic key order.
Text Format
- Key-value:
key: value(commas optional; newline or comma can separate fields) - Strings:
"..."with\",\\,\n,\r,\t - Numbers: integers and floats (e.g.
1,3.14) - Booleans:
true,false - Null:
null - Objects:
{ key: value, ... }or a key-value block at root - Arrays:
[ value, ... ] - Line comments:
// ...
Binary Format
- Magic:
KODA(4 bytes), version (1 byte) - Dictionary:
u32count, then for each keyu32length + UTF-8 bytes (keys unique, sorted lexicographically) - Root value with type tags: null, false, true, integer (8-byte big-endian), float (8-byte IEEE 754), string (4-byte length + UTF-8), array, object (key = dictionary index)
- Compatible with koda-go for encode/decode
Performance Notes
- Binary is typically smaller than JSON for repetitive structures (keys stored once in dictionary)
- Text is human-friendly with minimal syntax overhead
- Enable the
parallelfeature for faster decoding of large arrays/objects (e.g. 10k+ elements) - Use
parse_readerfor very large inputs to keep memory bounded (chunked streaming)
Comparison with JSON
| Aspect | JSON | KODA (this crate) |
|---|---|---|
| Text | Ubiquitous, many tools | Key-value style, minimal syntax |
| Binary | Not standard | Yes, with key dictionary |
| Key reuse | Repeated in every field | Once per key in dictionary (binary) |
| Determinism | Order-dependent | Deterministic (binary and stringify) |
| Interop | Universal | Rust + Go (binary); custom text |
Use JSON when you need maximum interoperability; use KODA when you want a compact, deterministic binary format and a simple text format in Rust (and Go).
Benchmarks
Run all benchmarks:
-
Decode (binary) — Sequential and (with
parallelfeature) parallel decoding: -
Koda vs JSON — Serialization and parsing compared to
serde_json(requiresserdefeature):
License
MIT