Kahon for Rust
A Rust writer for the Kahon binary format.
Usage
cargo add kahon
Builder API
use Writer;
let mut buf: = Vecnew;
let w = new;
// `start_object` consumes the empty `Writer` and returns a root builder.
let mut monster = w.start_object;
monster.push_i64?;
monster.push_bool?;
// `.end()` returns the writer in its `Filled` state, ready for `.finish()`.
let w = monster.end?;
w.finish?;
The builder API uses typestate to enforce one root container at compile time.
Advanced Use Cases
For more advanced use cases, we have the raw::RawWriter. Mismatched frames and stray keys surface as runtime
errors instead.
use RawWriter;
let mut buf: = Vecnew;
let mut w = new;
w.begin_object?;
w.push_key?;
w.push_i64?;
w.push_key?;
w.push_bool?;
w.push_key?;
w.begin_array?;
w.push_str?;
w.begin_object?;
w.push_key?;
w.push_str?;
w.push_key?;
w.push_i64?;
w.end_object?;
w.end_array?;
w.end_object?;
w.finish?;
Features
- Memory Friendly: Memory can stay bounded by how deep your JSON is, not document size.
- Fast Value Retrieval: Similar to SQLite, arrays and objects are stored as B+trees: index into a million-element array, or look up a key in a million-key object, without scanning for the whole thing first.
- Tuneable: There's a bunch of options to balance out memory usage and read performance to your liking.
Tuning
use ;
// Disk-tuned: each B+tree node targets one page, trailer is page-aligned.
let opts = WriterOptions ;
let w = with_options?;
The default (BuildPolicy::compact(128)) produces the tightest output and is best for in-memory or network use. disk_aligned trades a small amount of unreferenced padding for layout that plays well with the page cache.
Status
Pre-1.0. Stuff is still moving and breaking quite a lot due to having to play with the API so there's no guarantee of stability at the moment. The on-disk format is governed by an external spec; comments in the source cite section numbers. A reference reader lives under tests/common/ and is exercised by the golden-file and property tests.
License
All source code is licensed under MIT.
All contributions are to be licensed as MIT.