Expand description
Type-driven JSON: parse straight into the caller’s chosen type and
serialize from it, with no generic Value middle layer.
json-bourne skips the dynamic-tree intermediate that crates like
serde_json use. Each type knows how to deserialize itself from a
Lexer (via FromJson) or write itself to a JsonWrite
sink (via ToJson). The typed structure already enforces JSON’s
grammar, so the per-event state machine is pure overhead for typed
consumers — skipping it makes the typed path ~2× faster on
integer / string-heavy payloads.
- Derive-driven.
#[derive(FromJson, ToJson)](thederivefeature, on by default) generates the typed impls. The generated code is itselfno_std; only the compile-time derive pulls in the proc-macro stack. no_stdeverywhere. The streamingLexer/Parserlayer isno_stdalways; with the defaultstdfeature off the crate isno_std + alloc, and withallocoff too it is pureno_std.- Borrowed strings by default.
&'input strandCow<'input, str>parse zero-copy when the input contains no escapes. - Bounded by construction. Container nesting is depth-limited (default 128, const-generic). The streaming parser is a state machine with no recursion.
§Quick start
Parse a primitive directly into a Rust type:
use json_bourne::parse_str;
let n: u32 = parse_str("42").unwrap();
assert_eq!(n, 42);Parse a struct with #[derive(FromJson)]:
use json_bourne::{FromJson, parse_str};
#[derive(Debug, PartialEq, FromJson)]
struct User<'input> {
id: u64,
name: &'input str,
active: bool,
}
let u: User<'_> = parse_str(r#"{"id":1,"name":"alice","active":true}"#).unwrap();
assert_eq!(u.name, "alice");Serialize back out with #[derive(ToJson)]:
use json_bourne::{ToJson, to_string};
#[derive(ToJson)]
struct Point { x: i32, y: i32 }
let s = to_string(&Point { x: 3, y: -7 }).unwrap();
assert_eq!(s, r#"{"x":3,"y":-7}"#);§Output destinations
| Sink | Entry point |
|---|---|
String | to_string |
Vec<u8> | to_vec |
String (pretty-printed) | to_string_pretty |
any std::io::Write | to_writer (std only) |
any core::fmt::Write | to_fmt |
Custom sinks implement JsonWrite directly.
§Features
| Feature | Default | Purpose |
|---|---|---|
std | yes | HashMap, std::net, std::path, to_writer |
alloc | yes | String, Vec, Box/Rc/Arc, escape decoding |
indexmap | no | IndexMap / IndexSet (insertion order) |
default-features = false plus ["alloc"] gives a no_std + alloc
build. default-features = false alone gives pure no_std: only the
streaming Lexer / Parser and typed APIs that don’t need a
heap (e.g. parsing into stack-allocated primitives) are available.
Structs§
- Byte
Sink JsonWritesink that appends to aVec<u8>.- Checkpoint
- Saved lexer position + nesting depth, restorable via
Lexer::restore. - Error
- FmtWrite
Sink JsonWritesink that forwards to anycore::fmt::Writeimplementor.- IoWrite
Sink JsonWritesink that forwards to anystd::io::Writeimplementor.- JsonNum
- A JSON number span —
(start, end)into the original input. - JsonStr
- A JSON string span — a
(start, end)pair into the original input. - Lexer
- Stateless JSON lexer over a borrowed byte slice.
- Line
Column - Line and column reconstructed from a
Positionagainst its input. - Parser
- Streaming JSON parser over a borrowed byte slice.
- Position
- Byte offset into the input where an event was emitted or an error fired.
- Pretty
String Sink JsonWritesink that emits indented, multi-line JSON.- String
Sink JsonWritesink that appends to aString. Infallible.
Enums§
- Error
Kind - Event
- A single event from the streaming parser.
- Value
Kind - Result of
Lexer::peek_value_kind. Tells a caller what kind of value will be produced by the nextread_*call without committing to it.
Constants§
- DEFAULT_
MAX_ DEPTH - Default maximum container nesting depth.
- MAX_
INPUT_ LEN - Maximum byte size of a JSON document this parser will accept. Bounded by
our packed-offset representation in
Event.
Traits§
- From
Json - Types that know how to deserialize themselves from JSON via a
Lexer. - Json
Write - Output sink for
ToJsonimpls. - MapKey
- Internal adapter from a decoded JSON key to the user’s key type.
- MapKey
Out - Sealed adapter from a map’s key type to the borrowed
&strform. - ToJson
- Types that know how to serialize themselves to a
JsonWritesink.
Functions§
- key_
to_ cow - Materialize an object key from a
JsonStrspan. Borrows when the key is escape-free; decodes into an ownedStringotherwise. - parse
- Parse a value of type
Tfrom a slice of JSON bytes. - parse_
str - Parse from a
&str. - to_fmt
- Serialize
valueinto anycore::fmt::Writesink. - to_
string - Serialize
valueinto a freshString. - to_
string_ pretty - Pretty-printed equivalent of
to_string. Two-space indent, one space after:, newline between every element. Empty containers are kept compact ([]/{}). - to_vec
- Serialize
valueinto a freshVec<u8>. - to_
writer - Serialize
valuedirectly into astd::io::Writesink.