Skip to main content

Crate json_bourne

Crate json_bourne 

Source
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)] (the derive feature, on by default) generates the typed impls. The generated code is itself no_std; only the compile-time derive pulls in the proc-macro stack.
  • no_std everywhere. The streaming Lexer / Parser layer is no_std always; with the default std feature off the crate is no_std + alloc, and with alloc off too it is pure no_std.
  • Borrowed strings by default. &'input str and Cow<'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

SinkEntry point
Stringto_string
Vec<u8>to_vec
String (pretty-printed)to_string_pretty
any std::io::Writeto_writer (std only)
any core::fmt::Writeto_fmt

Custom sinks implement JsonWrite directly.

§Features

FeatureDefaultPurpose
stdyesHashMap, std::net, std::path, to_writer
allocyesString, Vec, Box/Rc/Arc, escape decoding
indexmapnoIndexMap / 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§

ByteSink
JsonWrite sink that appends to a Vec<u8>.
Checkpoint
Saved lexer position + nesting depth, restorable via Lexer::restore.
Error
FmtWriteSink
JsonWrite sink that forwards to any core::fmt::Write implementor.
IoWriteSink
JsonWrite sink that forwards to any std::io::Write implementor.
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.
LineColumn
Line and column reconstructed from a Position against 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.
PrettyStringSink
JsonWrite sink that emits indented, multi-line JSON.
StringSink
JsonWrite sink that appends to a String. Infallible.

Enums§

ErrorKind
Event
A single event from the streaming parser.
ValueKind
Result of Lexer::peek_value_kind. Tells a caller what kind of value will be produced by the next read_* 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§

FromJson
Types that know how to deserialize themselves from JSON via a Lexer.
JsonWrite
Output sink for ToJson impls.
MapKey
Internal adapter from a decoded JSON key to the user’s key type.
MapKeyOut
Sealed adapter from a map’s key type to the borrowed &str form.
ToJson
Types that know how to serialize themselves to a JsonWrite sink.

Functions§

key_to_cow
Materialize an object key from a JsonStr span. Borrows when the key is escape-free; decodes into an owned String otherwise.
parse
Parse a value of type T from a slice of JSON bytes.
parse_str
Parse from a &str.
to_fmt
Serialize value into any core::fmt::Write sink.
to_string
Serialize value into a fresh String.
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 value into a fresh Vec<u8>.
to_writer
Serialize value directly into a std::io::Write sink.